Enemy Ai Rotation not working properly
Basically what happens when I got to the corner of my screen the y rotation goes to 180 and i can't see the enemy as the game is 2d. This is my script.
public class Get_Player : MonoBehaviour {
public float Speed;
public Transform target;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update () {
Vector3 dir = target.position - transform.position;
transform.LookAt (target.position);
transform.Rotate (new Vector3 (0, -180, 0), Space.Self);
if (Vector3.Distance (transform.position, target.position) > 0f) {
transform.Translate (new Vector3 (Speed * Time.deltaTime, 0, 0));
}
}
}
You can edit your own posts to fix mistakes and clarify things, you know? ;)
Things to clarify: Who is getting to the corner? The player or the object controlled by this script?
Also, why do you rotate around y anyway there? looks strange..
I'm not entirely sure what you are trying to accomplish but what i see is that you call transform.LookAt and transform.Rotate in every frame this will lead to unexpected results. If you can explain a little bit more what you are trying to do I can maybe help a little bit more ;P.
call transform.LookAt and transform.Rotate in every frame this will lead to unexpected results
Sorry? There is nothing wrong with changing a transform rotation via different methods multiple times in a frame and every frame.
What are the unexpected results you are talking about? I am not aware of any hickup in Unity regarding to changing transform properties every frame (or even more often).
When he says unexpected results, he doesnt mean that unity has a problem doing that. But that the programmer usally has no clue what the result is, but very likely not the desired one.
So having just 1 way of changing helps you - as the programmer to understand what actually is happening - which in return helps a lot in making the script behave as you want. ;-)
Your answer
Follow this Question
Related Questions
how can i anchor a point of a sprite to make it swing? 1 Answer
scale a value 0 Answers
2D player enter/exit orbit 0 Answers
Unity 2d C# - Orbit object around another objects center using mouse position 0 Answers
Unity 2D- How to role a polygon? 0 Answers