banner



How To Set Velocity Unity

Unity Rigidbody Velocity

Introduction

When moving characters, vehicles, or other objects around your game physics speedily become an important factor. Unity comes with built in physics simulations which y'all can utilize to your GameObjects through the utilize of Rigidbody components. One of the most commonly used properties of Rigidbodies is velocity.

Furthermore, you can prepare Rigidbody velocity in ane of 2 ways. You can assign it directly by using `Rigidbody.velocity = Vector3`or by using Rigidbody.AddForce. When changing the velocity of your GameObjects it is recommended that you utilise Rigidbody.AddForce but there are circumstances where you may demand to modify the velocity directly. In this Tips and Tricks, we are going to talk about how you lot can assign velocity directly, what velocity means, and how it affects our GameObjects inside of Unity.

What is Rigidbody Velocity

Commencement, if you want to know how to use something the right way, information technology is best to understand what it is. There is no better place to commencement learning what something is than looking up the definition. According to Unity's Documentation on Rigidbody.velocity, velocity "represents the rate of alter of Rigidbody position". The Wikipedia entry for Velocity states "Velocity is equivalent to a specification of an object'south speed and direction of motion". Basically, for a GameObject to have velocity we must accept a speed and a management.

How Does the Rigidbody'due south Velocity Touch on Your GameObjects?

Unity'southward implementation of velocity requires that it be assigned to a Vector3 value only as we know a Vector3 is just a position with Ten, Y, and Z coordinates. How does supplying only a position translate into a direction and speed?

Well, for example, when you supply a Vector3 of (two,0,0), Unity will automatically convert this into the needed values. Our Vector3 of (ii,0,0) becomes travel in the direction of 2 X, 0 Y, and 0 Z. The values are read as units and are and then used to calculate our speed. Unity takes these units and converts them into units per 2nd. To sum up, this means our instance will caput in a line towards the X position of 2 at a speed of 2 units per 2d. If you are wondering what the distance of a unit is, Unity states that they are conceptualized as meters but really could be any measure of distance.

How to Use Rigidbody Velocity

For this demonstration we take set upwardly a unproblematic scene with a cube, a flooring, and a Cinemachine camera set to follow the cube. Nosotros have attached a Rigidbody to the cube so we can control its velocity and a bluish material to spice things upward a flake.

Create a script named VelocityController and attach it to the cube. Nosotros volition employ this to write some simple code to manipulate our Cube's velocity.

Within of our script, nosotros create 2 fields. Ane is to hold our Vector3 and is serialized so nosotros can edit it in the inspector, encounter why we declare this as a private field in our article on Encapsulation. The second is a reference to our Rigidbody which we initialize in our Showtime method.

For our Update method, nosotros volition check for UpArrow to be pressed, which will trigger the MoveTheCube method, or the Escape key, which triggers the ReturnToCenter method.

The MoveTheCube method will take our cube's velocity and set it to the ourVelocity Vector3 value nosotros gear up in Unity'due south inspector window.

Lastly, the ReturnToCenter method volition render the cube to the center of the screen and reduce the velocity to zilch using the Vector3.zero value, but in instance things get weird.

                using System.Collections; using System.Collections.Generic; using UnityEngine;   public class VelocityController : MonoBehaviour {     [SerializeField]     private Vector3 ourVelocity;     private Rigidbody ourRigidbody;     void Start()     {         ourRigidbody = GetComponent<Rigidbody>();     }     void Update()     {         if(Input.GetKeyDown(KeyCode.Escape))         {             ReturnToCenter();         }         if(Input.GetKeyDown(KeyCode.UpArrow))         {             MoveTheCube();         }     }       private void MoveTheCube()     {         ourRigidbody.velocity = ourVelocity;     }     private void ReturnToCenter()     {         ourRigidbody.position = ourRigidbody.velocity = Vector3.zero;     }   }                              

Rigidbody Physics

Our Rigidbody handles all of the physics simulations we need for this example for instance it will handle gravity and drag which will lower the velocity over fourth dimension as our object is affected by these forces.

Back in the Unity editor, set the Y value of Our Velocity to 100 and hit play to start the game. Press the up cardinal and yous will see that our object accelerates upwards for a skilful while earlier gravity takes effect and our cube begins to fall again. If you sentinel the position values inside of the cube's transform component, yous will run across that the cube'due south Y value increases quickly at beginning but starts to slow somewhen stopping around a value of 508. It takes almost 10 seconds to accomplish its highest position.

At first the cube is accelerating at 100 Y units per second just every bit gravity is taking outcome on the cubes velocity that rate slows. We can stop gravity from affecting our velocity by unchecking the Use Gravity option in the Rigidbody for our cube. If yous run the game without gravity and hit the up key, at 10 seconds you will see that the cube now has a position Y value of thou.

We tin hitting escape to return our cube to the center. To encounter how irresolute all the Vector3 values affects our velocity, change all values to 10 and recollect to turn gravity back on. At present when we hit our upward arrow the cube volition jump diagonally beyond our game earth. Go along to play with these values and come across the different results you can come up with.

And now yous are prepare to get-go using Rigidbody velocity in your ain games created in Unity. Experience free to clone the repo for this demonstration from our GitHub. Thanks for stopping by. Stick around and check out more of our tutorials or posts like our piece on How to Make a Multiplayer Pong game for Android. Too, go out a annotate telling the states what you lot liked or did not like well-nigh the tutorial. Was it piece of cake to follow along? What exercise you want to learn next? Equally always, check out some of our published apps below.

Source: https://www.monkeykidgc.com/2021/01/unity-rigidbody-velocity.html

0 Response to "How To Set Velocity Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel