- Home /
Raycasting Help
The problem i have is the AI should look at the player then when it hits a object with the tag Prop stop looking at the player and rotate then wait and look at the player again. This is my first attamp at AI and Raycasting so i am in a bit over my head i understand the logic of it but cant seem to get it coded correctly. I hope someone can help with this and if you do thanks :)
here is the code
var target : Transform;
var robotController : CharacterController;
var speed : float = 20.5;
var backwardsSpeed : float =-20.5;
var rayLength : float;
var lookAtPlayerTime : float;
function Start()
{
target = GameObject.Find("MadDoc").GetComponent(Transform);
robotController = GetComponent("CharacterController");
}
function Update()
{
var dir = transform.TransformDirection(Vector3.forward);
var dirleft = transform.TransformDirection(Vector3.left);
var dirright = transform.TransformDirection(Vector3.right);
var hit : RaycastHit;
Debug.DrawRay(transform.position, dir * rayLength, Color.black);
Debug.DrawRay(transform.position, dirleft * rayLength, Color.black);
Debug.DrawRay(transform.position, dirleft * rayLength, Color.black);
if(!Physics.Raycast(transform.position, dir, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Prop")
{
transform.Rotate(0,30 * Time.deltaTime,0);
var forward1 : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed1 : float = speed * Time.deltaTime;
robotController.SimpleMove(forward1 * curSpeed1);
}
}
else
{
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Time.deltaTime;
robotController.SimpleMove(forward * curSpeed);
LookAtPlayer();
}
}
function LookAtPlayer()
{
yield WaitForSeconds(lookAtPlayerTime);
transform.LookAt(target);
}
What is it doing now? What should it be doing? I'm not quite sure what parts of this script you are having problems with.
Basically i want him to look at me until he hits something. Then stop looking at me and rotate and find his way round things to get to me. If that makes any sense
Better explanation. He should look at me until he hits something called Prop then if he does stop looking at me Rotate until he no longer hits anything and then smoothly look at me. Again if his raycast hits something then stop look at me then rotate. This is all i need him to do and he does but it is the look at player i need a way to smoothly look at me ins$$anonymous$$d of just instantly turning and looking my way.
Answer by gfr · Nov 30, 2011 at 06:50 PM
From your comment your problem is how to smoothly look at a target. For that you can gradually update the rotation every frame, e.g. using `Quaternion.Slerp`:
// do this every update/iteration while in the "look at target" state:
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
Time.deltaTime * speed);
Answer by john-essy · Dec 01, 2011 at 08:12 AM
Thankyou gfr i will try this right now thanks mate very much
Your answer
Follow this Question
Related Questions
Finding RayCastHit's Origin Position 2 Answers
My navmesh agent isn't updating correctly? 0 Answers
Diagonal Raycasting to detect platform returns true, even when false 1 Answer
Raycasting and enemies ai 2 Answers
AI Raycast problem 1 Answer