OnCollisionEnter for translating gameObjects?

Hello,

My game involves a capsule that moves 5 units on the z-axis automatically infinitely. When it runs into a wall, I want it to become inactive. However, I don’t know what kind of if-statement to use for when something translates into another.

for further clarification, I’ll show you some script excerpts.

Moving infinitely:

Vector3 moveForward = new Vector3 (0, 0, speed);
transform.Translate (moveForward * Time.deltaTime);

Using this code, it will keep moving forward infinitely because it is being translated at 5 units every time.deltaTime.

However, I still want it to run into the wall as if its colliding using phisics. There is a red wall in its path, and when it runs into the wall, I want it to become inactive:

if(GAMEOBJECT RUNS INTO WALL TAGGED "Obstacle")
{
gameobject.setActive(false);
}

However, I don’t know what to put in that if statement.

Help please?

2 Answers

2

–your question is not clear for me properly but i think you want destroy or inAactive object

put this script to your object to destroy when it collide with other object

function OnCollisionEnter (col : Collision) {
 if(col.gameObject.name == "Your object name")
  {
   Destroy(gameObject);
  }
}

function OnCollisionEnter (inactive : Collision)
{
if(inactive.gameObject.name == “wall”)///// write the tag name of the wall in the double quotation
{
gameObject.active = false;
}
}

apply the script on the capsule. on runs into the wall it will deactivate(inactive) the capsule or what ever object on which this script is applied… :slight_smile:
and if you want to destroy the object (capsule in your case) use

 Destroy(gameObject);