- Home /
Ray-based pathfinding
Does anyone perhaps have a script of ray based pathfinding? A simple one, so that a object will follow the player and walk around objects.. Thanks
Answer by Raymond 2 · Mar 21, 2011 at 01:31 PM
Okay so here's the script I wrote, hope it'll be to use to someone Here it is: It should be self explaining, especially with all the added comments:P EDIT: It is written in C# so the extension should be .cs and the name should be EnemyFollowerNew so you'll get EnemyFollowerNew.cs You have to drag the target object into the target slot too or else it just won't follow you:)
If anything still isn't clear let me know.
using UnityEngine; using System.Collections;
public class EnemyFollowerNew : MonoBehaviour { // Variables public float speed = 20; public float rotateSpeed = 10; private int dIrection = -1; public float maxDistance = 5; public float directionDistance = 5; public float targetDistance = 5; public float followSpeed = 11; public Transform Target; // End variables
// Update is called once per frame
void Update ()
{
//Check Distance between current object and Target
float dist = Vector3.Distance(Target.position, transform.position);
//print("Distance to other:" +dist);
// If distance is bigger then distance between target, wander around.
if (dist > targetDistance)
{
//Check if there is a collider in a certain distance of the object if not then do the following
if(!Physics.Raycast(transform.position, transform.forward, maxDistance))
{
// Move forward
transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
}
else
{
// If there is a object at the right side of the object then give a random direction
if(Physics.Raycast(transform.position, transform.right, directionDistance))
{
dIrection = Random.Range(-1, 2);
}
// If there is a object at the left side of the object then give a random direction
if(Physics.Raycast(transform.position, -transform.right, directionDistance))
{
dIrection = Random.Range(-1, 2);
}
// rotate 90 degrees in the random direction
transform.Rotate(Vector3.up, 90 * rotateSpeed * Time.smoothDeltaTime * dIrection);
}
}
// If current distance is smaller than the given ditance, then rotate towards player, and translate the rotation into forward motion times the given speed
if (dist < targetDistance)
{
transform.LookAt(Target);
transform.Translate(Vector3.forward * followSpeed * Time.smoothDeltaTime);
}
}
}
thanks for the script! but unfortunately unity tell me there are lots of errors isn't it a java?
sry, hopefully last dumb question: how do i have to call the script? it says something about the class, but i don't know much about c#
should have mentioned that too, the public class name should also be the name you give the script, so your script will be called EnemyFollowerNew.cs
Answer by AVividLight · Feb 14, 2011 at 05:14 PM
When I get a chance today, I'll write one (I need something like it) and post it for you...
-Edit-
I just thought of a simple way to do it... Add this script to your "Enemy":
var thePlayer : Transform;
function Update () { transform.LookAt(thePlayer); }
And give it a constant relative force of 10, 0, 0
I will still write a script today using raycasting... I haven't tested the method I put above, but it might work... : )
Alright :D thanks in advance then ;) i'll see it come up then ;)
I also got this part right now: var thePlayer : Transform;
function Update () { transform.LookAt(thePlayer); } at least kinda like it, it also contains the translate.transform code hope you can get raycasting to work with this :)
Yeah, I will post it as soon as I can finish it... Sorry I didn't finish it yesterday... I'll try to get it working today!
Answer by DaveA · Feb 14, 2011 at 05:18 PM
Check this out: http://www.arongranberg.com/unity/a-pathfinding/
Answer by allesisda · Mar 17, 2011 at 05:03 PM
hey, I'm looking for such a script too! I would like to get something like this:
http://www.youtube.com/watch?v=f4gBEw0f6AA
I hope GBStudios (or maybe someone else) will get it soon ^^
I now wrote a script wich let's a enemy wander around, picking random directions, and when the player comes in a certain radius of the enemy it will follow him, so if that's what your looking for then i'll post it for you
this would be really nice ^^ can i also make the enemies run away from me?
running away from the player can be implemented, but it's not what i have now, but i will post the script for when following the player
Your answer
![](https://koobas.hobune.stream/wayback/20220613104344im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy follow script without rotation 1 Answer
Object Avoidance for my enemies 1 Answer
A* pathfinding avoidance 0 Answers