Moving Objects in Unity: Unterschied zwischen den Versionen

Aus hyperdramatik
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 43: Zeile 43:
'''C) RigidBody.AddForce'''
'''C) RigidBody.AddForce'''


'''RigidBody:''' Adding a Rigidbody component to an object will put its motion under the control of Unity's physics engine, which are a long list of physical properties such as acceleration, mass, gravity, inertia etc, as well as the capacity to react to other rigid bodies (for instance, collide).  
'''RigidBody:''' Adding a Rigidbody component to a GameObject will put its motion under the control of Unity's physics engine, which is a long list of physical properties such as acceleration, mass, gravity, inertia, etc, as well as the capacity to react to other rigid bodies (for instance, collide).  


'''RigidBody.AddForce:''' Adds a force vector to the class Rigidbody. Force can be applied only to an active Rigidbody. If a GameObject is inactive, AddForce has no effect. Specifying the ForceMode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change.
'''RigidBody.AddForce:''' Adds a force vector to the class Rigidbody. Force can be applied only to an active Rigidbody. If a GameObject is inactive, AddForce has no effect. Specifying the ForceMode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change.

Version vom 23. Oktober 2019, 14:31 Uhr

Unity : How to move an object in 3D space?

There are different ways to move an object within 3D space with code. Here we go over 5 options.

A) transform.position

Transform: a Class which contains the information of position, rotation and scale of an object.

transform.position: The position property of a GameObject's Transform. Assigns the object a new position.

Vector: A Class refering to a mathematical entity commonly drewn as an arrow in two or three dimensional space, which has a a magnitude (size or length) and a direction (angle θ).

Vector3: is a Unity C# struct with given x, y & z components, representing points and vectors in 3D space. In other words, a group of Vectors.

example 1)

private void Update()
   {
       transform.position += transform.forward * Time.deltaTime; 
   }
  • the “f” is there to state that the variable 0.01 is of type float.

example 2)

    void Update()
   {
     transform.position += new Vector3(0, 0, 0.01f);
   }
  • Vector3.forward is the unit vector defined by (0, 0, 1)
  • Time.deltaTime is a variable used so velocity is consistent across different frame rates.


B) transform.Translate

Similar to transform.position, but instead of assigning the GameObject a new position, it gives the GameObject an amount to move, data contained in a Vector3 struct.

  private void Update()
   {
       transform.Translate(Vector3.forward * Time.deltaTime);
   }


C) RigidBody.AddForce

RigidBody: Adding a Rigidbody component to a GameObject will put its motion under the control of Unity's physics engine, which is a long list of physical properties such as acceleration, mass, gravity, inertia, etc, as well as the capacity to react to other rigid bodies (for instance, collide).

RigidBody.AddForce: Adds a force vector to the class Rigidbody. Force can be applied only to an active Rigidbody. If a GameObject is inactive, AddForce has no effect. Specifying the ForceMode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change.

public class RigidBodyAddForce : MonoBehaviour
{
   public float forceMult = 200; //velocity float multiplier
   private Rigidbody rb; //declarate Class Rigidbody

   private void Awake()
   {
       rb = GetComponent<Rigidbody>();
   }

   private void Update()
   {
       rb.AddForce(transform.up * forceMult * Time.deltaTime);
   }
}