- Home /
Question by
dudedu · Nov 25, 2021 at 02:25 AM ·
addforceraycasthitrigidbody.addforce
addForce to rigidBody works toward rayCastHit, but moves backwards after passing. How can I keep the "push" functionality and stop the backwards motion? please, ty
public class ClickMovement : MonoBehaviour
{
public Rigidbody _rigidbody;
public LayerMask clickMask;
private Vector3 _hitPoint;
[SerializeField] private float moveSpeed = 3f;
[SerializeField] private float _maximumVelocity = 6f;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hitInfo))
{
_hitPoint = hitInfo.point;
}
} else
{
return;
}
}
private void FixedUpdate()
{
if (_rigidbody.velocity.magnitude >= _maximumVelocity)
return;
_rigidbody.AddForce((_hitPoint - transform.position).normalized * moveSpeed);
}
}
Hi, this script I put together a few years ago works, but theres a huge issue. The object will move beyond the raycasthit and will try to backup and park itself correctly. How would you go about turning off the force once the hitPoint has been reached/passed? tysm
Comment