Move, hit a wall and reverse movement.

Hello,

I’m looking to have an object move forward constantly on its own in a 2D environment (Sidescroller.)

However, I would like the object to rotate and move in the opposite direction when it collides with a wall.

At the moment, I am using the following script to rotate the character:

var box : GameObject;


function OnCollision( hit : Collision)

{ 

box.transform.Rotate(45,45,45);


}

And where I think some problem may lie – the code to move the character constantly.

var velocity : Vector3

;function Update()
{
transform.localPosition += velocity * Time.deltaTime;
}

Now I believe the constant velocity here is keeping the X as a constant to the world instead of local, but changing that was of no benefit.

At the moment, the colliders seem to be blocking one another from rotating. Or simply not doing anything – I’m not sure.

The end result I’m hoping for is for an asset (In this case, a box.) to move backwards and forwards along a 2D plain – changing direction when it hits a wall and continuing on its merry way.

If you're rotating an object 180 degrees, you'll only need to rotate it along one axis, what you probably want to do is Rotate(0,180,0).

And the function best to put it in is probably OnCollisionEnter() which is called once, at the start of collision.

To make sure that a collision event is happening, you'll want to ensure one of the objects has a rigidbody.

Edit: To solve your local Position & velocity problem I would suggest using Transform.Translate() and / or indeed using a specific vector like Vector3.forward. Your velocity vector could be a float, which amplifies Vector3.forward in accordance with time:

transform.Translate(Vector3.forward * velocity * Time.deltaTime);

That will move the object along the Z-axis. Velocity won't need to be negative so long as you rotate it 180 degrees about the Y-axis when it collides. Just remember to use OnCollisionEnter() as the collision function.

It is a local vs. global problem. Say velocity is (3,0,0). Adding to your pos always move you right in the game world.

Easiest way to get local movement is to key off of transform.forward, which is your local +1 forwards. Keep velocity as a float: transform.position+=transform.forward*velocity*Time.deltaTime;

You can also use Transform.Translate(0,0,velocity*Time.deltaTime);. That’s set up to normally use your local axis.

It you want to keep your style, can explicity rotate your velocity vector to local (from memory): transform.position += transform.TransformDirection(velocity)*Time.deltaTime; The TransDir function says to spin velocity to your local coords.

Ok, I see what you’re trying to do. I think you need a parent object with only a (bouncy) sphere collider and rigidbody on it, then a child object with your mesh filter and mesh renderer on it.

Then your script can check every so often if the local rotation of the child object matches the velocity vector of the rigidbody, and if not, RotateTowards until it does.

Changing the Movement script to “Time.delteTime” seemd to make it work - I’m not sure why, I think it was a case of it simply not changing its move direction! Thanks guys.