Home > Net >  How do i make this movement script work in C#
How do i make this movement script work in C#

Time:10-29

i was following a C# tutorial on YouTube about movement but the code didn't work i tried to find the issue but i found nothing. can someone please help me?

public class PlayerMovement : MonoBehaviour
{

    public float moveSpeed;

    CharacterController ch;


    void Start()
    {
        ch = GetComponent<CharacterController>();
    }


    void Update()
    {
        float x = input.GetAxis('Horizontal') * moveSpeed * Time.deltaTime;
        float z = input.GetAxis('Vertical') * moveSpeed * Time.deltaTime;
        ch.Move(x,0, z);
    }
}

i tried to make movement

CodePudding user response:

According to the documentation (https://docs.unity3d.com/ScriptReference/CharacterController.Move.html), there is no overload for (x, y, z).

Try to move like this:

float frameSpeed = moveSpeed * Time.deltaTime;
ch.Move(new Vector3(
    Input.GetAxis("Horizontal") * frameSpeed,
    0.0f,
    Input.GetAxis("Vertical") * frameSpeed);

Also, input is Input, and strings need "", not '' (those are for chars).

  • Related