學習視頻是b站上的來自程序員的暴擊博主
- unity中剛體的作用
①會於場景中的模型物體發生碰撞;
②會受重力影響(到達場影邊緣外,會下落)。 - 使用剛體移動物體
- 代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
private Rigidbody rigi;
public float speed=5.0f;
// Start is called before the first frame update
void Start()
{
rigi = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
rigi.MovePosition(transform.position + Vector3.back * Time.deltaTime * speed);
}
}
- 代碼效果
cube會一直往前走,直到從平面掉落。
- 剛體移動的方法
① Rigidbody. Moveposition( Vector3):使用剛體移動物體的位置。
使用剛體移動物體,物體是根據世界座標系的方向移動的。
使用剛體移動物體,物體會觸發物理相關的事件。
② Moveposition中的 Vector3要使用**“當前位置”+方向**的方式。
Transform. Position:屬性當前物體的位置。