So I found this tutorial series by BergZerg and they’re really awesome, I actually don’t know why it doesn’t have more views, a whole load of space sim videos to look through so if you like space sims check it out.
I did his pathfinding video and it all makes sense, however, no matter what I adjust when you try to make the gameobject that is moving avoid obstacles it just doesn’t seem to want to. Sometimes it will clip through cubes as you’ll see in the video of me trying out this code and then other times it will ignore the cubes entirely.
Does anyone have any ideas on how to fix this? I thought it would be a simply matter of increasing the range and offset of the raycasts so that the gameobject thinks it’s larger than it actually is and avoid the cubes it can’t squeeze through entirely however it still follows the same behaviour and changing the scale of the collider didn’t do anything either.
I don’t think many people are going to watch a 15 minute tutorial just to figure out what’s your script.
Why don’t you post the AI script? Also, do Debug.Logs often to see why the plane isn’t detecting the cubes.
Debug.Log is your best friend when debugging. If I run into issues I put a Debug.Log after almost everything so I can run the scene once, stop it then analyze the Console to see what’s wrong with my script’s logic.
Oh sorry, I actually completely forgot here it is.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyingAIPathfinding : MonoBehaviour {
[SerializeField]public Transform target;
[SerializeField]float movementSpeed = 10f;
[SerializeField]float rotationalDamp = 0.5f;
[SerializeField] float detectionDistance = 20f;
[SerializeField] float rayCastOffset = 0.3f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
Pathfinding();
Move();
}
void Turn ()
{
Vector3 pos = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation (pos);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotationalDamp * Time.deltaTime);
}
void Move ()
{
transform.position += transform.forward * movementSpeed * Time.deltaTime;
}
void Pathfinding ()
{
RaycastHit hit;
Vector3 raycastOffset = Vector3.zero;
Vector3 left = transform.position - transform.right * rayCastOffset;
Vector3 right = transform.position + transform.right * rayCastOffset;
Vector3 up = transform.position + transform.up * rayCastOffset;
Vector3 down = transform.position - transform.up * rayCastOffset;
Debug.DrawRay (left, transform.forward * detectionDistance, Color.red);
Debug.DrawRay (right, transform.forward * detectionDistance, Color.red);
Debug.DrawRay (up, transform.forward * detectionDistance, Color.red);
Debug.DrawRay (down, transform.forward * detectionDistance, Color.red);
if (Physics.Raycast (left, transform.forward, out hit, detectionDistance))
{
raycastOffset += Vector3.right;
}
else if (Physics.Raycast (right, transform.forward, out hit, detectionDistance))
{
raycastOffset -= Vector3.right;
}
if (Physics.Raycast (up, transform.forward, out hit, detectionDistance))
{
raycastOffset -= Vector3.up;
}
else if (Physics.Raycast (down, transform.forward, out hit, detectionDistance))
{
raycastOffset += Vector3.up;
}
if (raycastOffset != Vector3.zero)
{
transform.Rotate (raycastOffset * 5f * Time.deltaTime);
}
else
{
Turn();
}
}
}
Well I did do what BergZerg recommended and get the debug rays up it does avoid the cubes but not entirely, I thought I could fix this by spreading the rays out more so that the code will avoid tight spaces more but that didn’t seem to happen and I’m not sure why.
Personally, I find the script to be kinda trashy. It doesn’t even lerp rotation to make a smooth rotation. If I have time I’ll write a better one today or tomorrow as C# practice. Also, instead of raycasting, I’d probably use a trigger collider. That way you wouldn’t have spots where objects can get between rays; and it’s probably cheaper than shooting multiple rays every FixedUpdate frame.
So far I’ve gotten pretty nice plane lerping. Instead of a jagged jerky move, the plane smoothly follows the target. I’ll also add horizontal tilt to add realism.
I’ve actually never done 3D AI (only 2D AI) so this is interesting C# practice. Now onto object avoidance!
Thanks, I do appreciate you taking a look at this, I’d personally just be happy with the clipping issue being sorted but if you can demonstrate a better way it would be interesting to look at it. I was just following along the tutorials, been looking to add some proper AI into a shooter project I’m doing.
I’m sort of late because I’m away from my PC and am busy.
Basically, the script relies on multiple raycasts to move your plane. The problem is that it isn’t always detecting the cubes because they can slip between the raycasts, and it’s not very intelligent when it comes to avoidance.
In theory, an expensive ugly way to fix that script would be to have a script shoot many raycasts at specified intervals. It would be more expensive, but it would fare better at avoidance. Just curious, why is there no way coming directly from the front of your plane? You have rats around the plane, but not in the middle.
I’m still interested in finishing my script as practice, I’m just late
That’s just me following the tutorial, I haven’t had a chance yet to experiment with putting raycasts in the centre and so on, this type of raycast pathfinding is quite new to me and I just thought I’d try it out and there’s no rush so don’t worry about getting something up quickly I was just curious.
Thanks for the explanation of the raycasts missing the cubes, that explains why it catches the cube sometimes and then misses I would have no idea how to get raycasts firing off repeatedly like you suggest, would that involve IEnumerators or Invoke repeating perhaps?