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 4ndro1d · Nov 20, 2013 at 02:13 PM · animationrotationcube

Continously move an object with animation

Hi guys,

I'm just playing around with animations and now confronted a problem, when I want to move an object several times with the an animation.

I already made my play object with the animations a child of another empty GameObject. I instantiate my player objacte in code and not in editor and the first animation works fine. The player (cube) spawns on the middle field and I am able to move it one field left, down, etc.

alt text alt text

But when want to make another move the cube spawns at the center of the grid again and starts the animation from there.

I created 4 animation so the cube will rotate by 90° and also move by 1 in one of the 4 directions. Here is the code:

 if(Input.GetKeyDown("w")){        
     animation.Play("move_forward", PlayMode.StopAll);
     transform.Translate(Vector3.forward, Space.World);        
     }

When commenting the animation out, I can coninously move around the whole field. But the nice rotation is missing.

What would be the best solution for this? Can I fix the animation somehow or should I try to rotate the cube in code? (which I couldn't achieve yet).

unbenannt.png (5.9 kB)
unbenannt.png (26.7 kB)
Comment
Add comment · Show 3
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 4ndro1d · Nov 20, 2013 at 02:34 PM 0
Share

It seems I have to translate the parent object and animate the child? Is this right? I split the code 2 two scripts and applied the translation to the parent. Now i have the problem that both happens at the sime time -> translate happens first and then the animation is started. The behaviour would be right if it wouldn't skip one.

avatar image gajdot · Nov 20, 2013 at 02:44 PM 0
Share

you can reference from your script the parent object. transform.parent.transform.Translate etc. So in your example use:

  if(Input.Get$$anonymous$$eyDown("w")){
     animation.Play("move_forward", Play$$anonymous$$ode.StopAll);
     transform.parent.transform.Translate(Vector3.forward, Space.World);
  }
avatar image 4ndro1d · Nov 20, 2013 at 02:50 PM 0
Share

Okay then the result is still the same, but only with one script ;) First jump to Translate position, then starts the animation from there. I would need the translation at the end of the animation. I read something with yield, but couldn't make it work

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by gajdot · Nov 20, 2013 at 02:54 PM

K, so the animation you where doing wasn't right, because it was not looping, so it would jump when you turned around. So I went with the RotateAround solution.

I have it working, however you need your rigidbody to be isKinematic. So this is the code I ended up using:

 private bool isMoving = false;
 private int Direction = 0;
 private Vector3 rotatePosition;
 private static Vector3 originalRotation = new Vector3(90,0,0);

 void Update() {
     if(!isMoving)
     {
         if(Input.GetKeyDown("w")){
             isMoving = true; //disable input while I move
             Direction = 2;//I want to move upwards
             //Calculate where do I need to rotate the object form
             rotatePosition = transform.position;
             rotatePosition.z+=0.5f;
             rotatePosition.y-=0.5f;
         }
         if(Input.GetKeyDown("s")){        
             isMoving = true; //disable input while I move
             Direction = 1;//I want to move downwards
             //Calculate where do I need to rotate the object form
             rotatePosition = transform.position;
             rotatePosition.z-=0.5f;
             rotatePosition.y-=0.5f;
         }
         if(Input.GetKeyDown("a")){
             isMoving = true; //disable input while I move
             Direction = 3;//I want to move left
             //Calculate where do I need to rotate the object form
             rotatePosition = transform.position;
             rotatePosition.x-=0.5f;
             rotatePosition.y-=0.5f;
         }
         if(Input.GetKeyDown("d")){
             isMoving = true; //disable input while I move
             Direction = 4;//I want to move right
             //Calculate where do I need to rotate the object form
             rotatePosition = transform.position;
             rotatePosition.x+=0.5f;
             rotatePosition.y-=0.5f;
         }
     }else{ //animating
         switch(Direction)
         {
         case 1: //move upwards
             transform.RotateAround(rotatePosition, Vector3.right, - 40 * Time.deltaTime);
             break;
         case 2: // down
             transform.RotateAround(rotatePosition, Vector3.right, + 40 * Time.deltaTime);
             break;
         case 3: //left
             transform.RotateAround(rotatePosition, Vector3.forward, + 40 * Time.deltaTime);
             break;
         case 4: //right
             transform.RotateAround(rotatePosition, Vector3.forward, - 40 * Time.deltaTime);
             break;
         }
         if(transform.localEulerAngles.x>180) //when I turn 90 degree this value will become or more then 180 or less then 0 which will result in 359. so in both case it's greated then 180
         {
             isMoving = false; //disable animation and give back control for input
             transform.rotation=Quaternion.Euler(originalRotation); //set back the original rotation ( this works here because your every sight is the same, I do this to have easier if statemen when is my rotation over
             Vector3 roundedPos = new Vector3(Mathf.Round(transform.position.x),
                                              Mathf.Round(transform.position.y)+0.5f,
                                              Mathf.Round(transform.position.z)); // i round off the current position, so I would have accurate rotation even when moving far form origin
             transform.position = roundedPos; //save the rounded position back
         }
     }    
 }
Comment
Add comment · Show 17 · 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 4ndro1d · Nov 20, 2013 at 03:18 PM 0
Share
 yield WaitForAnimation( animation );

This line is not working for me. Where do I have to call is StatCoroutine stuff? In start? And how do I handle inputs then? ANd where would I cann the moveUp method from your code?

avatar image 4ndro1d · Nov 20, 2013 at 03:22 PM 0
Share

I made the event thing running. Before my unity crashed once. But now the cube just moves one field further after the animation. Still not the expected outcome :D

avatar image gajdot · Nov 20, 2013 at 03:24 PM 0
Share

You should have a code like this:

 if(Input.Get$$anonymous$$eyDown("w")){
   StartCoroutine(moveUp);
 }

isn't this workking?

avatar image 4ndro1d · Nov 20, 2013 at 03:26 PM 0
Share

Oh yeah now the code is working. But still it's moving two fields and gets resettet to center T_T I guess there is anothe rproblem I'm not thinking of :/

avatar image gajdot · Nov 20, 2013 at 04:24 PM 0
Share

$$anonymous$$ake a test project and post it please, so I could check what's the problem. I'll fix it and post it back.

Show more comments

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

18 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 avatar image avatar image avatar image

Related Questions

Animate a cube rotation with script only 3 Answers

Rotate an object such that it is theta degrees relative to another object 1 Answer

Lerping smoothly between animation and new position 2 Answers

Rotation Y axis is bugged. 2 Answers

Make the player go into the direction I am looking at 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