Making a GameObject rotate faster as the player gets closer to it
I am trying to make A shperical object rotate and the rotation speed to get faster the nearer it is to the player. I already have it rotating with a changing speed but it gets slower the closer it is to the player with this script:
using UnityEngine;
public class Enemy : MonoBehaviour {
public float speed_for_distance = 50f;
public GameObject player;
// Update is called once per frame
void FixedUpdate ()
{
transform.Rotate(Vector3.back, 180 * Vector3.Distance(player.transform.position, transform.position) / 10 * Time.deltaTime);
}
}
I have tried dividing the vector3(stuff) by Time.deltaTimeinstead of multiplying but that just made it spin VERY quickly instead.
Any help would be appreciated and thank you in advance
Hi, It gets slower because Vector3.Distance value gets smaller as the player reaches the object. and you multiply this value with 180. I guess a workaround could be to tweak your code to use division or subtraction in a way i.e
180/Vector3.Distance(player.transform.position, transform.position) or maybe 180 - Vector3.Distance(player.transform.position, transform.position)
You can tweak the value of 180 to suit your needs.
Answer by MrCrumbl3d · Apr 22, 2017 at 04:11 AM
void FixedUpdate () { transform.Rotate(Vector3.back, 180 Vector3.Distance(player.transform.position, transform.position) / speed_for_distance Time.deltaTime);