Unity 3d角色展示脚本(旋转 平移 缩放)展示界面

不考虑性能 很简陋的一个功能,主要是用于角色渲染的观察用,比simplecontroller要好用一点

csharp 复制代码
using System;
using UnityEngine;

public class CharacterViewer : MonoBehaviour
{
    public Transform target; // 人物模型的Transform
    public float rotationSpeed = 5f;
    public float zoomSpeed = 1f;
    public float panSpeed = 0.001f;
    private Vector3 lastMousePosition;
    

    void Update()
    {
        // 旋转
        if (Input.GetMouseButton(0))
        {
            float mouseX = -Input.GetAxis("Mouse X");
            // float mouseY = Input.GetAxis("Mouse Y");
            target.Rotate(Vector3.up, mouseX * rotationSpeed, Space.World);
            // target.Rotate(Vector3.right, -mouseY * rotationSpeed, Space.Self);
        }

        // 缩放
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (scroll != 0)
        {
            Vector3 zoomDirection = transform.forward;
            transform.position += zoomDirection * (scroll * zoomSpeed);
        }

        // 平移
        if (Input.GetMouseButtonDown(2))
        {
            lastMousePosition = Input.mousePosition;
        }
        if (Input.GetMouseButton(2))
        {
            Vector3 delta = Input.mousePosition - lastMousePosition;
            Camera.main.transform.Translate(-delta.x * panSpeed, -delta.y * panSpeed, 0);
            lastMousePosition = Input.mousePosition;
        }
    }
}
相关推荐
xexpertS1 小时前
队列积压治理:如何避免异步系统出现难以恢复的消息积压
java·开发语言
阿维的博客日记9 小时前
MultipartFile 是不是表示仅仅是一个分片?
java·后端·spring·multipartfile
程序员无隅10 小时前
Coding Agent 为什么压缩上下文后还能继续工作?上下文模块设计拆解
java·开发语言·数据库
Python+9910 小时前
Java 枚举类(Enum)详解:从基础到高级应用
java·开发语言·python
二炮手亮子10 小时前
浅记java线程池
java·开发语言
一路向北North10 小时前
Spring Security OAuth2.0(23):分布式系统授权-转发明文给微服务
java·spring·微服务
爱吃牛肉的大老虎12 小时前
rust基础之环境搭建
java·开发语言·rust
tellmewhoisi13 小时前
多版本共用redis的token有效期校验(过期重新登录)
java·redis·缓存
疯狂打码的少年13 小时前
【软件工程】结构化设计:模块独立性与耦合内聚
java·开发语言·笔记·软件工程
乐观的Terry13 小时前
3、数据库设计与领域实体
java·数据库·spring boot·spring cloud·ai编程