原文地址:http://gad.qq.com/article/detail/46250
1、编写控制代码:Global_TargetFollowMouseRotate
2、将该代码添加给需要跟随随便旋转的物体上
3、给地板的Layer添加LayerMask,同时给地板的Layer指定为LayerMask,同时需要给物体本身的脚本whatIsGround选择LayerMask
/***
* Title:"智慧工厂" 项目
* 主题:控制物体跟随鼠标旋转
* Description:
* 功能:XXX
* Date:2018
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Global
{
public class Global_TargetFollowMouseRotate : MonoBehaviour
{
[SerializeField] LayerMask whatIsGround; //地板的Layer设置为layerMask
public Camera mainCamera; //主摄像机
private void Update()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
//Debug.Log(ray);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 200, whatIsGround))
{
Vector3 target = hitInfo.point;
target.y = transform.position.y;
transform.LookAt(target);
}
}
}//Class_end
}