Unity obtains the direction of the object's own coordinate axis and the method of moving along that direction

Sometimes for a game object, it needs to move along its own coordinate axis direction, so how to get its own coordinate axis direction first?

Obtaining its own coordinate axis direction can be obtained through the transform component (just add a negative sign to the negative direction)

 Vector3 moveDirection = transform.right; Get the direction of its own x-axis

 Vector3 moveDirection = transform.forward; Get the direction of its own z-axis

 Vector3 moveDirection = transform.up; Get the direction of its own y-axis

The following is an example, assuming that a Cylinder cylinder object is created in the scene, as follows: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MoveControl : MonoBehaviour {     private float speed = 1.0f;     // Use this for initialization     void Start () {     }     // Update is called once per frame     void Update () {         Vector3 moveDirection = transform.right;         transform.position += moveDirection * Time.deltaTime * speed;        // transform.Translate(Vector3.right * Time. deltaTime * speed, Space. Self);     } }
 

 


     

    






 

That is, you can directly change the reference coordinate system parameter to Space.Self in the translation function, and the test results are the same.

Guess you like

Origin blog.csdn.net/zhouyuwen1/article/details/130800928