Unity3D如何实现游戏对象跟随鼠标旋转

这篇博客介绍了如何在Unity3D中实现游戏对象随着鼠标移动而旋转。通过编写Global_TargetFollowMouseRotate控制代码,并将其添加到目标物体上,结合地板的LayerMask设置,可以实现对象的平滑旋转效果。
摘要由CSDN通过智能技术生成

原文地址: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
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值