网站营销的优势,企业网站模板html下载,如何改wordpress主页,大学生创新创业大赛ppt模板要通过鼠标控制并模拟人物移动和转换视角#xff0c;将会使用射线检测、鼠标点击和鼠标水平移动#xff0c;配合物体旋转和移动方法共同实现。
首先搭建个由一个Plane地板和若干cube组成的简单场景#xff1a; 其次创建一个Capsule作为移动物体#xff0c;并把摄像头拉到该…要通过鼠标控制并模拟人物移动和转换视角将会使用射线检测、鼠标点击和鼠标水平移动配合物体旋转和移动方法共同实现。
首先搭建个由一个Plane地板和若干cube组成的简单场景 其次创建一个Capsule作为移动物体并把摄像头拉到该物体中。
创建以下脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CameraController : MonoBehaviour
{private Vector3 targetPosition; // 目标位置private float moveSpeed 5f; // 摄像头移动速度private bool isMoving false; // 标记物体是否正在移动public float rotateSpeed 3f; // 摄像头旋转速度public void Update(){if (Input.GetMouseButton(0)){//视角旋转transform.Rotate(Vector3.up, Input.GetAxis(Mouse X) * rotateSpeed);//检测射线获取目标点Ray ray Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo new RaycastHit();if (Physics.Raycast(ray, out hitInfo)){ if (hitInfo.collider.name Ground){ targetPosition hitInfo.point;targetPosition.y transform.localPosition.y;isMoving true;}Debug.DrawLine(ray.origin, hitInfo.point, Color.blue);}//让物体移动到目标位置if (isMoving){MoveObject();}}}private void MoveObject(){// 使用插值函数逐渐将物体移动到目标位置transform.position Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);// 检查是否到达目标位置if (transform.position targetPosition){isMoving false;}}
}把脚本拉到移动物体中就可以了。
实际效果如下 Unity鼠标模拟人物走动转换视觉