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 helppls123 · Jan 14, 2012 at 08:24 AM · rotationvector3coroutineslerp

Activating 3 rotations based on multiple presses on one key

Hi,

I am trying to create a sword swing with just built-in unity objects.

I have to make it so that when you press the action button once, it will rotate in one direction then press it again and rotates another direction and so on...

I've tried using Slerp and a timer which will cut off any button presses made but there is still a delay with each button press. I'm trying to do a 90 degree turn on the right direction and then a 90 degree turn on the forward direction and then a -90 degree turn on the right direction. However, it doesn't seem to turn in a complete 90 degree direction and it goes all over the place.

Is there a way to get the input of each button press separately and get a clean and complete animation? Thanks!

Comment
Add comment · Show 2
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 roamcel · Jan 14, 2012 at 08:45 AM 0
Share

Sorry but your question is unclear.

avatar image helppls123 · Jan 14, 2012 at 04:35 PM 0
Share

Basically, I want to know how to do a rotation of an object when a button is pressed and then when the same button is pressed again, it will do a completely different rotation without interfering with the previous rotation.

2 Replies

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

Answer by doomprodigy · Jan 15, 2012 at 09:11 AM

I suggest using an int.

private int swingNum = 0; private bool swinging = false; public float swingTime = 2.0f;

void Update (){ if (Input.GetButtonDown("whatever")){ if (swinging == false){ if (swingNum == 0){ StartCoroutine("SwingOne"); swingNum += 1; } if (swingNum == 1){ StartCoroutine("SwingTwo"); //from here onwards you would either add another digit to swingNum and put another if statement asking if it is swingNum 2 or you would reset swing num to 0 again Rinse and Repeat sort of deal` } } }

IEnumerator SwingOne () { //do your rotation here swinging = true; yield return new WaitForSeconds(swingTime); // return back to original position via your rotation or leave it where it has rotated to swinging = false; } //Repeat the IEnumerator function with the different name and with different rotations etc

This is exactly what you want, you just need to fix the syntax and insert the rotations and swings.

Peace,

EDIT: You can also add a timer to how long it will wait until it reverts back to swing one if you don't click for ages while in between the second and third swing for instance.

Comment
Add comment · Show 2 · 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 helppls123 · Jan 15, 2012 at 03:06 PM 0
Share

Thanks that actually worked :)

avatar image doomprodigy · Jan 16, 2012 at 02:36 AM 0
Share

No Problem :)

avatar image
0

Answer by roamcel · Jan 15, 2012 at 06:51 AM

What you're trying to do revolves around what's called "Coroutines".

For a start you need a button state manager, a simple script that cycles a variable from 0 to 2 (A Vector3 is a zero based array with three elements from index 0 for x, 1 for y and 2 for z) on a same key press (probably best done with OnButtonDown).

After that, you need a coroutine (mock code) to be called inside Update

 void Update () {
   //... other code
 
   if (Input.OnButtonUp(mysamekey)) {
     StartCoRoutine(rotateroutine(mycurrentaxis, 100));
   }
 }
 
 rotateroutine (axis, angles) {
 for 1 to angles {
 Vector3 newrotation;
 newrotation[axis] = angles
 transform.eulerAngles = newrotation;
 }
 }

Code certainly won't compile, but should help you get on the right track.

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

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

6 People are following this question.

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

Related Questions

Moving the player toward a direction 1 Answer

Slerp Finishes Rotation When Progress is 0.5f 1 Answer

LookRotation Vector3 is Zero, Yet Slerp Still Rotates? 2 Answers

What am I doing wrong with these rotations? 0 Answers

Issue with Slerping 1 Answer


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