简体   繁体   中英

Unity Basic Functions Explanation

As mentioned in the title I would like if possible someone to explain to me a few basic things.

1) Rigibody.AddForce(Vector3(50, 50, 50) * speed) ;

Why does an object move when you give it coordinates ? And it even goes faster if you multiply it as shown ? I guess it is the same for:

transform.Translate(-speed * Time.deltaTime * Input.GetAxis("Horizontal"), 0, 0);

The whole point is: I pass coordinates and it moves to the right direction with the calculated value

(Coordinates * Speed * Something_Else). How come ??

I would really appreciate any answers.

Update 1: If I multiply a Vector3 by some float number it will also return a Vector3 but the speed of the object will increase. I don't understand why. The coordinates are just coordinates. How come an object moves faster with "bigger" coordinates ?

float speed = 50.00f; Rigibody.AddForce(Vector3(50, 50, 50) * speed) float speed = 50.00f; Rigibody.AddForce(Vector3(50, 50, 50) * speed) ;

First, concepts:

  1. Transform: Every object in scene has a transform, which represents a position,rotation and scale
  2. Rigidbody: Part of the physics of unity, represents an object which physics apply

So, an object with transform but no rigidbody will not be affected by gravity, collisions or forces

You can actually "emulate" physics without a rigidbody by changing position of the translate according to time, but it would not interact correctly with the rest of the physics system (that's the main different between your 2 implementations, which are not the same but may look similar)

Then, both work the same way, you pass a Vector which represents the force/distance in each axys:

myRigibody.AddForce(Vector3(2,0,0)); //Force of 2 applied in x axys
myTransform.Translate(Vector3(2,0,0)); //translate 2 position in x axys

So, commonly, you use a normalize vector (module equals 1) and multiply it by your speed (or force technically):

//first vector is direction (x axys) and multiply by speed (2)
myRigibody.AddForce(Vector3(1,0,0)*2); 

Think of a vector not only as a coordinate in space, but also a direction or a distance in that direction.

Answer to Update
In given example, we have Vector3(50,50,50) when talking about Force, that represent a force in each axys:

Rigidbody.AddForce(Vector3(50, 40, 30));

This method, will apply to the object a initial force (which will accelerate it) of 50 units in each axys, this is, the object will have a force of 50 in x axys, 40 in y axys and 30 in z axys, making it accelerate in all directions

Multiplying a vector by an escalar will return a Vector3 where all components will be multiplyes, so Vector3(50,40,30)*2 will return the same as Vector3(100,80,60) , making the object accelerate in the same direction but with double of "speed"

Your main problem here is not about programming, but some basic Math/Physics understanding and how vectors are used, as i said before, a vector is not the same as a coordinate,in a nutshell, a coordinate represents a point in space, while a vector represents a direction and "distance", so, using as a coordinate, [1,0,0] will represent 1 meter on the positive x axys from origin, while as a vector, it represents the distance and direction from [0,0,0] to [1,0,0].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM