- Home /
Moving objects in game using .transform does not stop
Hi
I'm new to unity and this is probably a very easy question but I'm trying to make an object rotate in Unity using script instead of Animation. what I'm trying to do is this.
NOTE: like I said I'm new to Unity and the 3D world.
/////////////////////////////////////////////////////////////////////////////////////////
var Rotate //Outside the script
function OnMouseDown(){ //has player clicked the button?
Rotate = 1; //set rotate to (1)
}
Function Update(){
if (Rotate == 1){ //check if Rotate is set to 1
RotateArm() //Start function RotateArm
}
else
{
}
}
function RotateArm(){
GameObject.Find("ArmLower").transform.Rotate(0,0,Time.deltaTime * -6); //move the lower arm //Rotate the arm
yield WaitForSeconds (5) //wait for five seconds(sometimes this works sometimes it doesn't, still trying to figure out.
GameObject.Find("Sparks").particleEmitter.emit = true; //start the sparks emitter
//this is where I can't come right, I can't seem to stop the arm from rotating. I have tried the following
GameObject.Find("ArmLower").transform.Rotate(0,0,0) //still keeps spinning
Destroy(this) //yet this method works, I still want to add more to this script and don't want it destroyed yet.
}
////////////////////////////////////////////////////////////////////////////////////////
I would really appreciate it if someone can help me. I really enjoy using Unity but I'm a slow learner
Also Forgive me if I some of my code is missing capitals. Do not be concerned by it
Answer by bfowle · May 20, 2013 at 03:26 PM
In your if-check:
if (Rotate = 1){
A single =
here means you are assigning Rotate to 1
. You would want a double ==
to check if this value is set to 1
.
Sidenote-- Doing a GameObject.Find
check every Update()
will be quite expensive, you will want to store these GameObject
in the Awake()
function and call them by this variable throughout your other functions.
ok, thanks.
sorry I normally write == when checking that value. Ill edit that just so that nobody get's confused by it. Also your profile picture is awesome
Answer by robertbu · May 20, 2013 at 03:50 PM
There are a number of issues here, some are a bit hard to describe.
You apologize for a missing capitals. The convention used is that variable names are to start with a lower case letter. Types and functions start with an upper case letter. The compiler does not care, but the folks reading your code will have a much easier time if you follow this convention. So 'Rotate' is the only place I see where you did not follow this convention.
GameObject.Find() is an expensive operation. You should avoid calling it each frame. You can store the result to a variable in Start() and use it later.
Destory(this) destory's the script. If select the ArmLower object and watch it in the inspector, you will see the component disappear. I don't think this is what you want.
Your real issue with this code is the 'yield'. It is hard to describe, but since you are calling "RotateArm" every frame, you have many "threads" piling up. If you are going to use a Yield, then you need to work with the method as a coroutine rather than somethign you call every frame. Here are two different ways to implement your functionality:
pragma strict
private var rotate = false; private var goArm : GameObject;
function Start() { goArm = GameObject.Find("ArmLower"); }
function OnMouseDown(){ //has player clicked the button? rotate = true; //set rotate to (true) Invoke("StopRotation", 5.0); }
function StopRotation() { rotate = false; GameObject.Find("Sparks").particleEmitter.emit = true; //start the sparks emitter }
function Update(){ if (rotate){ //check if Rotate is set to 1 goArm.transform.Rotate(0,0,Time.deltaTime * -6); //move the lower arm //Rotate the arm } }
And a different way using the 'yield':
#pragma strict
private var rotate = false;
private var goArm : GameObject;
function Start() {
goArm = GameObject.Find("ArmLower");
}
function OnMouseDown(){ //has player clicked the button?
RotateArm();
}
function RotateArm(){
var timeStamp = Time.time+5.0;
while (timeStamp >= Time.time) {
goArm.transform.Rotate(0,0,Time.deltaTime * -6);
yield 0;
}
GameObject.Find("Sparks").particleEmitter.emit = true; //start the sparks emitter
}
Answer by bigbat · May 20, 2013 at 07:20 PM
I think main problem in your code is in below code:
function OnMouseDown(){ //has player clicked the button? Rotate = 1; //set rotate to (1) }
thus you set rotate var to 1 when click button and in update function :
if (Rotate == 1){ //check if Rotate is set to 1 RotateArm() //Start function RotateArm }
allways rotate will be 1 and function RotatArm will run, therefore your "ArmLower" never stop rotating. use a code like this:
var rotate:bool=false;
function OnMouseDown () {
rotate=!rotate;
}
function update()
{
if(rotate)
{
//call your rotating function here
yield waitforsecond(5);
//call your sparking function here
rotate=!rotate;
}
}
hope this help.
Your answer
Follow this Question
Related Questions
rotare arrow object to mouse position 0 Answers
rot not working for 2nd prefab 2 Answers
object rotates when moving backward 1 Answer
i want an object rotate 45 degrees more than an other one 1 Answer
Avatar Rotation Control [UnityScript] 0 Answers