Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 LoryColo · Nov 15, 2017 at 07:29 PM · unity 5raycastlerpcubequaternions

Need help rotating the inside of a cube correctly (lerp gets me mid-way there)

Hi guys, i'm here asking for help, i am using some code to rotate a cube that the player sees from the inside, if the player presses CTRL and W,A,S,D he will rotate the cube in that direction, the rotation works but i had to Hard-Code the rotation variables in my script and that causes a strange rotation(sometimes the cube rotates to the side and then forward):

Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class TestRotation : MonoBehaviour
 {
     public Vector3 targetAngle;
     public float speed;
     private Vector3 currentAngle;
     float time;
     Vector3 direction;
     public GameObject MainCamera;
     bool coolDown;
     string Tag;
     public Image ctrl;
     public void Start()
     {
         currentAngle = transform.eulerAngles;
         direction = Vector3.zero;
         coolDown = true;
     }
 
     public void Update()
     {
 
         ctrl.enabled = false;
 
         if (Input.GetKey(KeyCode.LeftControl))
         {
 
             ctrl.enabled = true;
             
             if (Input.GetKey(KeyCode.W) && coolDown)
             {
                 direction = MainCamera.transform.forward;
             }
            else if (Input.GetKey(KeyCode.A) && coolDown)
             {
                 direction = -MainCamera.transform.right;
             }
            else if (Input.GetKey(KeyCode.S) && coolDown)
             {
                 direction = -MainCamera.transform.forward;
             }
            else if (Input.GetKey(KeyCode.D) && coolDown)
             {
                 direction = MainCamera.transform.right;
             }
             RaycastHit hit;
             Tag = "/";
             if(Physics.Raycast(MainCamera.transform.position, direction, out hit) && direction != Vector3.zero && coolDown)
             {
                 Tag = hit.transform.parent.parent.tag;
                 direction = Vector3.zero;
                 coolDown = false;
                 StartCoroutine(cooldown());
             }
 
             if(Tag != "/")
             {
                 switch (Tag)
                 {
 
                     case "Bottom":
                         targetAngle = new Vector3(0, 0, 0);
                         time = 0;
                         break;
                     case "Forward":
                         targetAngle = new Vector3(90, 0, 0);
                         time = 0;
                         break;
                     case "Backward":
                         targetAngle = new Vector3(-90, 0, 0);
                         time = 0;
                         break;
                     case "Right":
                         targetAngle = new Vector3(0, 0, -90);
                         time = 0;
                         break;
                     case "Top":
                         targetAngle = new Vector3(180, 0, 0);
                         time = 0;
                         break;
                     case "Left":
                         targetAngle = new Vector3(0, 0, 90);
                         time = 0;
                         break;
                 }
             }
           
             Debug.Log(Tag);
             Tag = "/";
         }   
         if (time > 1)
         {
             currentAngle = targetAngle;
         }
         Rotate();
     }
 
     void Rotate()
     {
         time += Time.deltaTime;
         transform.rotation = Quaternion.Euler(Vector3.Lerp(currentAngle, targetAngle, time));
         
     }
 
     IEnumerator cooldown()
     {
         yield return new WaitForSeconds(1f);
         coolDown = true;
     }
 }

Note that i want the face that was hit by the raycast to be under my player(which is in the middle of the cube). The face that needs to be under the player is always correct, i just don't like the way the cube rotates and am wondering if there is something like a mathematical law to apply to get the right rotation without the need of hard-coding quaternions.

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
1
Best Answer

Answer by ryanmillerca · Nov 16, 2017 at 12:24 AM

The issue you're having is a result of the rotations and Lerp being calculated on a Vector instead of a Quaternion. You can calculate your bookmarked rotation angles as Vector3s and store them as Quaternions with Quaternion.Euler(), then use Quaternion.Lerp so that the rotations tween properly.

So, instead of

 Vector3 targetAngle = new Vector3(0, 0, 90);

do

 Quaternion targetAngle = Quaternion.Euler(0, 0, 90);

and instead of

 transform.rotation = Quaternion.Euler(Vector3.Lerp(currentAngle, targetAngle, time));

do

 transform.rotation = Quaternion.Lerp(currentAngle, targetAngle, time);

Rotations won't get messed up if they stay Quaternions!

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 LoryColo · Nov 16, 2017 at 02:08 PM 0
Share

Thank's most of the issues were fixed, by the way, is it strange that Lerp and Slerp produce the same exact result?

avatar image
1

Answer by Legend_Bacon · Nov 16, 2017 at 11:11 AM

Hello there,

Ryan's answer is right, although in some cases I would recommend Quaternion.Slerp. If your rotations are far apart, Slerp usually looks better. For more information, click here.

Hope that helps!

~LegendBacon

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

172 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 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

Object detect my light RayCast and activating Lerp 0 Answers

c# Raycast going off at odd angles. Unity 5 2 Answers

How i can create a cube with specific coordenates? 3 Answers

Problem rotating a cube with quaternions using lerp 1 Answer

RaycastHit.normal 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