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 William_Goodspeed · Aug 20, 2014 at 02:38 PM · raycasttransformraycastingpathfinding

How do I change movement speed of my Raycasting A*pathfinding transform?

So I'm going for my first attempt at RayCasting with A*Pathfinding. Everything is going well I click and my path is set based on where the ray has hit, that's all good. But my issue is setting the speed of my movement. Now I'm not using a character controller for various reasons.

Is there a simple way to slow the movement down? Here's my code so far :)

 using UnityEngine;
 using System.Collections;
 using Pathfinding;
 
 public class PlayerPather : MonoBehaviour {
 
     public GameObject theCamera;
     public RayCast cC;
 
     public Transform target;
 
 
     Seeker seeker;
     Path path;
     int currentWaypoint;
 
     float speed = 10;
 
     float maxWaypointDistance = 1f;
 
     // Use this for initialization
     void Start () {
 
 
         cC = theCamera.GetComponent<RayCast>();
 
         seeker = GetComponent<Seeker>();
 
         seeker.StartPath(transform.position, cC.targetposition, OnPathComplete);
     }
     
 
 
     public void OnPathComplete(Path p){
         if(!p.error)
         {
             path = p;
             currentWaypoint = 0;
             cC.gothere = false;
         }
         else {
             Debug.Log (p.error);
         }
     }
 
     void FixedUpdate(){
         if(cC.gothere == true){
         seeker.StartPath(transform.position, cC.targetposition, OnPathComplete);
         }
         if(path == null){
             return;
         }
 
         if(currentWaypoint >= path.vectorPath.Count){
             return;
         }
 
         transform.position = path.vectorPath[currentWaypoint];
         if(Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]) < maxWaypointDistance){
         currentWaypoint++;
         
         }
     }
     
 
 }

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

2 Replies

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

Answer by stulleman · Aug 20, 2014 at 02:47 PM

You are not using any speed at all, as far as I can see. I expect that your character moves really fast to the target because you just put him on every waypoint.

You have to do something like this

 void FixedUpdate () {
         if(path == null || pathComplete) {
             return;
         }
         
         Vector3 currTarget = path.vectorPath[currentWaypoint];

         Vector3 dir = (currTarget - transform.position).normalized;

         // Position
         transform.position += dir * speed * Time.deltaTime;
         
         if(Vector3.Distance(transform.position, currTarget) < nextWaypointDistance) {
             currentWaypoint++;
         }
 
         if(currentWaypoint >= path.vectorPath.Count) {
             pathComplete = true;
         }
     }

If you need some rotation, just let me know.

EDIT: Edited the code, I missed the direction Vector

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 William_Goodspeed · Aug 20, 2014 at 03:40 PM 0
Share

I altered quite a lot of my code to try and get this working but it doesn't. I never declare 'dir' so I'm not sure what that's doing.

avatar image Cherno · Aug 20, 2014 at 03:59 PM 0
Share

It stores the direction of the next waypoint and thereby, where to move, based on teh current position.

stulleman's code should work as it stands.

avatar image stulleman · Aug 20, 2014 at 05:48 PM 0
Share

Have you seen my edit? $$anonymous$$aybe you tried the old version, where I forgot the direction.

avatar image William_Goodspeed · Aug 27, 2014 at 03:01 PM 0
Share

Hmmm, I'm not sure where you're getting the 'nextWaypointDistance' from it's never assigned anything so it's always 0

avatar image
0

Answer by Cherno · Aug 20, 2014 at 02:49 PM

Well, your script doesn't even include any movement code, so that would help a lot ;)

 IEnumerator Move() {
 
         isMoving = true;
 
 
         Vector3 startPosition = transform.position;
         Vector3 endPosition = path.vectorPath[currentWaypoint];
         float distance = Vector3.Distance(startPosition, endPosition);
 
         float t = 0.0f;
         while(t < 1.0f) {
             t += Time.deltaTime / distance * moveSpeed;
             transform.position = Vector3.Lerp(startPosition, endPosition, t);
             yield return null;
         }
 
         isMoving = false;
 
     }

At the end of OnPathcomplete(), you add

 doMove = true;

and in Update(), you add

 if(doMove == true) {
             if(isMoving == false) {
                 StartCoroutine(Move());
 
             }
 
         }


Then you could of course also set doMove to false when the last waypoint is reached.

The example scenes and scripts of the Pathfinding proejcts contain several scripts worth a look, AIBot and AIPath in particular.

Comment
Add comment · Show 6 · 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 William_Goodspeed · Aug 20, 2014 at 04:15 PM 0
Share

This didn't work, my character would only move to the next waypoint.

avatar image Cherno · Aug 20, 2014 at 04:19 PM 0
Share

That's probably because the maxWaypointDistance is too low.

If no one here can help you, you really should look at the example scripts provided with the project, there is one very simple that has a proper function for moving and speed. I think it's AIFollow.

avatar image William_Goodspeed · Aug 20, 2014 at 04:30 PM 0
Share

Simple moving and speed isn't the problem, it's getting that working with Raycasting and A*Pathfinding that I'm struggling with.

It's not the maxWaypointDistance

avatar image Cherno · Aug 20, 2014 at 04:52 PM 0
Share

Okay. Have you looked at the example script? You only need to put the Vector3 you get from your raycast into the script's targetPosition variable.

avatar image William_Goodspeed · Aug 27, 2014 at 03:47 PM 0
Share

So I've updated my code since the answers above and I've got my guy beginning to move along the waypoints, but he stops half way along the path and I get a constant error stating "".

I suspect that the issue is I'm updating the currWaypoint before he is actually reaching it. Any help would be appreciated!

 using UnityEngine;
 using System.Collections;
 using Pathfinding;
 
 public class PlayerPather : $$anonymous$$onoBehaviour {
 
     public GameObject theCamera;
     public RayCast cC;

 
 
     Seeker seeker;
     Path path;
     int currentWaypoint;
 
     float nextWaypointDistance;
 
     float speed = 5f;
 
 
     // Use this for initialization
     void Start () {
 
 
         cC = theCamera.GetComponent<RayCast>();
 
         seeker = GetComponent<Seeker>();
         seeker.StartPath(transform.position, cC.targetposition, OnPathComplete);
     }
     
 
 
     public void OnPathComplete(Path p){
         if(!p.error)
         {
             path = p;
             currentWaypoint = 0;
             cC.gothere = false;
         }
         else {
             Debug.Log (p.error);
         }
     }
 
     void FixedUpdate(){
 
         if(cC.gothere == true){
             seeker.StartPath(transform.position, cC.targetposition, OnPathComplete);
         }
 
 
         if(path == null) {
             return;
         }
         
         Vector3 currTarget = path.vectorPath[currentWaypoint];
         
         Vector3 dir = (currTarget - transform.position).normalized;
 
         nextWaypointDistance = Vector3.Distance(dir, currTarget);
         
         transform.position += dir * speed * Time.deltaTime;
         
         if(Vector3.Distance(transform.position, currTarget) < nextWaypointDistance) {
             currentWaypoint++;
         }
         
         if(currentWaypoint >= path.vectorPath.Count) {
             return;
         }
 
     }
 
 
 }

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do i get the transform of an object with raycasthit? 1 Answer

NullReferenceException when using raycast 1 Answer

Unable to apply "transform" on gameobject from Raycast 1 Answer

Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer

Raycast object as far as possible? 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