2d LookAt?

Hello I want to make a cannon in my 2d camera game,so how can I make it follow the player rotation using LookAt?
I’ve tried this:

var player:Transform;
function Update () {
transform.LookAt(player);
}

But it’s doing a 3d follow(I want to cancel the y-axis follow)
I’ve tried this:

var player:Transform;
function Update () {
transform.LookAt(player.postion.x);
}

But I got this error:

What can I do?

mas code should work

Ummm… postion?

Just move the 3D point you want to Lookat to the Z-plane before LookingAt … like

var player:Transform;
function Update () {
  var lookAtPoint = player.position;
  lookAtPoint.z = transform.position.z;
  transform.LookAt(lookAtPoint);
}

This way you will be sure that you will only rotate into the z-plane.

it still doesn’t works:(
Check a demo out:
http://migre.me/5ryGO

Hehehe corrected to position…But it’s not working…

the code of ‘mas’ should work. Try it

also didn´t work for me :frowning:

1 Like

Try this :

usingUnityEngine;
usingSystem.Collections;

publicclassJumpScript : MonoBehaviour {
publicintplayerSpeed = 5;
privatefloatrotationx;
privatefloatrotationy;
privateVector3touchcoordinates;
privateTransformmyTrans;
privateboolRestartButton_Bool;
publicTransformbackground_map;
publicTouchtouch1;
publicGameObjectGameoverText;

voidStart ()
{
//Cachingofthevariables
myTrans = this.transform;
}

//Updateiscalledonceperframe
voidUpdate ()
{
//Keepthecharacterwithoutanyrotation
myTrans.rotation = Quaternion.Euler(0,0,0);
//ChecktoseeiftheappisrunningoveriOSorAndroidDevices
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
{
//Gettouchdata
foreach (TouchtouchinInput.touches)
{
touch1 = touch;
touchcoordinates = touch.position;
//Covertingtouchcoordinatesinaccordancewithgameuse.
Rayray = Camera.main.ScreenPointToRay(touchcoordinates);
transform.LookAt(ray.GetPoint(-1000),Vector3.forward);

}
touchcoordinates = touch1.position;
}

//CheckiftheappisruuninganywhereotherthanMobiledevices
else
{
//Covertingtouchcoordinatesinaccordancewithgameuse.
Rayray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
transform.LookAt(ray1.GetPoint(-1000),Vector3.forward);
}
//Movingthecharacterforward
transform.Translate(Vector2.up * Time.deltaTime * 5);
}
}

Try this. Simple but worls…

public Transform target; //Add target in inspector

    void Update () 
    {
            float angle = 0;
          
            Vector3 relative = transform.InverseTransformPoint(target.position);
            angle = Mathf.Atan2(relative.x, relative.y)*Mathf.Rad2Deg;
            transform.Rotate(0,0, -angle);
    }
5 Likes

Raimi you was right!, thank you a lot for that.

This should work also, at least in my case. targetVector3 is a vector you want to look at…

void Update()
{
transform.LookAt(targetVector3, Vector3.back);
transform.eulerAngles = new Vector3(0f, 0f, -transform.eulerAngles.z);
}
1 Like

Another example of the same concept but using the mouse position as look target

  void Update()
  {
    Vector3 direction = Input.mousePosition;
    direction.z = -Camera.main.transform.position.z;
    direction = Camera.main.ScreenToWorldPoint(direction) - transform.position;
    transform.rotation = Quaternion.LookRotation(Vector3.forward, direction);
  }
3 Likes

Thanks ABerlemont, that worked perfectly. I also added a custom mouse cursor via the Cursor.SetCursor method, so got me some pixel-perfect pew-pew action going (with the obligatory Kenny 2D space kit art):
6647278--759118--2020-09-28 22-08-38 ig-rogue 03.gif

Raimi options works. But it does instantly rotate, not smoothly rotate. Another solution I found is the following, very similar to Raimi’s one:

SCRIPT

// Assign in inspector the gameobject you want to look at
public GameObject targetGameObject

// Rotation modifier makes a correction on the rotation axis and allows us to adjust the look

// at from axis the one we want to be (just try it and you will understand what it means easily)

float rotationModifier = 90;

// Call the function in fixedUpdate

private void FixedUpdate()

{

ManualLookAt2D();

}

private void ManualLookAt2D()

{

// Calculate the difference between our gameObject and the desired target

Vector3 vectorToTarget = targetGameObject.transform.position - transform.position;

// This functions serves as conversion from 3D space to 2D space

// Forward makes the look at axis being the x axis

// Returns the angle in radians whose Tan is y/x.

// Return value is the angle between the x-axis and a 2D vector starting at zero and terminating at (x,y).

float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg - rotationModifier;

// Sets the transform’s current rotation to a new rotation that rotates “angle” degrees around

// the - y-axis(Vector3.up), z-axis (Vector3.forward), x-axis (Vector.right)

Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);

// Smoothly rotates from our gameObject current rotation to the desired rotation to look

// at the target

transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);

}