- Home /
How to get a ai to drive to a raycast
I am making a script for a AI vehicle (Robot)to follow (drive, goto) the hit of a raycast Think of it as toying with a dog with a laser. You point laser = dog follows laser.
I think this sort of script is used. var speed : float = 0.6; speed * Time.deltaTime
but im missing something... Im guessing its along these lines...
var bot : GameObject; //The robot that must follow the raycast var speed : float = 0.6 //Speed of the robot bot.transform.position, speed * Time.deltaTime =(Hit.point);
So heres my current script.
var bot : GameObject; //The robot that must follow the raycast
var Range : float; // Range of the raycast
var mesh : GameObject; //Cammea the raycast comes from
var Hit : RaycastHit; //Impact of the raycast the robot must follow
var speed : float = 0.6; //Speed of the robot
//References of how i learnt about raycasting
//http://answers.unity3d.com/questions/15761/click-on-terrain-and-record-vector3-location-i-cli.html
//http://www.youtube.com/watch?v=cZsG4dfCUec
//http://www.youtube.com/watch?v=-F_w3RDRbN4
function Update () {
if(Input.GetMouseButtonUp(1))
var Directionofline : Vector3 = mesh.transform.TransformDirection(Vector3.forward);
Debug.DrawRay( mesh.transform.position, Directionofline * Range, Color.yellow); //Raycast in visual
if(Physics.Raycast( mesh.transform.position, Directionofline, Hit, Range)){
if( Hit.collider.gameObject){
bot.transform.position, speed * Time.deltaTime =(Hit.point); //The final result of the script
Debug.LogWarning("Potatoes!"); //Success the script works!
}
}
}
All scripts above are attached to the player camera (Camera.main)
Im not sure if i should attach the movement script to the raycast script or on the robot it self (which is currently a cube with ridgid body) as the beginings of the script is on the raycast.
Also if you can provide something for turning the robot instead of sidestep, that would be cool!
I'd basicly like it to move like a tank.
Edit: Ok clearly i didnt explain it well enough. The aim is to get the 'bot' variable to move (as in character move) to the point the raycast has hit, which is done. So what do i do here?
bot.transform.position, speed * Time.deltaTime = (Hit.point)
I have only glanced at your script, but the first thing that stands out is line 25 :
bot.transform.position, speed * Time.deltaTime =(Hit.point); //The final result of the script
this is incorrect. I have no idea what you are trying to do with the hit information. All I can assume is that you meant :
bot.transform.position += Directionofline * speed * Time.deltaTime;
Hopefully someone else will give this a better read than I did. Good Luck.
= (Hit.point);
is the destination where the robot needs to go to in world cordonates
Your answer
Follow this Question
Related Questions
How to get a ai to drive to a raycast 0 Answers
Use OnTriggerEnter during a transform ! 0 Answers
Raycast when swipe or touchphase move 0 Answers
Why is this Raycast not working ? 3 Answers
Set Quaternion to another Quaternion, but oriented along 1 Answer