- Home /
How to lock the Z axis so my Enemy just flips on the Y axis
Hi there,
So I have the script below and i need my enemy to follow the player (which it is doing).
Then when the player starts moving the opposite way I need the enemy to just flip on the Y axis so the sprite is facing the correct way.
What I don't want the enemy to do is to rotate on the Z axis as if the enemy is following the player as it jumps and falling backwards.
I want to lock the enemy's Z axis if you understand what I mean.
Thank you! :)
---The Code---
Using UnityEngine; Using System.Collections;
public void Awake()
{
myTransform = transform;
}
public void Start()
{
}
public void Update()
{
transform.LookAt(Player.position);
transform.Rotate (new Vector3 (0, -90, 0));//correcting the original rotation
//move towards the player
if (Vector3.Distance(transform.position,Player.position) > MinDistance)
{
//move if distance from target is greater than 1
transform.Translate(new Vector3(MoveSpeed* Time.deltaTime,0,0));
}
if(Vector3.Distance(transform.position,Player.position) <= MaxDistance)
{
}
}
Answer by VesuvianPrime · Sep 10, 2014 at 11:56 AM
I'm not entirely sure what you're describing in your question, but you can lock a rotation axis by doing:
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0.0f);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Mouse flying 0 Answers
Flipped objects Rotation issues 1 Answer
Problem with transform.Rotate 1 Answer
How to rotate the camera smoothly for just 1 second? 2 Answers