how to implement transform.forward

how to implement transform.forward in this script

    private Vector3 _movementVector
{
    get
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");

        return new Vector3(horizontal, 0.0f, vertical);
    }

remove line 8.

then

transform.position += transform.forward * vertical * Time.deltatime;

usually you will want another variable such as “Speed” to increase movement as the above line alone will move slowly.

I guess you’re looking for something like this:

private Vector3 _movementVector
{
    get
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");
        return transform.forward * vertical + transform.right * horizontal;
    }
}
1 Like

My go:

Vector3 _movementVector
    get
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");
        return transform.rotation * new Vector3(horizontal, 0.0f, vertical);
    }
2 Likes

But then you didn’t satisfy the condition of implementing ‘transform.forward’ :stuck_out_tongue:

1 Like

thanks everyone for answering the correct answer:

  • Vector3 _movementVector
  • get
  • {
  • var horizontal = Input.GetAxis(“Horizontal”);
  • var vertical = Input.GetAxis(“Vertical”);
  • return transform.rotation * new Vector3(horizontal, 0.0f, vertical);
  • }