How to rotate cube2 if the script is in cube1?
What i want to do is to rotate the cube2 when the cube1 collides with it, i know i could put the script on the cube2 and test if they are colliding, but with all the thigns i will have it won't be possible to do that, and i want to know how i would do that, cuz in my point of view it's a important thing to know. Basically i have this javascript script on cube1:
function OnCollisionEnter(col : Collision){
if(col.gameObject.name=="cube2"){
Debug.Log("Hit Something");
}
}
Probably i'll use transform.Rotate, but i need to tell the program that is the cube2 that needs to rotate and not the cube1. Thanks!!
Answer by jgodfrey · Jun 07, 2016 at 10:16 PM
You have a direct reference to the game object you've collided with in the event handler. In fact, the code you've posted is already referencing it.
You can access the transform of the "cube2" object like this:
function OnCollisionEnter(col : Collision)
{
if(col.gameObject.name == "cube2")
{
col.transform.Rotate(someNewVector3);
}
}
If your question is answered, please "Accept" the answer.
Your answer
Follow this Question
Related Questions
Make GameObject rotate around another around random axis but with fixed distance 0 Answers
constant rotation using keypress 0 Answers
ROTATION STOPS when i move the char on the air! 0 Answers
Ask some problem about rotation, quaternion and angle 0 Answers
أنجازات غير مسبوقة؟؟!!توكيل كاريير +بالاسكندرية 01286629661 0 Answers