I am trying to make a game object which is basically a ball. For now, I can bring the ball towards the player with the FollowPlayer Script, but cannot rotate towards the player like a ball. Requesting assistance, The code is as of the following:
public float MaxDistance; private Transform Player; public float moveSpeed;
// Start is called before the first frame update
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
if(Vector3.Distance(Player.transform.position, gameObject.transform.position)<= MaxDistance)
{
FollowPlayer();
}
}
void FollowPlayer()
{
var lookPos = Player.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
}