Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 StormMuller · Jul 09, 2016 at 02:38 PM · c#physicsrigidbodyvector3vector

Rigidody.velocity = Direction * speed; How to get direction?

So I have a navigation script, I can't use the nav mesh and nav mesh agent due to technical reasons. But that's ok. Anyway I have a list of vector3s as waypoints that my sphere needs to pass through. I want it to roll so I need to use the rigidbody and physics engine. Anyway my code works, mostly, the only problem is that my sphere accelerates and decelerates when moving from waypoint to waypoint. I don't want this I want the speed to remain constant. My scene just has the ball and a plane.Here's my code I've attached to my sphere:

 using System.Collections.Generic;
 
 public class Nav : MonoBehaviour {
 
     [HideInInspector]
     public bool isNavigating;
     public List<Vector3> points;
 
     Rigidbody playerRB;
     int currentPoint, lastPoint;
     float speed = 5f;
 
     bool isMoving = false;
 
     // Use this for initialization
     void Start ()
     {
         // set in instaniate script
         points = new List<Vector3>()
         {
             new Vector3(5, 0, 1),
             new Vector3(5, 0, 5),
             new Vector3(8, 0, 5),
             new Vector3(8, 0, 8),
             new Vector3(14, 0, 8),
             new Vector3(14, 0, 14),
             new Vector3(20, 0, 14)
         };
 
         isNavigating = true;
 
         // leave here
         currentPoint = 0;
         lastPoint = points.Count;
         playerRB = GetComponent<Rigidbody>();
         
     }
     
     void FixedUpdate ()
     {
         if (!isNavigating) return;
 
         if (new Vector3(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y), Mathf.Round(transform.position.z))  == points[currentPoint])
         {
             if (!(currentPoint + 1 > points.Count - 1))
             {
                 currentPoint++;
             }
             else
             {
                 isNavigating = false;
                 playerRB.velocity = Vector3.zero;
                 return;
             }
         }
 
         // Navigate
         Vector3 direction = (points[currentPoint] - transform.position).normalized;
         playerRB.velocity = direction * speed;
 
         
     }
 }
 


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
1
Best Answer

Answer by Charmind · Jul 21, 2016 at 09:23 PM

If i got it right - you can divide Vector3 that stands for direction with it's highest value so:

velocity = (direction /Mathf.Max(Mathf.Max(direction[0], direction[1]),direction[2]) )*speed;

it will give you speed of 1 in one direction and some lower values in other determined by ratio AKA constant speed

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Set Velocity at relative position. 2 Answers

Rigidbody character controller can't walk on stairs 0 Answers

Vector3 is always subject to serious stutter in Translation 0 Answers

Punching objects (C#) 1 Answer

Rigidbody.SweepTest Hit Trigger Colliders 0 Answers


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