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 Vladislav-Videnov · May 28, 2019 at 08:02 AM · transformlookatrotation axisorientationlookattarget

How to make an object "LookAt" a target rotating around, but keep its current orientation

Hello guys !

I am working on a VR project and i want to use LookAt so that i can rotate objects intuitively. I've tried multiple ways and the best one is with LookAt.

The issue is that when i start rotating the object "snaps" to the new target.

I am rotating the object on only the Y axis and that is my goal.

How can i keep its Y axis orientation and still make it "LookAt" (not from its forward) at the target and follow it ?

Example of problem :

https://drive.google.com/file/d/1Pd3rt78RzsnU_Cq3jM-nGM9Bw2y-yExe/view?usp=sharing

here is the current code ( i use the ray-cast hit point on the ground as a target point)

 RaycastHit rotatingHit;
 Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
 
 Vector3 targetPosition = new Vector3(rotatingHit.point.x ,
                                        ObjectToEdit.transform.position.y,
                                        rotatingHit.point.z);
 
 ObjectToEdit.transform.LookAt(targetPosition);


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 Vladislav-Videnov · May 28, 2019 at 09:38 AM

I figured it out. My problem was that i didnt understand both points are changing with rotation ( targetRotation and the objectRotation)

So what i did is :

OnGrab -> Store ObjectInitialRot and TargetRotationInitial

   initialObjectRot = ObjectToEdit.transform.rotation;
             RaycastHit rotatingHit;
             Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
             Vector3 targetPos = rotatingHit.point - ObjectToEdit.transform.position;
             targetPos.y = 0;
             initialNewRot = Quaternion.LookRotation(targetPos, Vector3.up);

OnUpdate ->

     RaycastHit rotatingHit;
             Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
 
             
             Vector3 targetPos = rotatingHit.point - ObjectToEdit.transform.position;
             targetPos.y = 0;
 
             Quaternion delta = Quaternion.Inverse(initialNewRot) * Quaternion.LookRotation(targetPos, Vector3.up);
 
             ObjectToEdit.transform.rotation = initialObjectRot * delta;

Result:

https://drive.google.com/file/d/18483_FOKwVc6g3IB0HejFVTyAKhbFXSN/view?usp=sharing @highpockets

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 highpockets · May 28, 2019 at 08:51 AM

This should work, but it is untested, let me know if something isn't right. When the mouse button gets pressed, we get the angle difference and then we rotate to the target and multiply the rotation on the y axis by the difference.

 float angleDif;
 
  if (Input.GetMouseButtonDown(0))
 {
 angleDif = Vector3.SignedAngle(ObjectToEdit.transform.forward, targetPosition - ObjectToEdit.transform.position);// this gets the angle difference the moment the mouse clicks on the position
 }
 
     RaycastHit rotatingHit;
      Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayerMask);
      
      Vector3 targetPosition = new Vector3(rotatingHit.point.x ,
                                             rotatingHit.point.y,
                                             rotatingHit.point.z);
 
 
 ObjectToEdit.transform.rotation = Quaternion.Euler(ObjectToEdit.transform.rotation.eulerAngles.x, ObjectToEdit.transform.rotation.eulerAngles.y + angleDif, ObjectToEdit.transform.rotation.eulerAngles.z ) * Quaternion.LookRotation(targetPosition - ObjectToEdit.transform.position, ObjectToEdit.transform.Up);







Comment
Add comment · Show 7 · 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 Vladislav-Videnov · May 28, 2019 at 08:58 AM 0
Share

Sorry, maybe i didnt write it clear enough.

I want the object to keep its initial Rotation ( so i do not want it to smooth / snap to the look at position )

avatar image highpockets Vladislav-Videnov · May 28, 2019 at 08:59 AM 0
Share

So what do you need to change? if nothing rotates, why are you using LookAt()?

avatar image Vladislav-Videnov highpockets · May 28, 2019 at 09:07 AM 0
Share

https://drive.google.com/file/d/1Pd3rt78RzsnU_Cq3j$$anonymous$$-nG$$anonymous$$9Bw2y-yExe/view?usp=sharing

Link to a gif showing what the problem is.

I want the object not to snap, but stay at the rotation it is currently, and rotate based on where i move the mouse like shown on the gif.

Show more comments
avatar image Vladislav-Videnov · May 28, 2019 at 09:00 AM 0
Share

I've just tried some other approach.

    RaycastHit rotatingHit;
             Physics.Raycast(transform.position, transform.forward, out rotatingHit, rayDistance, GroundLayer$$anonymous$$ask);
 
             
             Vector3 targetPos = rotatingHit.point - ObjectToEdit.transform.position;
             targetPos.y = 0;
             Quaternion rotation =  Quaternion.LookRotation(targetPos, Vector3.up);
             ObjectToEdit.transform.rotation = initialObjectRot * rotation;

But that doesnt keep the initial rotation for some reason :/ (initialObjectRot is captured at the start of rotation )

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

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

Having a plane at exactly the far clip of a camera 1 Answer

How to rotate towards a target but only on the y axis ? 1 Answer

How to make AI look at player 0 Answers

Lock rotation axis? 4 Answers

LookAt Problem 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