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
1
Question by stardust · Nov 16, 2013 at 10:38 AM · rotationaxisrotate object

Rotating an object based on the side the player is looking at

I would like to script this behavior: The player should be able to rotate a cube 90 degrees to the left or up (depending on his input), based on the side the player is looking at. In other words in the players point of view the cube should rotate to the left or up.

So the problem with this is, that based on the rotation the cube already has and the side the player approaches the cube, the axis around which the cube has to rotate changes. How can I identify the axes around which the script should rotate the cube?

Fancy Drawing

rotation.png (286.3 kB)
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 stardust · Nov 20, 2013 at 11:14 AM

After I got Vector3.Cross and RaycastHit.normal explained to me by my prof I could figure out the solution by myself. So with this script the player can rotate an object to the left and up, based on how the player is looking at the object. From the players point of view the rotation is always to the left and up. This script is placed on the player.

 using UnityEngine;
 using System.Collections;
 
 public class RotateTarget : MonoBehaviour {
     
     public string rotateLeftButton;
     public string rotateUpButton;
     
     //Rotate the given object to the left
     //For this I use the gravity's vector, because the player is always affected by gravity in my game
     void RotateCubeLeft(Transform rayHit) {
         rayHit.Rotate(Physics.gravity.normalized*(-90), Space.World);
     }
     
     //Rotate the given object up
     //With Vector3.Cross we get the third axis from the gravity and the normal the player is pointing at
     void RotateCubeUp(Transform rayHit, Vector3 hitNormal) {
         Vector3 rotationVec= Vector3.Cross(Physics.gravity, hitNormal).normalized;
         rayHit.Rotate(rotationVec*90, Space.World);
     }
     
     // Update is called once per frame
     void Update () {
                         
         RaycastHit hit;
         // Raycast detects if the player is close enough to the object to be rotated
         if(Physics.Raycast(transform.position,transform.forward, out hit, 5)) {
             Debug.DrawLine(transform.position, hit.point);
             
             //Rotate the hit object to the left, based on the side(normal) the player is pointing at
             //I only want this to work with objects tagged with "Cube"
             if(Input.GetButtonDown(rotateLeftButton)) {
                 if(hit.transform.tag=="Cube") {
                     RotateCubeLeft(hit.transform);
                 }
             }
             
             //Rotate the hit object up, based on the side(normal) the player is pointing at
             //I only want this to work with objects tagged with "Cube"
             if(Input.GetButtonDown (rotateUpButton)) {
                 if(hit.transform.tag=="Cube") {
                     RotateCubeUp(hit.transform, hit.normal);
                 }
             }
         }
     
     }
 }
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
avatar image
0

Answer by robertbu · Nov 16, 2013 at 04:11 PM

You can rotate in the global rather than the local space. Using Transform.Rotate(), you can add the 'Space.World' parameter for an instant rotation:

  transform.Rotate(Vector3.up -90.0, Space.World);

Over time, it is easier to get an accurate rotation using Quaternions. At the start you calculate the rotation:

 var dest = Quaternion.AngleAxis(Vector3.up, -90.0) * transform.rotation;

Then in Update():

 transform.rotation = Quaternion.RotateTowards(transform.rotation, dest, speed * Time.deltaTime);


Comment
Add comment · Show 3 · 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 stardust · Nov 16, 2013 at 06:14 PM 0
Share

I added a fancy picture to illustrate the problem. I already used Space.World, and as you can see this solves the problem of the previously rotated cube (the global axes in every row are the same).

But there is still the problem, that if the player looks at the cube from another side, the local and the global axes around which the cube should be rotated change. Correct me if I'm wrong...

I also thought about comparing the cubes and the players position, but i think that wouldn't work either...

avatar image robertbu · Nov 16, 2013 at 07:50 PM 0
Share

I'll give the problem more of a look later when I have more time, but I'm puzzled about the goal here. Is the Left or Up rotation always be relative to how the character is viewing the cube? If so, then the axis of rotation will be either be the character's up or the character's right depending on which way you are doing the rotation. Ins$$anonymous$$d of 'Vector3.up' in the 'dest' calculation, you would use something like 'character.up'.

avatar image stardust · Nov 20, 2013 at 11:23 AM 0
Share

Yes exactly, it should always be relative to how the character is viewing the cube, because it may be very confusing for the player in a first person view if it wouldn't always behave the same from his point of view.

You can't use character.left because the player could stand somewhere and still point at the object while his angle would indicate something else (for example if he is standing very close to the object and looking along its side).

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

17 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

Related Questions

Smooth rotation about global axis instead of local axis. 1 Answer

rotate object around another object 1:1 1 Answer

2D Rotate object so y axis faces other object 1 Answer

Rolling a capsule lengthways, sideways and twisting with AddTorque 1 Answer

Rotate object based on another rotation above a certain threshhold 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