使用射线和DOTween插件实现点击物体镜头移到物体的前方。
使用射线和DOTween插件实现点击物体镜头移到物体的前方。
实现功能:
1、镜头移动
2、镜头旋转
3、鼠标点击获取物体
4、镜头移动到物体前方(非世界坐标的前方)
前期准备:
1、 任意版本DOTween插件
2、 在场景中随意添加几个物体,保证摄像机能看到即可
3、 在添加脚本到摄像机组件
编写脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;public class DotweenCam : MonoBehaviour
{Ray ray;GameObject targetGameObject;void Update(){raySystem();}void raySystem(){if (Input.GetMouseButtonDown(0)){ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hitInfo;if (Physics.Raycast(ray, out hitInfo)){ targetGameObject = hitInfo.collider.gameObject;Debug.Log(targetGameObject.name);clickMove();}}}void clickMove() {transform.DOMove(targetGameObject.transform.forward*-5+targetGameObject.transform.up*5+targetGameObject.transform.position, 1);transform.DORotate(new Vector3(30, targetGameObject.transform.eulerAngles.y, 0), 1);}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
