Hi All, I need your help.
I have script to rotate my character via joystick.
public class PlayerController : MonoBehaviour {
[SerializeField]
private Animator animator;
[SerializeField]
private float directionDampTime = .25f;
private float speed = 0.0f;
private float h = 0.0f;
private float v = 0.0f;
private float turnSmoothing = 2f;
void Start () {
animator = GetComponent();
if (animator.layerCount >= 2) {
animator.SetLayerWeight (1, 1);
}
}
void Update () {
if (animator) {
h = CrossPlatformInputManager.GetAxis ("Horizontal");
v = CrossPlatformInputManager.GetAxis ("Vertical");
speed = new Vector2 (h, v).sqrMagnitude;
animator.SetFloat ("Speed", speed);
animator.SetFloat ("Direction", h, directionDampTime, Time.deltaTime);
if (speed > 0) {
RotatingPlayer (h, v);
}
}
}
void RotatingPlayer(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3 (horizontal, 0.0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp (transform.rotation, targetRotation, turnSmoothing * Time.deltaTime);
transform.rotation = newRotation;
}
}
So, I don't understand how to work with vectors and quaternions, and i need character move to joystick direction relatively own face, but not relatively world coordinates...how to do this? I tried to illustrate what i need, hope it's clear.
![alt text][1]
p.s. sorry for english)
[1]: /storage/temp/81183-безымянныи.png
↧