Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Raymond 2 · Feb 14, 2011 at 05:01 PM · enemypathfindingray

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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);
     }

 }

}

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image allesisda · Mar 22, 2011 at 12:48 PM 0
Share

thanks for the script! but unfortunately unity tell me there are lots of errors isn't it a java?

avatar image Raymond 2 · Mar 22, 2011 at 06:12 PM 0
Share

no sorry should have mentioned its c#

avatar image Raymond 2 · Mar 22, 2011 at 06:18 PM 0
Share

(extension should be .cs )

avatar image allesisda · Mar 23, 2011 at 08:59 PM 0
Share

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#

avatar image Raymond 2 · Mar 24, 2011 at 08:47 AM 0
Share

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

Show more comments
avatar image
0

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... : )

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raymond 2 · Feb 14, 2011 at 05:18 PM 0
Share

Alright :D thanks in advance then ;) i'll see it come up then ;)

avatar image Raymond 2 · Feb 14, 2011 at 10:29 PM 0
Share

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 :)

avatar image Raymond 2 · Feb 15, 2011 at 07:19 PM 0
Share

Any progress with the script? :)

avatar image AVividLight · Feb 15, 2011 at 10:15 PM 0
Share

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!

avatar image Raymond 2 · Feb 15, 2011 at 10:40 PM 0
Share

Okay no problem, there's no rush;)

Show more comments
avatar image
0

Answer by DaveA · Feb 14, 2011 at 05:18 PM

Check this out: http://www.arongranberg.com/unity/a-pathfinding/

and http://www.arongranberg.com/unity/pathfinding/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ^^

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raymond 2 · Mar 17, 2011 at 09:42 PM 0
Share

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

avatar image allesisda · Mar 18, 2011 at 12:19 PM 0
Share

this would be really nice ^^ can i also make the enemies run away from me?

avatar image dot · Mar 21, 2011 at 10:31 AM 0
Share

oshi napalm that's something that i'm looking for too

avatar image Raymond 2 · Mar 21, 2011 at 01:29 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Enemy follow script without rotation 1 Answer

Object Avoidance for my enemies 1 Answer

A* pathfinding avoidance 0 Answers

Enemy is not moving to player with A* pathfinding 1 Answer

How to Make Enemy Avoid Player Field of View? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges