Home > Blockchain >  Issue with Player Movement Unity
Issue with Player Movement Unity

Time:01-02

I am VERY new to unity and I wrote this very basic program to move the player. This program works when there is no conditional statement on if the w key is pressed, but if you add it, the capsule is unable to move.

if (Input.GetKeyDown("w"))
{
    Vector3 cameraForward = Camera.main.transform.forward;
    cameraForward.y = 0;
    cameraForward = cameraForward.normalized;
    Vector3 moveDirection = cameraForward * speed * Time.deltaTime;
    transform.position  = moveDirection;
}

The only issue I can see with this is that the capsule is clipping into the plain, although I have a collider and a rigidbody on it. If you run this the capsule just does not move, at all. If it at all matters, I also have another line of code that sets the rotation of the capsule to 0, 0, 0, 0 every frame.

CodePudding user response:

Input.GetKeyDown() only triggers once per key-press. Your biggest issue (there may be others) is that that will only move the player forward for a single tick. You want Input.GetKey() (Unity Docs) instead.

  • Related