player moves without touching keys

I was making the player move in a 3D game, but after it touches a wall it moves when I don’t use the keys. Can anyone help?

using UnityEngine;

public class playermoving : MonoBehaviour
{
    public float speed = .1f;
    // Update is called once per frame
    void Update()
    {
        float xDirection = Input.GetAxis("Horizontal");
        float zDirection = Input.GetAxis("Vertical");

        Vector3 moveDirection = new Vector3(xDirection, 0.0f, zDirection);

        transform.position += moveDirection * speed;
    }
}

Does this player have a Rigidbody? If so, use the .MovePosition() method on the Rigidbody, don’t use transform.position

1 Like

If it isn’t the Rigidbody issue, unplug your joystick/controller.

1 Like

I tried this one, but it gives me, error CS1513: } expected.

How to understand errors in general:

https://discussions.unity.com/t/824586/8

Thanks a lot! But I can’t find the missing ‘;’ it says on (13,75) but whenever I do something on row 13 it says 79

Often one error will snowball into others, like missing semicolons or extra/too few { }.
Check to be sure Visual Studio is formatting your document correctly, and do it on Save so you can always have it up to date. Proper formatting will show you most of these structure-based errors.

I think I got the old error out of it, but now it gives me error CS1003: syntax error, ‘,’ expected, I looked up some help but I can’t find it. my new code, I hope someone can help me find the mistake I made, I’m still new with coding.

using UnityEngine;

public class playermoving : MonoBehaviour
{
    public float speed = .1f;

    // Update is called once per frame
    void Update()
    {
        float xDirection = Input.GetAxis("Horizontal");
        float zDirection = Input.GetAxis("Vertical");

        Vector3 moveDirection = new Vector3(xDirection, 0.0f, zDirection);

        Rigidbody.MovePosition(Vector3 moveDirection = new Vector3(xDirection, 0.0f, zDirection)) += moveDirection * speed;
    }
}

You have two assignments on line 15. One after the = and one after the +=. Split that up in to more than one step, one on each line.

You also want to store a reference to a specific rigidbody instance rather than calling MovePosition on the RigidBody class itself, or you’ll get an ‘Object reference required for non-static field’ error.

I tried the split it up in 2 lines, but it says (15,40) and that is between the vector3 and the first MoveDirection, but I also don’t understand the specific rigid body part, sorry

I could be wrong, but I think you intend line 15 to be this:

Rigidbody.MovePosition(somePositionYouWantToMoveTo);

That alone won’t quite work though because you can’t refer to a function by SomeClassName.SomeFunction() unless the class is static. If the class is an instance class instead of a static class (your Rigidbody component is an instance class), you have to

  1. make an instance of it
  2. store a reference to the instance.
  3. use the class through that reference.

Like this:

Assuming your gameObject has a Rigidbody component, that’s your instance created already. No code needed. Now you have to get a reference to this instance. Usually this is done using GetComponent in start, assuming your Rigidbody is on the same gameObject as this script.

    Rigidbody rb;

    private void Start()
    {
        rb = this.gameObject.GetComponent<Rigidbody>();
    }
 
    void Update()
    {
        //now you access all the Rigidbody functions by saying rb.SomeFunction(); like this:
        rb.MovePosition(someVector3MoveToPositionGoesHere);
    }

Note that on line 5 ‘this.gameObject’ is redundant, but I put it there because it helps illustrate where GetComponent is getting the from.