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 Leigh Yuill · Feb 28, 2013 at 03:01 AM · vector3lookat

Creating an offset for the LookAt() function

Hi,

I need to use the LookAt() function but with an offset of one of its child GameObjects position. So the ChildObject is lined up between the Target and the Main Object like in the image. Then the main object will LookAt() the target in this position instead of along the Z axis.

alt text

I can't put the Main GameObject in an empty GameObject as it is going to be used in a small IK script that I'm building for a Uni project, and the extra GameObjects messes with the character structure.

The method I'm trying at the moment is to get the angles along the 3 different axes, then applying that rotation to the main object after the LookAt() function is called. This then caused issues with the X & Z rotation which I tried to balance out in relation to the Y axes.

alt text

 using UnityEngine;
 using System.Collections;
 
 public class LookAt_Via : MonoBehaviour {
 
     private Transform Arm = null;
     private Transform Forearm = null;
     private Transform Target = null;
     private Transform lookZ = null;
     private Transform lookX = null;
     
     private float x;
     private float y;
     private float z;
     
     private float Xx;
     private float Xz;
     private float Zz;
     private float Zx;
     
     void Start () {
         Target = GameObject.Find ("Target").transform;
         Arm = GameObject.Find("Arm").transform;
         Forearm = GameObject.Find("Forearm").transform;
         NewLook = GameObject.Find ("lookZ").transform;
         lookX = GameObject.Find ("lookX").transform;
 
     }
     
     void Update () {
         LookAtOffset(Arm, Forearm, Target);
     }
     
     void LateUpdate(){}
 
     private void LookAtOffset(Transform MainObj, Transform OffsetObj, Transform TargetObj){
         
         /* Set up internal vars */
         var vectorOffset = Vector3.zero;
         float a;
         float b;
         float c;
         float h;
         float yRatio;
         
         /* Set the position of points of refrence, used when working out the angles */
         lookZ.localPosition = new Vector3(0,0,5);
         lookX.localPosition = new Vector3(5,0,0);
         
         /* Getting the X axis offset angle */
         if(OffsetObj.localPosition.z>0){
             a = Vector3.Distance(MainObj.position, MainObj.position + new Vector3(0,0,OffsetObj.localPosition.z));
             h = Vector3.Distance(MainObj.position, new Vector3(0,OffsetObj.localPosition.y,OffsetObj.localPosition.z));
             vectorOffset.x = (OffsetObj.localPosition.y<0)? (Mathf.Acos(a/h)*Mathf.Rad2Deg)*-1 : Mathf.Acos(a/h)*Mathf.Rad2Deg;
         }
         else{            
             a = Vector3.Distance(OffsetObj.position, lookZ.position);
             b = Vector3.Distance(MainObj.position, lookZ.position);
             c = Vector3.Distance(MainObj.position, OffsetObj.position);
             vectorOffset.x = (OffsetObj.localPosition.y<0)? Mathf.Acos(((b*b)+(c*c)-(a*a))/(2*b*c))*Mathf.Rad2Deg*-1 : Mathf.Acos(((b*b)+(c*c)-(a*a))/(2*b*c))*Mathf.Rad2Deg;
         } 
         
         /* Getting the Y axis offset angle */
         if(OffsetObj.localPosition.z>0){
             a = Vector3.Distance(MainObj.position, MainObj.position + new Vector3(0,0,OffsetObj.localPosition.z));
             h = Vector3.Distance(MainObj.position, new Vector3(OffsetObj.localPosition.x,0,OffsetObj.localPosition.z));
             vectorOffset.y = (OffsetObj.localPosition.x>0)? Mathf.Acos(a/h)*Mathf.Rad2Deg*-1 : Mathf.Acos(a/h)*Mathf.Rad2Deg;
         }
         else{
             a = Vector3.Distance(OffsetObj.position, lookZ.position);
             b = Vector3.Distance(MainObj.position, lookZ.position);
             c = Vector3.Distance(MainObj.position, OffsetObj.position);
             vectorOffset.y = (OffsetObj.localPosition.x>0)? Mathf.Acos(((b*b)+(c*c)-(a*a))/(2*b*c))*Mathf.Rad2Deg*-1 : Mathf.Acos(((b*b)+(c*c)-(a*a))/(2*b*c))*Mathf.Rad2Deg;
         }
         
         /* Getting the Z axis offset angle */
         if(OffsetObj.localPosition.x>0){
             a = Vector3.Distance(MainObj.position, MainObj.position + new Vector3(OffsetObj.localPosition.x,0,0));
             h = Vector3.Distance(MainObj.position, new Vector3(OffsetObj.localPosition.x,OffsetObj.localPosition.y,0));
             vectorOffset.z = (OffsetObj.localPosition.y>0)? Mathf.Acos(a/h)*Mathf.Rad2Deg*-1 : Mathf.Acos(a/h)*Mathf.Rad2Deg;
         }
         else{
             a = Vector3.Distance(OffsetObj.position, lookX.localPosition);
             b = Vector3.Distance(MainObj.position, lookX.localPosition);
             c = Vector3.Distance(MainObj.position, OffsetObj.position);
             vectorOffset.z = (OffsetObj.localPosition.y<0)? Mathf.Acos(((b*b)+(c*c)-(a*a))/(2*b*c))*Mathf.Rad2Deg*-1 : Mathf.Acos(((b*b)+(c*c)-(a*a))/(2*b*c))*Mathf.Rad2Deg;
         }
          
         /* Get the ratio to divid up the X & Z */ 
         yRatio = vectorOffset.y/90;
         yRatio = (yRatio<0)? yRatio*-1 : yRatio;
         yRatio = (yRatio>1)? (90-((yRatio-1)*100))/100 : yRatio;
         
         /* Blance X & Z in relation to the Y */
         Xz = vectorOffset.x * yRatio;
         Xx = vectorOffset.x - Xz;
         Zx = vectorOffset.z * yRatio;
         Zz = vectorOffset.z - Zx;
         
         /* Set the new X & Z */
         vectorOffset.x = Xx+Zx;
         vectorOffset.z = Zz+Xz;
         
         /* Call the lookat function to get the new rotation ... */
         MainObj.LookAt(TargetObj.position);
         /* ... then edit that rotation based on the offset values */
         MainObj.localEulerAngles = MainObj.localEulerAngles + vectorOffset;
     }
 }
 

This method is very fiddly and seems excessive, it isn't working at the moment but I feel that it can work with time.

Is there a better way of going about this or can someone see where I'm going wrong with my code?

Thanks.

untitled-1.png (18.9 kB)
untitled-2.png (5.2 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
0

Answer by DaveA · Feb 28, 2013 at 03:18 AM

I would try something like (untested):

transform.LookAt (childTransform.position + childTransform.forward);

// assumes you have the transform on the child handy

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 · Feb 28, 2013 at 03:59 AM

If you just want to place the child object between the main object and the target, you don't need any rotations at all. Just use Vector3.MoveTowards(). Just specify MainObject's position as the first parameter, target position as the second, the distance you want from Main Object as the third.

If you want a percentage instead of a distance, you can use Vector3.Lerp() with percentage for the third parameter.

Note MoveTowards() may break if the target can pass between the main object and the child object. You can do this as a alternate:

childPos = mainPos + (targetPos - mainPos).normalized * distMainChild;

To get the forwards of the main object and the child object to align with the target, you will need to call Transform.LookAt().

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

10 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

Related Questions

Get mapping postion of 1 point on a plane defined by 3 points 1 Answer

What is the algorithm to assign to transform.right? 1 Answer

Get a child object to rotate a parent to the cameras direction -1 Answers

How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers

I can get a sprite to look at my mouse but not another Vector3, im not doing anything wrong but the sprite just looks in one direction. 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