• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by rangapraveen · Aug 01, 2013 at 12:00 PM · waypointwaypointswaypoint system

How will i make my objects/model(say a car) face the way points and travel towards it,instead of blindly following it?

i'm a newbie and i'm trying out some stuff with a few models. I have a car model and when i was trying to use waypoints to make it follow throught those points ,my car isn't facing towards it . Instead it is trying to just move towards the waypoint no matter what side it is turned .

I want the car's front side to be facing towards the waypoint it is trying to reach.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by save · Aug 01, 2013 at 12:26 PM

To get a direction from two positions the basic calculation is like this:

 target.position - transform.position

To set a new look rotation from a direction over time (with linear interpolation):

 var rotationSpeed : float = 10.0;
 var direction : Vector3 = Vector3(target.position - transform.position);
 var newLookRotation : Quaternion = Quaternion.LookRotation(direction);

 transform.rotation = Quaternion.Slerp(transform.rotation, newLookRotation, rotationSpeed*Time.deltaTime);

There's also a function called RotateTowards which can be useful. If you don't want to rotate over time and just set the rotation immediately, you can use LookAt.

 transform.LookAt(target);

So what you basically have to do is to assign each next waypoint as your target when you've finished the current. As you haven't posted any script, an example:

 var allWayPoints = Transform[];
 var currentWayPoint : int;
 var target : Transform;

 // Called when reaching current waypoint
 function FinishedWaypoint () {
     // Increase currentWayPoint by one and make it loop if exceeds length of all waypoints
     currentWayPoint++; currentWayPoint = currentWayPoint%allWayPoints.Length;

     // Set next waypoint as new target (to later rotate towards it)
     target = allWayPoints[currentWayPoint];
 }


On another note, I saw you had duplicate posts in the moderation queue. There might be a little bit of delay of the publication of your questions as you have to wait for approval from a moderator - just so you don't think that your question got lost.

Comment
Hamilcar-games

People who like this

1 Show 3 · 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 rangapraveen · Aug 01, 2013 at 12:33 PM 0
Share

thanx a lot , will give it a try ! :)

avatar image rangapraveen · Aug 01, 2013 at 02:04 PM 0
Share

thanks for ur help

 using UnityEngine;
 using System.Collections;
 
 public class waypoint : MonoBehaviour {
     
     public Transform[] waaypoint;
     public int speed=50;
     private int currentwaypoint;
     public Vector3 velocity;
     
     // Use this for initialization
     void Start () {
         //Debug.Log(currentwaypoint);
     
     }
     
     // Update is called once per frame
     void Update () {
         //Debug.Log(currentwaypoint);
         if(currentwaypoint< waaypoint.Length)
         {
              Vector3 target= waaypoint[currentwaypoint].position;
             Vector3 movedirection=target-transform.position;
          velocity=rigidbody.velocity;
             
             if(movedirection.magnitude<1)
             {
                 currentwaypoint++;
             }
             else
             {
                 velocity=movedirection.normalized*speed;
             }
                 
         }
         rigidbody.velocity= velocity;
             transform.LookAt(waaypoint[currentwaypoint].position);
         if(currentwaypoint == waaypoint.Length)
         {
             // ??
         }
     }
 }



this is the code that i used ....it works fine but my car still continues to move even after reaching the last waypoint.

moreover, i hav a simple terrain with small mountains , the car just after a few elevated movement moves through the mountain to the last waypoint.

how do i solve this ?

avatar image Hamilcar-games · Sep 12, 2017 at 10:15 AM 0
Share

thank you so much forr this

avatar image

Answer by Hamilcar-games · Sep 12, 2017 at 10:21 AM

who likes the c# from the answer code this is the code :

             Vector3 direction = targetPosition - transform.position;

             Quaternion newlook_rotation = Quaternion.LookRotation(direction);

             transform.rotation = Quaternion.Slerp(transform.rotation, newlook_rotation, rotation_speed * Time.deltaTime);
Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Easy WayPoint System - Character climbing at waypoint 0 Answers

How to play an animation with waypoints 1 Answer

Why isn't a Ai Waypoint Spawning??? 0 Answers

waypoint system help 1 Answer

How do you move a character to another waypoint after landing on a specific waypoint 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