So i have two cubes in my scene, one is the player who has a click to move script enabled and a rigidbody, and the other is the enemy who of course has a collider. The problem here is that i don’t know how to make the player stop at a certain distance before the enemy so now he just crashes on the enemy and the whole camera shakes. Is there a solution for that?
Sure, something like this should work:
Vector3 startPoint;
Vector3 endPoint;
float moveDist;
void Update()
{
//on mouse click
if(Input.GetMouseButtonDown(0))
{
endPoint = //whatever code you use to calculate the point to move to
//store the point where we are starting our new move
startPoint = transform.position;
//store the distance between the start point and end point
moveDist = (startPoint - endPoint).magnitude;
}
//if not at target move point
if(transform.position != endPoint)
{
//direction to move towards end point
Vector3 dir = (endPoint- transform.position).normalise;
//move towards end point
transform.position += dir * Time.delateTime;
//current distance from start point
float dist = (transform.position - startPoint).magnitude;
//if we have moved a distance greater than we should have
if(dist > moveDist)
transform.position = endPoint; //set us to target point
}
}
The camera is shaking because the target of the camera is the Rigidbody Center of mass. Change the target to the Gameobject to fix this.
use FixedUpdate() in Camera script! It will fix problem/