Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with Player Movement Unity

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading