- Home /
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?
Answer by Quokmoon · Mar 13, 2014 at 05:47 AM
--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);
}
}
Answer by Omer.Hussain · Mar 13, 2014 at 07:05 AM
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.. :) and if you want to destroy the object (capsule in your case) use
Destroy(gameObject);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with casting a raycast C# 1 Answer
Distribute terrain in zones 3 Answers
Transform.Position Collision Issue 0 Answers
Move until collision sticks 0 Answers