- Home /
Find a new target when raycast triggers
I have an AI that finds a target location and walks to it then finds another, etc. This is so it can have a wandering effect. My problem I am having is that once the ray cast triggers for the AI being too close to a wall terrain, it doesn't find a new path. All it does it turn a 180 and the turns around again to continue the same path which is into a wall. I am trying to get the AI to 180 and at the same time find another path to walk, as if it were getting too close to a wall then changing direction to avoid the wall.
Any help would be greatly appreciated!
I need help with finding out what to put in my 'if' Statement where the raycast is being called.
Here is the code I am using for the AI:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class AIScript : MonoBehaviour
{
Transform _player;
Transform _Raptor;
[SerializeField]
public float
_moveSpeed = 5.0f;
/*[SerializeField]
float
Distance = 0.0f;*/
[SerializeField]
public float
_gravity = 2.0f;
public float _yVelocity = 0.0f;
// Cached variables
CharacterController _controller;
protected Animator RaptorAnimator;
Transform _transform;
ChangeDirection _ChangeDirection;
//Movement variables;
float speed = 5f;
float gravity = 100f;
Vector3 moveDirection;
Vector3 _target;
float maxRotSpeed = 200.0f;
float minTime = 0.1f;
float _velocity;
bool change;
float range;
bool chase = false;
int RandomAnimation = 1;
int OldRandomAnimation = 0;
int choice = 0;
RaycastHit hit;
void Start()
{
_controller = GetComponent<CharacterController>();
_transform = GetComponent<Transform>();
RaptorAnimator = GetComponent<Animator> ();
GameObject playerGameObject = GameObject.FindGameObjectWithTag ("Player");
_player = playerGameObject.transform;
range = 2f;
_target = GetTarget();
InvokeRepeating("NewTarget", 0.01f, 2.0f);
}
void OnTriggerStay (Collider inrange)
{
if (inrange.tag == "Player"&& _controller.isGrounded==false)
{
//Debug.Log ("Not Touching");
RaptorAnimator.SetBool ("RunDistance", true);
_moveSpeed = 25;
chase = true;
}
}
void OnTriggerExit(Collider outofrange)
{
if (outofrange.tag == "Player"&& _controller.isGrounded==false)
{
RaptorAnimator.SetBool ("RunDistance", false);
_moveSpeed = 5;
chase = false;
}
}
void Update()
{
var fwd = transform.TransformDirection (Vector3.forward);
Debug.DrawLine(transform.position, fwd , Color.red);
if (Physics.Raycast (transform.position, fwd, out hit, 3) ) {
//Debug.DrawLine(transform.position, fwd * 100, Color.red);
//Debug.Log (hit);
Debug.Log ("The line works");
transform.Rotate(0,180,0);
change = true;
GetTarget();
//Move();
}else{
}
if(chase == false){
if (change) _target = GetTarget();
if (Vector3.Distance(_transform.position, _target) > range )
{
Move();
RaptorAnimator.SetBool ("Idle", false);
}else{
RaptorAnimator.SetBool ("Idle", true);
RandomAnimation = Random.Range(0, 10);
// RaptorAnimator.SetBool ("Animation Running", true);
if(RandomAnimation != OldRandomAnimation){
RaptorAnimator.SetInteger ("RandomAnimationIdle", RandomAnimation);
}else{
RandomAnimation = Random.Range(0, 10);
}
}
}else{
RaptorAnimator.SetBool ("Idle", false);
Vector3 direction = _player.position - transform.position;
direction.Normalize ();
Vector3 velocity = direction * _moveSpeed;
if (!_controller.isGrounded) {
_yVelocity -= _gravity;
}
velocity.y = _yVelocity;
direction.y = 0;
transform.rotation = Quaternion.LookRotation (direction);
_controller.Move (velocity * Time.deltaTime);
}
}
void Move()
{
// Movement
moveDirection = _transform.forward;
moveDirection *= speed;
moveDirection.y -= gravity * Time.deltaTime;
_controller.Move(moveDirection * Time.deltaTime);
//Rotation
var newRotation = Quaternion.LookRotation(_target - _transform.position).eulerAngles;
var angles = _transform.rotation.eulerAngles;
_transform.rotation = Quaternion.Euler(angles.x,
Mathf.SmoothDampAngle(angles.y, newRotation.y, ref _velocity, minTime, maxRotSpeed),
angles.z);
}
Vector3 GetTarget()
{
return new Vector3(Random.Range(0, 300), 0, Random.Range(0, 300));
}
void NewTarget()
{
choice = Random.Range (0, 3);
switch (choice)
{
case 0:
change = true;
break;
case 1:
change = false;
break;
case 2:
_target = transform.position;
break;
}
}
}
Your answer
Follow this Question
Related Questions
Jitter on Slerp - Raycast 0 Answers
How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer
How to get a vector rotation? 1 Answer
Enemy raycast detection 0 Answers
Finding RayCastHit's Origin Position 2 Answers