- Home /
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.
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.
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.
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
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().
Your answer
Follow this Question
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