- Home /
Change Direction on Collision
So I've been killing myself over this trying to get it to work and I've finally decided to cave and post here. I'm an artist so I'm trying to learn to script. I'm trying to make an object change direction once it has hit another object. Right now its just a cube moving form one side of the screen to the other. Once it hits the wall on the right I want it to change direction and again when it hits the left wall. Here is the code I have...
var isMoved = true; var change = false;
function Update () {
if (isMoved == true) transform.Translate(Vector3(1,0,0) * Time.deltaTime);
if (change == true) transform.Translate(Vector3(-1,0,0) * Time.deltaTime); }
function OnCollisionEnter (hit:Collision){ if (hit.gameObject.tag == "wallRight")
{
isMoved = false;
change = true;
Debug.Log("hit");
}
if (hit.gameObject.tag == "wallLeft")
{
change = false;
isMoved = true;
Debug.Log("hit");
}
}
My problem is that at the start of the level the cube moves fine to the other side but once it hits the right wall nothing happens. I don't even get the "hit" message in the debug. So something is obviously not registering. Any help is really appreciated. Thank you!
Answer by Statement · Mar 20, 2011 at 08:08 PM
It seems you want to simply invert the x motion of the object when it hits a wall. We can do this by multiplying the x with -1. So for example if the object was moving with direction (4, 7, 1) and hitting one of the walls, it would change direction to (-4, 7, 1). This also means you could change to only use one tag: "xWall" or something. It doesn't matter if it is the left or right wall.
// This is our direction we're travelling in. var direction = Vector3(1, 0, 0);
// Move it along its direction. function Update () { transform.Translate(direction * Time.deltaTime); }
// If we hit a left or right wall, invert x direction. function OnCollisionEnter (hit : Collision) { var tag = hit.gameObject.tag;
if (tag == "wallRight" || tag == "wallLeft") direction.x *= -1; }
Answer by Earth-O-Matic · Mar 20, 2011 at 09:09 PM
Thanks to every one on this one! Finally got it working. This is what I ended up going with for any future people looking for a similar solution. I'm using triggers in this case because I eventually want to use them for other things.
// Direction of movement - X var direction = Vector3(1, 0, 0);
// Move it along its direction. function Update () { transform.Translate(direction * Time.deltaTime); }
// Hit edge of screen - invert x direction. function OnTriggerEnter(collisionInfo : Collider) {
var bump = collisionInfo.gameObject.tag; if( bump == "wallX"); direction.x *= -1; Debug.Log("hit"); }
Thanks Statement, I ended up using your cleaner code. It helped point out the error of my ways. Learning is fun! Thanks again guys!
Answer by Jake-L · Mar 18, 2011 at 08:59 AM
Hi Brian,
you need to set up Colliders on all objects that you want to collide. Have a look at this page, especially look at the collider matrix (down the page under "Advanced") to check if your objects meet all conditions to send the trigger/colliding messages.
In your case you could make the walls a Static Collider and the ball a Kinematic Rigidbody Trigger Collider and catch the collision in a OnTriggerEnter function.
Answer by Meltdown · Mar 18, 2011 at 09:34 AM
Take a look at adding RigidBody components to your cubes, and when they collide with something, look at using RigidBody.AddForce to send them in another direction.
If your colliders in your game are added to the physics system Unity can take care of all the collisions for you automatically instead of you having to worry about them yourself.
Answer by Earth-O-Matic · Mar 20, 2011 at 07:51 PM
So I have physics components on the main object using rigid body but using add force to change its direction gives me some unwanted results. I really want a single steady movement for the object. The idea is a character walking across the screen. As soon as he hits one side of the screen he turns around and same on the other side.
I got the direction to change when the cube hits the right wall now so its detecting my triggers. (ended up using triggers for the side walls) Here is the code I have now....
var isMoved = true; var change = false;
function Update () {
if (isMoved == true)
transform.Translate(Vector3(1,0,0) * Time.deltaTime);
if (change == true)
transform.Translate(Vector3(2,0,0) * Time.deltaTime);
}
function OnTriggerEnter(collisionInfo : Collider){
if(collisionInfo.gameObject.tag == "wallRight")
isMoved = false;
change = true;
Debug.Log("hit");
if(collisionInfo.gameObject.tag == "wallLeft")
change = false;
isMoved = true;
Debug.Log("hit");
}
My problem now. is that it seems to run everything under the On Trigger enter function all at once. The player will hit the right wall. Turn around. Send the "hit" text to the command console but it will send both of them as if it also hit the left wall already. Then once it actually gets to the left wall it just passes right through it. What am I doing wrong here? :-/ Thanks again for all the help.
Your answer
Follow this Question
Related Questions
Direction isn't changing after multiplying it *-1 0 Answers
Change direction on collision problem 4 Answers
Simple Collision Detection not working. [Solved] 3 Answers
Collision Only being detected on one of the objects involved in the collision - C# 0 Answers
How do Unity manages collisions in a game internally? 0 Answers