# 2d rotation and translation issue

I am stuck in this weird issue .

First I want that my instantiated object should always translate in the direction of its head but it should be a random direction.

Second ,

I want this translation object to bounce off any object it collides with like this .

Script done so far :

``````    Vector3 directionOfMovement = Vector3.zero;
public bool shouldTranslate = false;

void Start()
{
SetDirection();
Invoke("StartTranslating", 1);
}

void SetDirection()
{
float x = Random.value;
float y = Random.value;

directionOfMovement = new Vector3(x, y, 0);

directionOfMovement = directionOfMovement.normalized;

}

void StartTranslating()
{
shouldTranslate = true;
}

void OnCollisionEnter(Collision other)
{

if (other.gameObject.tag == "Wall" )
{

Vector3 direct = Vector3.Reflect(directionOfMovement, other.contacts[0].normal);

directionOfMovement = direct;
directionOfMovement = directionOfMovement.normalized;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, -transform.rotation.z), 1);

}
}

void FixedUpdate()
{
transform.Translate(directionOfMovement * speed * Time.deltaTime);
}
``````

The question is clearer but I’m still puzzled. You state this is a 2D game, but you are using OnCollisionEnter() so it indicates you have a 3D collider on your object. Given a triangle shape to your object, this indicates a custom mesh?

I guess the last ingredient needed is what side of your object do you consider the front side when the rotation is (0,0,0)? For 3D rotations typically you want the mesh constructed so that when the rotation is (0,0,0), the front side is facing positive ‘z’ and the top side is facing positive ‘y’. If this is the case, you likely want this as the destination rotation for your Quaternion.Slerp():

``````Quaternion qTo = Quaternion.LookRotation(directionOfMovement, Vector3.forward);
``````

If the right side is considered front (as is typical of sprites), then you likely want:

``````float angle = Mathf.Atan2(directionOfMovement.y, directionOfMovement.x) * Mathf.Rad2Deg;
Quaternion qTo = Quaternion.AngleAxis(angle, Vector3.forward);
``````