43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
public Transform body;
|
|
public float movingSpeed = 0.1f;
|
|
public float turningSmoothFactor = 0.1f;
|
|
public Joystick joystick;
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
body.Translate(new Vector3(0, movingSpeed), body);
|
|
if (joystick.input != Vector2.zero)
|
|
{
|
|
//Turn
|
|
var angle1 = Mathf.Atan2(-joystick.input.y, -joystick.input.x) * Mathf.Rad2Deg;
|
|
body.rotation = Quaternion.Lerp(body.rotation, Quaternion.AngleAxis(angle1 + 90, Vector3.forward), turningSmoothFactor * joystick.input.magnitude);
|
|
}
|
|
}
|
|
|
|
public float Angle(Vector2 vector2)
|
|
{
|
|
return 360 - (Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg * Mathf.Sign(vector2.x));
|
|
}
|
|
|
|
|
|
//Auto assign default variables [Editor only]
|
|
void OnValidate()
|
|
{
|
|
if (body == null)
|
|
{
|
|
body = transform;
|
|
}
|
|
}
|
|
}
|