How to rotate one cube from another cube?
I am completely new to unity as well as java script. Maybe my question makes you laugh but I want your help. I have 4 cubes and each cube has its own script file. I am using java script. I want to control rotation of cube1 from cube3 and vice versa. and also have to add add force in it. how can I do that? Thanks in advance!
create a public variable for a gameobject in the script and assign each I'd the two cubes the other one. in update, check their rotation and save the others current rotation. if the rotation differs from the saved one, rotate and save again.
for AddForce, I don't know what you mean. you already know the method name so with a rigidbody on the respective cube you can just call it.
Answer by Nirvana33 · Sep 12, 2015 at 09:12 PM
I'm not really sure if it works properly. I don't really know javascript. But it will help you.
First you need to create variables for cubes.
public var cube1 : GameObject;
public var cube2 : GameObject;
public var cube3 : GameObject;
you'll assign the cubes from inspector panel. Don't forget.
To rotate you can simply use cube1.transform.Rotate(Vector3.right * Time.deltaTime); this will slowly rotate
cube1.transform.rotation= Quaternion(90,0,0,0);
this will set the exact rotatiton of cube1(that you attached on inspector)
To use addForce
cube1.GetComponent(Rigidbody).AddForce(transform.forward*300);
Thank you very much for replying.. . with that I also want to rotate cube1 from the script file of cube3. That means if I want to rotate cube1 I have to call the function that I set in cube3.js file. How can I do that?