using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseRayPoint : MonoBehaviour
{
Vector3 target; // 跟踪目标
public float smoothTime = 5f; // 平滑时间
private Vector3 velocity = Vector3.zero;
GameObject mainRole;
public float speedMove = 10f;
private void Start()
{
mainRole = GameObject.Find("Cube");
}
void Update()
{
Ray oneRayMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit OneShotOBJ;
Physics.Raycast(oneRayMouse, out OneShotOBJ);
if (Input.GetMouseButtonDown(0))
{
Debug.Log(OneShotOBJ.point+"== "+ OneShotOBJ.transform.name);
target= OneShotOBJ.point;
}
if (target!=null)
{
FollowPointSmooth();
}
} //end update
void FollowPointSmooth()
{
计算新的位置
Vector3 targetPosition = target + new Vector3(0, 1, -1);//提供一个相对target.position的一个偏差
mainRole.transform.position = Vector3.SmoothDamp(mainRole.transform.position, targetPosition, ref velocity, smoothTime); //平滑移动到新的位置
mainRole.transform.LookAt(target + new Vector3(0, 1.5f, 0));
}
}//end class