I’ve been trying to get this code to work for an object to draw a line to the player based on the players current location. This script is going to be used for the enemyAi to follow the player so I’m going to use dir (which is the angle that the object is looking at) and then use the translate the object by using the Sin(dir) * spd; and the Cos(dir) * spd;
My Code:
dir =Mathf.Deg2Rad * Vector3.Angle(player.transform.position - transform.position,Vector3.forward);
Debug.DrawRay(transform.position,new Vector3(Mathf.Sin(dir),0,Mathf.Cos(dir))*sightRange);
//To move the enemy object
amountToMove.x = Mathf.Sin(dir)*spd*Time.deltaTime;
amountToMove.z = Mathf.Cos(dir)*spd*Time.deltaTime;
But for some reason it either doesn’t work and points in a different direction or only works at certain spots.
Any reasons why this is happening?
Any thoughts or any extra info that I might need to add?
– mkjrfanWhat are you trying to find the angle of in comparison to what position or direction?
– JChiltonI have one object and the player. What I need to do is find the angle between the object and the player
– mkjrfanTo get the angle you need two vectors, the distance between the two items is one vector. What do you want the other to be? the direction the player is facing?
– JChiltonAlso, do you actually want an angle, or do you want the vector/distance between the two?
– JChilton