- Home /
Question by
Zyie · Apr 06, 2018 at 05:48 PM ·
aicollision detectionobstacle avoidance
Spaceship Obstacle Avoidance
Hi, so I'm creating a procedural generated solar system and i want to have a couple of spaceships race each other around the planets.
im using waypoints to direct the spaceships on where to go, which works fine. However the issue i am having is that occasionally the ship will not avoid the planets and go right through them.
Does anyone know how to fix this or know of a better way to do obstacle avoidance with moving obstacles?
Below is the code i am using to move the ships right now
private void Update()
{
if (Target != null)
{
//Look At Somthly Towards the Target if there is nothing in front.
if (!_isThereAnyThing)
{
Vector3 relativePos = Target.transform.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
}
else
transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed);
}
// Enemy translate in forward direction.
transform.Translate(Vector3.forward * Time.deltaTime * Speed);
//Checking for any Obstacle in front.
// Two rays left and right to the object to detect the obstacle.
Transform leftRay = transform;
Transform rightRay = transform;
if (Physics.Raycast(leftRay.position + (transform.right * _xScale), transform.forward, out _hit, RaycastRange) ||
Physics.Raycast(rightRay.position - (transform.right * _xScale), transform.forward, out _hit, RaycastRange))
{
if (_hit.transform != transform)
_isThereAnyThing = true;
}
else
_isThereAnyThing = false;
}
Comment
Your answer
