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 /
  • Help Room /
avatar image
1
Question by Joyble · Aug 23, 2016 at 10:45 PM · rotationscripting problemquaternion

Quaternion snaps back to original position

I'm trying to make an 8-directional movement for my character and I've somewhat succeeded. My problem is that when I press forward button, my character is looking in the direction in which the camera is pointing(as intended), BUT whenever I release the button, it just snaps back to the original position.

Here's the code

     private Vector3 inputDir = Vector3.zero;
     private float lockRotation = 0.0f;
 
 
     [SerializeField]
     private CharacterController controller;
     [SerializeField]
     private GameObject gameCam;
     [SerializeField]
     private Transform transCam;
 
 
     void Start()
     {
         transCam = GameObject.FindGameObjectWithTag ("MainCamera").transform;
         gameCam = GameObject.FindGameObjectWithTag ("MainCamera");
         controller = GetComponent<CharacterController>();
 
     }
 
     void Update()
     {
 
         Vector3 inputDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         Vector3 v = inputDir.normalized;
         v.x = Mathf.Round(v.x);
         v.z = Mathf.Round(v.z);
         if (v.sqrMagnitude > 0.1f)
             inputDir = v.normalized;
 
 
         inputDir = Camera.main.transform.rotation * inputDir;
 
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), Time.time*5f);
 
     }
         
 
     void LateUpdate()
 
     {
 
         controller.transform.rotation = Quaternion.Euler(lockRotation, transform.rotation.eulerAngles.y, lockRotation);
 
     }
 
 }
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

2 Replies

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

Answer by oStaiko · Aug 24, 2016 at 12:14 AM

Problem

if (v.sqrMagnitude > 0.1f) { Slerp }

Therefore,

if (v.sqrMagnitude < 0.1f) { Not Slerpin } //!!!!

When no keys are pressed, you aint slerpin. Or maybe it's just resetting you to Quaternion.identity somewhere else in your script. You didn't post anything like that so idk.

What I tried:

 Update()
 {
     if ( you wanna slerp) //and you do
     {
         doSlerp(newDir);
     }
 }

 doSlerp()
 {
     //Slerp here
 }

Now when you're no longer pressing keys, it will continue to slerp. At least in my own test it did. If it doesn't fix yours, I'll come up with something else for ya.

EDIT:

Per request, here's a more elaborate explanation. I'm just gonna show you the scripts I used in an old project that I had used slerp to rotate. I never encountered your problem of snapping back, so idk what's causing it.

 void FlyTo (float angle)
 {
     Quaternion newDir = Quaternion.identity;
     newDir.eulerAngles = new Vector3(0,angle,0);
     transform.rotation = Quaternion.Slerp (transform.rotation, newDir, Time.time * turnSpeed/3000);
 }

That script was great for what I needed. You throw in an angle, and it looks at it! Easy enough.
Here's how I called it, which is an 8 direction system you are using.

 Update()
 {
     float hor = (Input.GetAxisRaw ("Horizontal"));
     float ver = (Input.GetAxisRaw ("Vertical"));

     if (hor == 0 && ver == 0) {
         return;
     }
     else if (hor == 1 && ver == 0)
     {
         FlyTo (90);
     }
     else if (hor == 1 && ver == 1)
     {
         FlyTo (45);
     }
     else if (hor == 0 && ver == 1)
     {
         FlyTo (0);
     }
     else if (hor == -1 && ver == 1)
     {
         FlyTo (315);
     }
     else if (hor == -1 && ver == 0)
     {
         FlyTo (270);
     }
     else if (hor == -1 && ver == -1)
     {
         FlyTo (225);
     }
     else if (hor == 0 && ver == -1)
     {
         FlyTo (180);
     }
     else if (hor == 1 && ver == -1)
     {
         FlyTo (135);
     }
     else
     {
         Debug.Log("Movement Error");
     }
 }

Now, that's a horrible way to call the function, it's an old project but I know better now. Do what fits your project best.

Comment
Add comment · Show 5 · 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 Joyble · Aug 24, 2016 at 12:30 AM 0
Share

Could you please elaborate on that a bit more? I'm fairly new to this and it seems like I should've started with something simpler.

avatar image oStaiko Joyble · Aug 24, 2016 at 12:52 AM 0
Share

Your question is fine, but why did you include Start? What is Controller and why do you need to rotate it

avatar image Joyble oStaiko · Aug 24, 2016 at 01:18 AM 0
Share

Controller is just used to freeze my y axis? I'm using orbiting camera on my character and it flips my character around without it

Show more comments
avatar image
0

Answer by Joyble · Aug 24, 2016 at 02:21 PM

FIXED!

replacing

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), Time.time*5f);

with

if ( inputDir != Vector3.zero ) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDir), 0.5f);

Keeps the direction!

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

89 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

Related Questions

AI targeting player in complete wrong direction 1 Answer

How do I rotate the GameObejcts smoothly with negative degrees 0 Answers

Rotation script isn't working 1 Answer

Getting a 2D object to face the direction of its velocity relative to other objects in the level 0 Answers

How do I smoothly rotate the GameObejcts with negative 90 degrees 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