user11154649
user11154649

Reputation:

How to make LookAt align the transform.up vector instead of transform.forward?

The problem is that i cannot make a script that makes the enemy rotate so that "up" is pointing against the player, (with that i mean so that it will come to the player with transform.up) And i have not found any working solution yet, (from what i can tell)

I am trying to make a small 2D game, just for my friends to play. What i have tried is making the enemy look at the player and then set the "z" rotation to the same as the "x" rotation and after that reset the "x" and "y" rotation (so that "up" is pointing at the player). But the enemy just goes straight up.

using UnityEngine;
public class Enemy : MonoBehaviour
{
    public Transform Player;  // The Transform of the Player that you can controll
    public float MoveSpeed = 4; // How fast the enemy should go
    void FixedUpdate()
    {
        transform.LookAt(Player);
        transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.x);
        transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z);
        transform.position += transform.up * MoveSpeed * Time.deltaTime;

    }
}

So what i want to happen is that the Enemy will move to the Player but the rotation part is not working and it just goes straight up in the air instead of following the player. I might just be stupid and have missed something simple...

Upvotes: 3

Views: 4455

Answers (1)

derHugo
derHugo

Reputation: 90659

By using

transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.x);
transform.rotation = Quaternion.Euler(0, 0, transform.rotation.z);

both times you overwrite the rotation with new values additionally mixing Euler (Vector3) with Quaternion fields ... transform.rotation.x is not the pure rotation around the X-axis you see in the Unity inspector.


It depends a bit on how exactly you want the enemy rotated in the end but you could simply use Transform.RotateAround like

transform.LookAt(Player);
transform.RotateAround(transform.position, transform.right, 90);

LookAt by default results in a rotation such that transform.forward is pointing at the target, transform.right remains pointing right(ish) and transform.up remains pointing up(ish). So all you have to do is rotating around that local X-axis transform.right exactly 90° so now transform.forward points down instead and transform.up points at the target.

which results in

enter image description here


If you than also want to change the other axis you can still additionally rotate it around the local transform.up axis as well e.g. to flip the forward vector "upwards":

transform.LookAt(Player);
transform.RotateAround(transform.position, transform.right, 90);
transform.RotateAround(transform.position, transform.up, 180);

Is there actually a special reason why you are doing that in FixedUpdate and not rather in Update? FixedUpdate should only be used for Physics related stuff.

Upvotes: 5

Related Questions