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
0
Question by mheyman89 · Feb 16, 2014 at 04:59 AM · rotatemathtrigonometry

Rotate object's local up towards a vector.

Hey I'm working on a 2D game and am trying to figure this out.

What Im trying to do is when an object collides with another Object A's local up (Y) axis points towards the position of Object B.

Any help with the math on this one :D?

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
4

Answer by whydoidoit · Feb 16, 2014 at 05:14 AM

You need to set the "up" to be the vector from one object to the other:

     var direction = (objectHit.transform.position - objectA.transform.position).normalized;
     objectA.transform.up = direction;

Or:

     objectA.transform.rotation = Quaternion.FromToRotation(Vector3.forward, Vector3.up) * Quaternion.LookRotation(direction);
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 mheyman89 · Feb 16, 2014 at 05:24 AM 0
Share

This worked perfectly, thank you! I used the first option you posted. Is there anyway to do this over time so it doesn't pop to that rotation? Thanks!

avatar image whydoidoit · Feb 16, 2014 at 05:28 AM 0
Share

You need to Lerp towards the direction:

Using the first method this might work:

     objectA.transform.up = Vector3.Lerp(objectA.transform.up, direction, Time.deltaTime * speedFactor);

I'd normally use the second method which would look like:

      var targetRotation = Quaternion.FromToRotation(Vector3.forward, Vector3.up) * Quaternion.LookRotation(direction);
      transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * speedFactor);
avatar image nesis · Feb 16, 2014 at 05:40 AM 0
Share

$$anonymous$$ight also be worth trying the Slerp method, too.

avatar image whydoidoit · Feb 16, 2014 at 05:43 AM 0
Share

In this case it would be hard to quantify the difference since it's using the "fudgy" smoothing of a Lerp towards a value using the time step - if you were to ensure that the smooth movement happened over a fixed period of time (perhaps by using a coroutine) then Slerp would provide an easing effect rather than a hard start stop.

avatar image BeforeTheLight · Sep 27, 2014 at 01:49 PM 0
Share

Thank you for this. I have been using NGUI for years and have often wanted to point a UI element not along the z axis at something in the scene and this works great for that. Here is some code to have an arrow follow the edge of the screen and point at a target using UGUI.

 public Transform target;
         public Camera mainCamera;
         public Transform ship;
         Image arrow;
         
         void Start () {
             arrow = GetComponent<Image> ();
         }
     
         void Update () {
 //Check used to see if item is on screen
             Vector3 screenPos = mainCamera.WorldToViewportPoint (target.position);
 //Get Vector to target from ship
             Vector3 distance = ship.position - target.position;
 //Get left, right, up and down variance and use UI width and height to deter$$anonymous$$e position
             float leftRight = Vector3.Dot (distance.normalized, target.right);
             float upDown = Vector3.Dot (distance.normalized, target.up);
             leftRight = $$anonymous$$athf.Clamp(-leftRight*480, -375, 375);
             upDown = $$anonymous$$athf.Clamp(-upDown*800,-88, 215);
 //Assign position
             transform.localPosition = new Vector3 (leftRight ,upDown,transform.localPosition.z);
 //Look at target using the right arrow.  Invert the normalized vector because the arrow moves //around the screen area.
                 transform.right = -distance.normalized;
 
 //If target is on screen show it if now hide it
             if (screenPos.x > 0 && screenPos.x < 1 &&
                 screenPos.y > 0.2f && screenPos.y < 1) {
                 arrow.enabled = false;
             } else {
                 arrow.enabled = true;
             }
     
         }

 

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

20 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

Related Questions

clamp rotation by mouse direction 0 Answers

Define circle using 3 points 0 Answers

Detect GUI rotation degree 1 Answer

How can I launch a ball given an angle in degrees and a magnitude? 2 Answers

Rotating camera around the sphere 2 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