1
\$\begingroup\$

I have Unity Pro 4.6.1 and I am trying to make a cube move with the arrow keys. Here are the barriers before I move the cube:

enter image description here

And here's when I move the cube (the problem):

enter image description here

I don't want the cube to go through the blocks, just inside them. Here is my moving code:

#pragma strict

static var selectedId : int;

static var speed : int = 5;

function Update () {

     if (Input.GetKey (KeyCode.UpArrow)) transform.Translate (Vector3(0,0,1) * Time.deltaTime*speed);
     if (Input.GetKey (KeyCode.DownArrow)) transform.Translate (Vector3(0,0,-1) * Time.deltaTime*speed);
     if (Input.GetKey (KeyCode.LeftArrow)) transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
     if (Input.GetKey (KeyCode.RightArrow)) transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
}

How do I fix this?

EDIT: I modified my move code but it still goes through things:

#pragma strict

var speed : float = 10.0;

function Update () {
    var horMovement = Input.GetAxis("Horizontal");
    var forwardMovement = Input.GetAxis("Vertical");

    if (horMovement) {
        transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
    }
    if (forwardMovement) {
        transform.Translate(transform.forward * forwardMovement * Time.deltaTime * speed);
    }
}
\$\endgroup\$
  • \$\begingroup\$ One of the objects must have a rigid body attached. If you attach one and it still doesn't work, try using the rigid body methods to move the object, rather than the transform directly. \$\endgroup\$ – Ben Feb 1 '15 at 20:33
  • \$\begingroup\$ Also, make sure everything has a collider attached. \$\endgroup\$ – Ben Feb 1 '15 at 20:33
  • \$\begingroup\$ How can you tell if it has a rigid body attached, and where can I find the rigid body methods? \$\endgroup\$ – kprovost7314 Feb 1 '15 at 20:35
  • \$\begingroup\$ When you select an object the properties window will display all attached components. Use gameObject.ridigBody to access those methods. Make sure it's not null first, though. \$\endgroup\$ – Ben Feb 1 '15 at 20:55
  • \$\begingroup\$ I added a rigidbody, but now it's acting all crazy. It rotates and goes over the red cubes. \$\endgroup\$ – kprovost7314 Feb 1 '15 at 21:12
1
\$\begingroup\$

Collision detection is not applied when you Translate() a transform directly. In order to apply collision detection while moving, you must Move() a Rigidbody or CharacterController.

\$\endgroup\$
0
\$\begingroup\$

You must add a Rigidbody component to the cube's GameObject; this will allow it to get pushed back by other objects. The cube and all the barriers must have Collider components. You don't need rigidbodies on the barriers. Then, if you do not want the cube to rotate, turn on Freeze Rotation for all axes under the Rigidbody's Constraints.

While it is possible to use transform.Translate, you will get better results if you use rigidbody.MovePosition. This is because Translate attempts to teleport your object to the new position, and then during the next physics step, the colliders will interact. MovePosition will try to check collisions during the move.

You must call rigidbody.MovePosition during FixedUpdate. Notice that instead of a direction, it takes a destination point as argument. Calculate this accordingly. Also, try setting the Rigidbody component to use Interpolate for smoother motion.

As a performance tip, I suggest caching your Transform and Rigidbody as member variables during Awake, because the built-in properties will use GetComponent internally.

\$\endgroup\$
0
\$\begingroup\$

Rigid body collision using the cube mesh may cause you another issue as to get your cube between the gaps requires you to position it exactly. This may turn out to be quite frustrating, especially if you are colliding by one pixel and you cant see it.

I see that your map is created using a minimum of one cube in dimensions ( IE, there is no half cube steps ). If the cube is say 10 units in size, you could make the movement routine always move that amount. So pressing left would move 10 units ( or even better, move 1 unit 10 times for smoothness ). This way you will always be on a cube size boundary.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.