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
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;
}