代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateDamoScripts : MonoBehaviour
{
public Transform tTransform;
public Transform aorientationPos;
public Transform pointOnePos;
private void Update()
{
UpdateRotate(pointOnePos.position, aorientationPos.position);
}
public void UpdateRotate(Vector3 pointOne, Vector3 orientationPos)
{
var vect = AxisLookAt(tTransform, orientationPos, Vector3.forward);
tTransform.rotation =
Quaternion.Euler(
vect);
ModificationAxisLookAt(tTransform, pointOne, Vector3.down);
}
Vector3 AxisLookAt(Transform trSelf, Vector3 lookPos, Vector3 directionAxis)
{
var rotation1 = trSelf.rotation;
var rotation = rotation1;
var targetDir = lookPos - trSelf.position;
//指定哪根轴朝向目标,自行修改Vector3的方向
var fromDir = rotation1 * directionAxis;
//计算垂直于当前方向和目标方向的轴
var axis = Vector3.Cross(fromDir, targetDir).normalized;
//计算当前方向和目标方向的夹角
var angle = Vector3.Angle(fromDir, targetDir);
//将当前朝向向目标方向旋转一定角度,这个角度值可以做插值
rotation1 = Quaternion.AngleAxis(angle, axis) * rotation;
// trSelf.rotation = rotation1;
return rotation1.eulerAngles;
}
void ModificationAxisLookAt(Transform trSelf, Vector3 lookPos, Vector3 directionAxis)
{
var rotation1 = trSelf.rotation;
var rotation = rotation1;
var targetDir = lookPos - trSelf.position;
//指定哪根轴朝向目标,自行修改Vector3的方向
var fromDir = rotation1 * directionAxis;
//计算垂直于当前方向和目标方向的轴
var axis = Vector3.Cross(fromDir, targetDir).normalized;
//计算当前方向和目标方向的夹角
var angle = Vector3.Angle(fromDir, targetDir);
//将当前朝向向目标方向旋转一定角度,这个角度值可以做插值
rotation1 = Quaternion.AngleAxis(angle, axis) * rotation;
trSelf.rotation = rotation1;
}
}