Is there a Transform.LookAt() equivalent that will work for 2D?
Without turning the gameobject on it’s side that is…
Or alternatively does anyone have a workaround using Eulers or something ?
Thanks in advance
Just wanted to drop in and say this code solved my issues of having my projectile instantiate with the wrong rotation. This code works great. Thank you!
I tried using this code to make a gameobject to "follow" my cursor [48529-code.jpg|48529] But I get this problem where it believe that the position of my gameobject is (0,0) so I have to move my mouse down in the left corner to get the gameobject to rotate. What am I doing wrong? (I have the Camera as a child under my "player" gameobject, does that have anything to do with this?) Please help <3
Maybe that hasn’t always been possible, but it sure beats messing about with Atan2.
This IS genius. And it SHOULD be the accepted answer. I had to use transform.up for my solution but all the same, there's no need math functions. Bedurbedur
I had an issue when using Camera.ScreenToWorldPoint(Input.mousePosition) as the target. Turns out that ScreenToWorld point returns -10 as the Z, so one simply needs to add (0, 0, 10).
It is too genius that I have to login my account and give you a vote!! This is really easy to understand and it makes me able to teach my students the 2DLookAt function on my lesson!! Thanks a lot!!
this isn't working for me, the sprite i'm having turn is moving and the farther it gets from the center the more uncontrollable it is, and the rotations seem to be reversed, up and down at right, but left and right are flipped. the only difference is my diff is the target - transform.postion where target is a vector3 of the object i'm trying to chase
I know it is a bit late, but is it possible to make sprite rotate for a certain degree? I am using OnMouseDrag(), and I don't want it to juse follow mouse, but to skip 15 degrees.
I like your solution for it's simplicity and in this way you don't have to use .Normalize. What I did to make this piece of code result in a sprite really pointing forward to the target was simply to increase the angle with 270f. So you get: Vector3 dir = target.position - transform.position; float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg + 270; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Thank You!!!!!!! I went crazy because of my enemies didn't follow the Player. They disappeared! Then I saw that Transform.LookAt (...) caused the bug and now I know how to fix it. Thanks!!
Rotate 90 degrees on y-axis (your rotation may differ) so front of sprite is facing parent’s Z-axis
When LookAt() called on parent, previously empty gameobject, Z-axis is front. Since child is rotated locally so its front is facing the parent gameobject’s Z-axis, LookAt() works fine.
using UnityEngine;
using System.Collections;
public class JumpScript : MonoBehaviour
{
public int playerSpeed = 5;
private float rotationx;
private float rotationy;
private Vector3 touchcoordinates;
private Transform myTrans;
private bool RestartButton_Bool;
public Transform background_map;
public Touch touch1;
public GameObject GameoverText;
void Start ()
{
//Caching of the variables
myTrans = this.transform;
}
// Update is called once per frame
void Update ()
{
//Keep the character without any rotation
myTrans.rotation = Quaternion.Euler(0,0,0);
//Check to see if the app is running over iOS or Android Devices
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
{
//Get touch data
foreach (Touch touch in Input.touches)
{
touch1 = touch;
touchcoordinates = touch.position;
//Coverting touch coordinates in accordance with game use.
Ray ray = Camera.main.ScreenPointToRay(touchcoordinates);
transform.LookAt(ray.GetPoint(-1000),Vector3.forward);
}
touchcoordinates = touch1.position;
}
//Check if the app is ruuning anywhere other than Mobile devices
else
{
//Coverting touch coordinates in accordance with game use.
Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
transform.LookAt(ray1.GetPoint(-1000),Vector3.forward);
}
//Moving the character forward
transform.Translate(Vector2.up * Time.deltaTime * 5);
}
}
fixedZ = -90;// Or whatever value is needed to set correct facing of your object. Play with this.
transform.LookAt( new Vector3( target.position.x , target.position.y , fixedZ ) );
@LEDWORKS
While i realize that this is an old question, i wanted to share my easy to use solution.
Just stick this in an empty C# file somewhere in your project. a LookAt2D will magically appear
I just whipped this up after spending some time trying all of your solutions, none of which quite worked for me.
/** copyright Leroy Ketelaars, 2015.
* I hereby license the entire human race to use this code as they see fit,
* provided they maintain this license in their source code as-is.
* A credit mention in your resulting work would be appreciated. */
using UnityEngine;
using System.Collections;
public static class TransformExtensions {
public static void LookAt2D(this Transform t, Vector3 worldPosition) {
t.rotation = Quaternion.identity;
t.Rotate(Vector3.forward, (Mathf.Atan2(t.position.y - worldPosition.y, t.position.x - worldPosition.x) * 180 / Mathf.PI) - 180f);
}
public static void LookAt2D(this Transform t, Transform target) {
t.rotation = Quaternion.identity;
t.Rotate(Vector3.forward, (Mathf.Atan2(t.position.y - target.position.y, t.position.x - target.position.x) * 180 / Mathf.PI) - 180f);
}
public static void LookAwayFrom2D(this Transform t, Vector3 worldPosition) {
t.rotation = Quaternion.identity;
t.Rotate(Vector3.forward, (Mathf.Atan2(t.position.y - worldPosition.y, t.position.x - worldPosition.x) * 180 / Mathf.PI));
}
public static void LookAwayFrom2D(this Transform t, Transform target) {
t.rotation = Quaternion.identity;
t.Rotate(Vector3.forward, (Mathf.Atan2(t.position.y - target.position.y, t.position.x - target.position.x) * 180 / Mathf.PI));
}
}
@Cladnic
You need to draw a raycast through the camera itself into the worldspace to accurately put the cursor on screen. You completely have to for 3D and I use it in my 2D games as well, it seems to work better when you don’t want to worry about screen resolution come build time. My example code is in 3D but for 2D you just don’t lock the y axis.
Implementing this method also gives you the ability to restrict the movement of the cursor to a designated area using the layerMask parameter. For making it follow your cursor you just use Vector3.MoveTowards and feed it a movement speed like this:
I’d also recommend going a step further and making sure your mouse is in an area it should be able to move to, and making sure if the target trails too far behind the cursor it catches up. Here’s an example from an async-multiplayer game where the second player used the mouse to control a reticle while the first player controlled the primary location of the screen.
// Return the angle in degrees of vector v relative to the x-axis.
// Angles are towards the positive y-axis (typically counter-clockwise) and between 0 and 360.
public float Angle ( Vector2 v )
{
float angle = (float)Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
if (angle < 0) angle += 360;
return angle;
}
You could use vector2, it have only x and y coordinates.
Na, No Luck, just tried... Vector2 LookAtPoint = new Vector2(Target.transform.position.x, Target.transform.position.y); transform.LookAt(LookAtPoint); Is This what you mean?
@LEDWORKS That works fine for me. Make sure to keep your plane in mind. If you're working with the XY plane then that should work fine, but if you're working on the ZY plane then you'll have to do Vector2 LookAtPoint = new Vector2(Target.transform.position.z, Target.transform.position.y); transform.LookAt(new Vector3(0,LookAtPoint.y,LookAtPoint.x);
Thanks so much. You just solved a problem I've been having for days!
– Stoneheadthx dude. that rly helped
– diabliaJust wanted to drop in and say this code solved my issues of having my projectile instantiate with the wrong rotation. This code works great. Thank you!
– Zingetransform.LookAt(Vector3(target.position.x , target.position.y , fixedZ));
– FapppI tried using this code to make a gameobject to "follow" my cursor [48529-code.jpg|48529] But I get this problem where it believe that the position of my gameobject is (0,0) so I have to move my mouse down in the left corner to get the gameobject to rotate. What am I doing wrong? (I have the Camera as a child under my "player" gameobject, does that have anything to do with this?) Please help <3
– Cladnic