Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SecretAgentMango · Jan 02, 2018 at 10:06 PM · rotation2d gamequaternionlerp

Sword rotation not working?

I wrote a script where I want my sword to essentially glide 90 degrees downwards and then glide back up to its original position. My script is not working and I'm not sure why. Here it is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SwordMovement : MonoBehaviour 
 {
 
     private Vector3 newRotation;
     private Vector3 originalRotation;
     private bool rotating = false;
 
     // Use this for initialization
     void Start () 
     {
         newRotation = new Vector3(0, 0, -135);
         originalRotation = new Vector3(0, 0, -45);
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyDown(KeyCode.R)) 
         {
             rotating = true;
         }
         if (rotating) 
         {
             RotationChange ();
             rotating = false;
         }
     }
 
     void RotationChange()
     {
         transform.rotation = Quaternion.Euler(Vector3.Lerp (originalRotation, newRotation, Time.deltaTime));
         transform.rotation = Quaternion.Euler(Vector3.Lerp (newRotation, originalRotation, Time.deltaTime));
         Debug.Log ("The sword is moving");
     }
 }

All this does is cause my sword to rapidly snap to the bottom position and then do nothing whenever I press "R" again. I tested my two lines under the method RotationChange() individually; the first one does nothing and the second one causes the "snap" (which didn't make sense to me, since I thought the second one would bring the sword back up to its original position). Am I improperly using "quaternion" or "loop"?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OneCept-Games · Jan 02, 2018 at 10:15 PM

You should wait until your sword has reached newRotation, and then you can Lerp it back to originalRotation.

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 SecretAgentMango · Jan 02, 2018 at 10:34 PM 0
Share

What do you mean by wait? I thought that the three arguments for "Vector3.Lerp" would move the sword from "originalRotation" to "newRotation" at the rate of "Time.deltaTime", i.e. waiting would already be accounted for; if waiting is not accounted for, what is the third argument (Time.deltaTime) actually doing? How do I go about identifying the exact moment that the sword reaches "newRotation"?

avatar image OneCept-Games SecretAgentMango · Jan 02, 2018 at 11:50 PM 1
Share

The 3rd argument is the time in seconds the rotation will use to go from arg1 to arg2, but you start a new Lerp right after, so the first Lerp will never finish. So start the first Lerp, wait for it to reach it's arg2 rotation, and then you can start the second one.

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

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

Related Questions

Controlling the direction of a rotation 2 Answers

Issues with Lerps 2 Answers

Make Quaternion Slerp happen over a 1 second period 3 Answers

Quaternion.Lerp shaky while in coroutine? 1 Answer

how do i use Quaternion lerp on only the Y and Z axis 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