Rotate player y with A/D is buggy.
Hello,i want to rotate the player y with a/d. But its super buggy. Moving forward works but if i rotate 90° moving forward doesnt work anymore. It also looks kind of bad,is there any way to fix it? (Gif should explain all) https://gyazo.com/8c97d17dbc53d6c6e45810da6a435198
Code :
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public Animator anim;
public Rigidbody rbody;
private float inputH;
private float inputV;
public float RotateSpeed = 100f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
rbody = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
inputH = Input.GetAxis ("Horizontal");
inputV = Input.GetAxis ("Vertical");
anim.SetFloat ("inputH", inputH);
anim.SetFloat ("inputV", inputV);
float moveX = inputH * 20f * Time.deltaTime;
float moveZ = inputV * 50f * Time.deltaTime;
rbody.velocity = new Vector3 (moveX, 0f, moveZ);
if (Input.GetKey(KeyCode.A))
transform.Rotate(-Vector3.up * RotateSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up * RotateSpeed * Time.deltaTime);
}
}
Comment
Answer by Pranaryx · Nov 02, 2016 at 09:30 PM
My guess for why rotation looks weird is that the character model is not centered on the transform that is being rotated. Moving forward still works you have just locked it to moving along a certain axis.