Question by
dead_byte_dawn · Aug 03, 2018 at 11:26 PM ·
boundarycircular
smoother clamp by magnitude?
I have a prefab that spawns and moves around like an RC car with a circular boundary. This script is functional, but when I hit the 'radius' the motion is jagged.
Is there a way to smooth this out?
public float moveSpeed = 8f;
public Joystick joystick;
Vector3 centerPOS;
Vector3 newLocation;
float radius = 50;
private void Start()
{
joystick = (Joystick)FindObjectOfType(typeof(Joystick));
centerPOS = transform.localPosition;
Debug.Log(centerPOS);
}
void Update()
{
newLocation = transform.position;
Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
if (moveVector != Vector3.zero)
{
Vector3 v = newLocation - centerPOS;
v = Vector3.ClampMagnitude(v, radius);
//newLocation = centerPOS + v;
transform.rotation = Quaternion.LookRotation(moveVector);
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
if (v.magnitude >= radius) {
transform.position -= moveVector;
}
}
}
Comment