I am making a sandbox fps with zombies (just to learn unity more). i made a simple script that is sends a ray from player camera and when you press right mouse button it spawns the enemy correct. the enemy walks to the same point on the terrain every time and i have no clue why.
I just changed (under enemy movement) transform.position to player.transform.position and this time insead of spawning on my raycast hit it spawned at the same spot they used to just walk to. i then took off player. from both position and rotation and now they just sit there.
public class EnemyMovement : MonoBehaviour
{
public GameObject player;
[SerializeField]private float speed = 4.5f;
void Update()
{
Move();
}
private void Move()
{
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
}
}
public class PlayerEnemySpawner : MonoBehaviour
{
[SerializeField]private Camera PlayerCamera;
[SerializeField]private GameObject Enemy;
[SerializeField]private float Range = 15f;
void Update()
{
Spawn();
}
private void Spawn()
{
RaycastHit hit;
if(Input.GetKeyDown(KeyCode.Mouse1))
{
Physics.Raycast(PlayerCamera.transform.position, PlayerCamera.transform.forward, out hit, Range);
if(hit.transform.tag == "Terrain")
{
Instantiate(Enemy, hit.point + Vector3.up * 1f, Quaternion.identity);
Debug.Log("Enemy Spawned");
}
else
{
Debug.Log("Cannot Spawn Enemy here!");
}
}
}
}