Making a cube stop if it hits a wall

I want to create a very simple moving cube, that stops if it hits the wall, however I can’t get the cube to stop if it hits the wall at the moment I use this script to move the cube:

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour {

    private float horizontal;
    private float vertical;
    public float speed = 1;

    void Update()
    {
        horizontal = Input.GetAxis ("Horizontal");
        vertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3 (horizontal, 0f, vertical) * speed;

        transform.Translate (movement);

    }

}

And this is the part where I try to stop the cube if it hits the wall named “wall1”, however I would be happy to remove this wall1 if it would be possible, however this doen’t seem to work:

  void OnCollisioEnter(Collision collision)
    {
        if (collision.gameObject.name == "wall1")
        {
            transform.Translate (0f, 0f, 0f);
        }
}

It would be also great if somebody couldshow me how to add gravity to it and how to make the cube jump, but these aren’t as important as my main question.
Thanks for your help in advanced!

You need to move the cube using its Rigidbody, not its Transform. Setting transform position is an instant teleportation and won’t respect the collision settings.

Assuming your cube has a Rigidbody and Collider, you can do this:

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour {

    public float speed = 1;
    public float jumpForce = 10f;

    private float horizontal;
    private float vertical;
    private Rigidbody rb;

    void Awake() {
        rb = GetComponent<Rigidbody>();
    }

    void Update() {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        if(Input.GetButtonDown("Jump")) { // define a jump button in the input settings if there isn't one
            rb.AddForce(jumpForce * Vector3.up, ForceMode.Impulse);
        }
    }

    // physics step (does not run every frame, only on physics update interval)
    void FixedUpdate() {
        Vector3 movement = new Vector3(horizontal, 0f, vertical) * speed;
        rb.AddForce(movement, ForceMode.Impulse);
    }
}

You could also use “rb.MovePosition” to move the object to a specific location, still respecting collisions.

2 Likes

While I understand thaht you use a cube to move, I still want it to slide over the ground and not roll like a ball. With the Forces it reacts like a dice. Is there any way to stop this from happening. This is also the reason why I tried to use transform, cause the cube doesn’t get flipped over if just his position is changed.

Set the velocity rather than use AddForce. Using jeffreyschoch’s excellent code:

  void FixedUpdate() {
  Vector3 movement = new Vector3(horizontal, 0f, vertical) * speed;
  rb.velocity = movement;
  }
1 Like

The cube still moves like a dice and doesn’t slide over it, also the jumping seems to break if I use your code. Is there something I’m missing?

Try restricting rotations in the rigidbody properties - restrict either all, or leave y axis unlocked (f.e. if you want it to be able to spin when hit and/or align nicely to a wall just by moving onto it).

The reason why jumping breaks is because you’re setting the velocity directly - hence whatever was there as the y component of the vector gets overwritten with 0 (this would also allow to slide in the air).
AddForce is the correct solution IMHO, as it doesn’t override anything.

1 Like

Thanks for all the help!