- Home /
Achieving something like LookAt but with the X axis.
I'm working on a 2D scene and have been trying to come up with a way to get the results of look at but on the X axis, that is my current objects right faces the target. I found a way around it by using parent objects but it's a jumbled mess. It gives me problems when I use colliders2D and mainly with sprites as these can't be seen if you rotate the objects. So I am wondering if some of you have had this problem before and if maybe you can lend me a hand.
Answer by ashique · Feb 06, 2014 at 06:06 AM
Calculate the direction between current Object and Target object e.g
Vector3 dir = (CurrentObject's Position - TargetObject position). normalized;
CurrentObject.transform.right = dir;
In my experience, the direct assignment to transform.right sometimes results in the sprite flipping.
There's only one problem with your code. When you use LookAt if the target goes behind the object, the object will turn around (do a 180) and continue looking at them. With your code the object just keeps on turning and is upside down if the target goes behind them. I'm working to find a solution to this last part.
Answer by robertbu · Feb 06, 2014 at 05:56 AM
Since this is 2D and the right side of your object is consider the 'forward' side to be pointed at the target, you can do it this way:
Vector3 dir = target.position - transform.position;
float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
There's only one problem with your code. When you use LookAt if the target goes behind the object, the object will turn around (do a 180) and continue looking at them. With your code the object just keeps on turning and is upside down if the target goes behind them. I'm working to find a solution to this last part.
This rotation issue is inherit with a 2D. A fix is to detect when you are on the left side and change the texture. You can check left/right by checking the angle:
if (angle > 90 || angle < -90) {
Debug.Log("The target is on the left side of me");
}
Another solution would be to convert the point to local coordinates and test:
Vector3 local = transform.InverseTransformPoint(target.position);
if (local.x < 0.0) {
Debug.Log("The target is on the left side of me");
}
I'm gonna try it out. I know in the reference it says that LookAt points the objects up toward the worlds up vector so if your object starts turning upside down it'll do a 180 and fix it. I've been trying to replicate that.
Answer by RyanZimmerman87 · Feb 06, 2014 at 06:00 AM
Might need more info but I think what you need is something like:
//playerLookVector can be where you want to look
//playerTransform is whatever object you want to look at the playerLookVector
//lookObject is the object you want to lookAT
//this.PlayerTransform.position.y will cause the player too look straight forward
playerLookVector = new Vector3 (lookObject.transform.position.x,
this.playerTransform.position.y, lookObject.transform.position.z);
this.playerTransform.LookAt(playerLookVector);
That's a 3d example but pretty sure you can still use it or do something similar with Vector2 instead of Vector3.
Your answer
Follow this Question
Related Questions
Unity 5.1.1 Transform Lag/Bug 0 Answers
Inverting Position on One Axis 0 Answers
Pixel Perfect 2 Answers
How can I make a 2d sprite shoot the direction player is facing? 2 Answers
[2D] - Moving the child moves the parent <-- Unwanted behavior 1 Answer