Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Michel Schnetler · May 20, 2013 at 03:11 PM · transformrotatecutscene

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Michel Schnetler · May 20, 2013 at 03:47 PM 0
Share

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

avatar image
0

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
 } 
 

 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 21, 2013 at 02:34 AM 0
Share

@bigbat - you don't want to yield inside Update().

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges