Rotate only on the z axis

In my game I want the enemy to look at my character, which works but it is doing it on all of the axises. It is a 2D game so it won’t let me freeze the x and y axis. How can I fix this issue?

public float speed;
    private Transform target;
    public float rotatespeed;
    private float targetangle;

    void Start () {
        target = GameObject.FindGameObjectWithTag ("Player").GetComponent<Transform> ();

    }
   

    void Update () {
        transform.position = Vector3.MoveTowards (transform.position, target.position, speed * Time.deltaTime);

        //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position),rotatespeed * Time.deltaTime);

    }
}

I’m haven’t done this in 2d, but in 3d if I want it to rotate around the y axis, I give the target.y, the transform.y. So I think you would do that for the z:

Vector3 lookPos = target.position;
lookPos.z = transform.position.z;
Quaternion.LookRotation(lookPos - transform.position)

AngleAxis, Quaternion.Euler … using Mathf.Atan2

A LookRotation example is also in that link.

(copied from the link**).

You can also set it like so:

transform.right = target.position - transform.position;

That code uses ‘Quaternion.FromToRotation’ internally.

set { rotation = Quaternion.FromToRotation(Vector3.right, value); }

I hope that helps. lol :slight_smile:

Okay, I’m trying it but I don’t know how to convert radiants to degrees and I can’t find a function for it in c#

I really only have to freeze the x and y axis but there are no constraints on rigidbody2D for those axes.

Sorry to keep moving around topics but I’m trying the issue from a bunch of different angles. I’m wondering why this simple code isn’t doing it;

transform.rotation.z = Quaternion.LookRotation (target.position - transform.position);

I should have included a proper message in that previous post of mine. There is a method called ‘Mathf.Rad2Deg’ in Unity which you can use.

Why that last line of code you posted doesn’t work is because you cannot assign to the ‘.z’ portion of that property; not for rotations, or any other struct property.
Did this 1 line of code not work for you?

transform.right = target.position - transform.position;

I included the others to be thorough or in case you wanted more options, or whatever…

Thats getting closer but now he’s moving towards me sideways. Like his shoulders are always pointed towards me.

If you look at the sprite, and the character is facing their right, does the sprite have to have a rotation to produce that?
It sounds like no rotation is not facing right at the moment.

I think he might just be looking on the wrong axis, here is an image of him in both my scene and gameplay. (The dog is the character and the enemy is guy)3478520--276585--Screen Shot 2018-04-29 at 7.09.26 PM.png3478520--276586--Screen Shot 2018-04-29 at 7.10.04 PM.png

I have to us transform.down, which doesn’t exist, and i can’t do -transform.down

I GOT IT! I had to use ‘transform.up = -target.position - transform.position;’
thank you so much for your help on everyone of these posts!

1 Like

No problem. I’m glad you found that solution on your own. It was because your sprite is not facing right by default. I guess, to be fair, so long as you choose 1 way and stick with it, that’s good enough. :slight_smile: I mean, it’s probably easier to remember that way, is all.

Anyways, good stuff. Enjoy your game. :slight_smile:

2 Likes