当物体带有刚体组件时并有持续的运动或者旋转时,使用刚体组件的MovePositon,MoveRotation方法;
using UnityEngine;
using System.Collections;
public class MoveAndRotationTest : MonoBehaviour
{
public Transform player;
public Transform enemy;
private Rigidbody rgd;
// Use this for initialization
void Start()
{
//player.eulerAngles = new Vector3(45, 45, 45);
//player.rotation = Quaternion.Euler(new Vector3(45, 45, 45));
rgd = player.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//Vector3 dir = enemy.position - player.position;
//dir.y = 0;
//Quaternion target = Quaternion.LookRotation(dir);
//player.rotation = Quaternion.Slerp(player.rotation, target, Time.deltaTime);
//rgd.position = player.position + Vector3.forward*Time.deltaTime;
//rgd.MovePosition(player.position + Vector3.forward * Time.deltaTime*5);
Vector3 dir = enemy.position - player.position;
dir.y = 0;
Quaternion target = Quaternion.LookRotation(dir);
//rgd.rotation = Quaternion.Slerp(player.rotation, target, Time.deltaTime);
//rgd.MoveRotation(target);
rgd.MoveRotation(Quaternion.Slerp(player.rotation, target, Time.deltaTime));
}
}