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 nadeemd · Jul 16, 2013 at 07:17 PM · modelpathfindingtank

How to make a tank move/follow a path?

So I have created a pathfinding program which finds a route from point A to point B which I'm sure most of you are familiar with. What I am trying to do is let my 3D tank model move along this path. My question is 1. how to make a tank move? (I've got the basics to this but tips would help) and 2. Is there some sort of scripting trick or something that allows the model to follow the path?

Thank you!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Narv · Jul 16, 2013 at 07:29 PM

Depending on how you wrote the pathfinding vs using an asset from the store such as A*. The free version of A* has an example for moving between nodes. You will most likely want to use a character controller and the SimpleMove (or move if you want) (http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.SimpleMove.html) method for the controller to move it between nodes. Or you can get the point that it's at, and the point that the tank is supposed to be moving to, and do a Vector3.Lerp(frompoint, topoint, speed) approach to move the object.

You may want to add a rigidbody and bounding volumes to handle the collision so you don't move through objects too.

Comment
Add comment · Show 5 · 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 nadeemd · Jul 16, 2013 at 07:45 PM 0
Share

okay so say I want to get from start point (2,0,-4) to (-1,0,4) end point at a speed of 5.5? how do I script for that??

avatar image Narv · Jul 17, 2013 at 10:01 AM 0
Share
 Vector3 start = new Vector3(2,0,-4);
 Vector3 end = new Vector3(-1,0,4);
 transform.position = Vector3.Lerp(start, end, Time.deltaTime * 5.5f);

$$anonymous$$eep in $$anonymous$$d however that you still need bounding volumes to avoid collision between the character and any objects / other characters. if you have pathfinding like A* and wanted to use the Simple$$anonymous$$ove function ins$$anonymous$$d you can't do from point a to point b, you would just do a charcontroller.Simple$$anonymous$$ove(charcontroller.transform.forward * 5.5f); and you would just send it forward between nodes in a graph.

avatar image nadeemd · Jul 17, 2013 at 02:20 PM 0
Share

okay, this is really helping me but I need a few more things...I'm getting an error that the name charcontroller does not exist?? If this helps, I'm using the pathfinder that AngryAnt created. All I'm looking for is a script trick that makes the car move from the starting "$$anonymous$$ySeeker" to the end point "$$anonymous$$yTarget". When I press play, it finds a path that is shown with a white line..I hope this information helps you help me :) !

avatar image Narv · Jul 17, 2013 at 10:34 PM 0
Share

Sorry if I didn't make that portion clear, "charcontroller" was just a temp variable I made up as a reference to the CharacterController. I unfortunately am not sure how the AngryAnt pathfinder works. I assume that the point $$anonymous$$ySeeker and $$anonymous$$yTarget are the start and end with a series of waypoints in between. Are you aware of any way (via the documentation) to get a list of all the waypoints the pathfinder calculates? Here is an example i have from using the A* Path asset (free version) from the store:

 public class $$anonymous$$oveController : $$anonymous$$onoBehavior {
    private Seeker seeker;
    private float speed = 100;
    private CharacterController controller;
    private Path path;
    private float nextWaypointDistance = 3;
    private int currentWaypoint;
 
    void Start()  {
         seeker = GetComponent<Seeker>(); // this is from the package, a script they use for attaching an object to a path
         controller = GetComponent<CharaceterController>();
    }
 
 
    void Update()  {
 
    if(Input.Get$$anonymous$$ouseButtonDown(0)) //what triggers a new path? i put a mouse down.. but can be whatever.
    {
       Vector3 newpoint = new Vector3(x,y,z); // this im leaving blank, this would be the value of your $$anonymous$$yTarget variable, however it is you are calculating that, put that here.
       seeker.StartPath($$anonymous$$yStart, $$anonymous$$yTarget, OnPathComplete); // this is the function you call when calculating the path, this will change based on your AngryAnt code.  start a path from $$anonymous$$yStart -> $$anonymous$$yTarget and it sets a callback for a function to call when the path is complete, i call this "OnPathComplete" but can be called whatever
     } // end if
   } // end mouse click code
 
   // if there is no path to follow, end the update, not advised if you have other non-path related code in this update function
   if(path == null)
     return;
 
    // if you are at the last point in the waypoint graph, end the update since you have no more paths to follow
   if(currentWaypoint >= path.vectorPath.Count)
       return;
 
    // Direction to the next waypoint
    Vector3 direction = (path.vectorPath[currentWaypoint] - transform.position).normalized; // this gets the direction from point A to point B and gets a vector to rotate towards
     direction *= speed * Time.deltaTime;
     controller.Simple$$anonymous$$ove(direction);
 
     // rotate the tank in the direction you want it to move so it doesn't appear to glide
     Vector3 rotation = new Vector3(x,y,z); // xyz values from the vectorPath
      transform.rotation = Quaternion.Slerp(...); // rotate from position to new position over a time.
 
      // check if near the next waypoint, if we are, go to the next waypoint
     if(Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance){
     currentWaypoint++;
     return;
    }
 } // end update function
 
 
 void OnPathComplete (Path p) {
    path = p;
    currentWaypoint = 0;
 }
 
 } // end class


That is kind of long but it should be able to help you. I tried to comment the important parts and i got lazy towards the end rewriting the code so hopefully it makes sense the logic flow behind it.

avatar image Narv · Jul 17, 2013 at 10:34 PM 0
Share

Forgot to add, you need to put

 using Pathfinding;

At the top too

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to rotate at the same rpm wheels of a tank? 1 Answer

How to turn a tank? 1 Answer

Why Is OnMouse Not Working Correctly? 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