using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityScript : MonoBehaviour
{
public Transform target;
// Update is called once per frame
void Update()
{
CreateRotation();
}
void CreateRotation()
{
float speed = 5.0f; //设置移动速度
Vector3 relativePos = target.position - transform.position; // 物件朝target向量
Quaternion rotation = Quaternion.LookRotation(relativePos); // Vector3转换为四元数
Quaternion currentRotation = transform.localRotation; // 初始转向
transform.localRotation = Quaternion.Slerp(currentRotation, rotation, Time.deltaTime); // 下一帧转向为对初始旋转和目标旋转求球形差值
transform.Translate(0, 0, 3 * Time.deltaTime * speed);
}
}
具体可见教程视频,仅作记录。
参考资料:C# 中级编程 | Unity 中文课堂,15. 四元数