Hello guys, this problem I’ve been encountering small problems, I understand the logic of when i click objects go to that position. But what I’m really struggling to do is implement raycasting, and stopping objects if there is another opbject infront of it…
Simply, I’m trying to code an RTS animal game, where say all of the zebra within range go to the mose click position, btu when they get there all hell breaks loose with juddering etc.
Any suggestions on how to move forward and tackle this? I am not using a Character controller, but am open to the idea. Would just like some help with this to be honest
private var move : boolean = false;
var hit : RaycastHit;
function Update () {
if (Input.GetButtonDown ("Fire1")) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(Camera.main.transform.position, ray.direction, hit, 1000)) {
move = true ;
}
}
if(move)
{
var wantedPosition= Vector3(hit.point.x, hit.point.y, hit.point.z);
transform.LookAt(wantedPosition);
transform.position = Vector3.Lerp(transform.position, hit.point, Time.time/200);
if(Vector3.Distance(transform.position, wantedPosition)<0.01)
{
move = false;
}
}
}
Note this script replaced my old script (found this on my searches! …cheeky!)