简体   繁体   中英

Why is my character flying off randomly Unity3D?

I'm making a third person game and when I try to makie the character (cube) face the way the players mouse is pointing my character just goes flying off even though I didn't use W, A, S or D?

public class FollowPlayer : MonoBehaviour
{
    public Transform player;
    
    public Vector3 CamPosition;
    
    public Transform PlayerBody;
   
    public float MouseSensitivity = 100f;
    
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float MouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
        float MouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
        
        PlayerBody.Rotate(Vector3.up * MouseX);
       
        transform.position = player.position + CamPosition;
     }
}

Did you attach it to the player or the cam?

See if you have a rigidbody attached to the player, it may be buggy due to the way they work in relation with transforms.

Try to mark the line that rotates the player. I believe it's somewhere else that causes the problem because there is nothing in there that moves the player.

Reply me if you found anything that affects the movement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM