Unity sports related learning records

Time:2022-11-23

Some articles in Unity are learning records, and the understanding is not very deep. They are mainly used for self-study summary. If there are any mistakes, please lightly spray.


1. Realize the movement of objects through the position attribute

1. Call Vector3.movetowards in the update loop to directly set the position of the object

input parameters end position, speed
scenes to be used Know the coordinates of the end point
type of exercise uniform motion

Sample code:

//target [Vector3] The position of the end point
//Speed ​​[float] The speed of uniform movement, which can be set to 5.0f  

void Update(){
    transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * Speed);
}

2. Call Vector.Lerp in the update loop to directly set the position of the object

input parameters end position, speed
scenes to be used Know the coordinates of the end point
type of exercise Movement will become slower and slower
//target [Vector3] The position of the end point
//Speed ​​[float] The speed of uniform movement, which can be set to 5.0f   

void Update(){
    transform.position = Vector3.Lerp(transform.position, target, Time.deltaTime * Speed);
}

3. The translate method of transform

input parameters Direction vector of motion, velocity
scenes to be used I don’t know the coordinates of the end point, so I will keep moving
type of exercise uniform motion
//direction [Vector3] The direction of motion, can be set to (0,0,1)
//Speed ​​[float] The speed of uniform movement, which can be set to 5.0f 

void Update(){
    transform.Translate(direction * Speed * Time.deltaTime);
}

2. Based on the rigid body, the motion of the object is realized through force and acceleration

learning