[Unity3D] 视角旋转学习笔记

3 篇文章 0 订阅

目录

前言

一、基础旋转

1.1  相机环绕角色旋转

Declaration

Description

1.2  LookAt()函数

二、通过鼠标拖拽距离来旋转视角


前言

学习笔记,仅供学习,不做商用,如有侵权,联系我删除即可。

参考博文:

Unity 自由视角的惯性旋转icon-default.png?t=N3I4https://haiyue.blog.csdn.net/article/details/123297435

一、基础旋转

1.1  相机环绕角色旋转

使用Transform.RotateAround,axis为轴,方向旋转

Declaration

public void RotateAround(Vector3 point, Vector3 axis, float angle);

Description

Rotates the transform about axis passing through point in world coordinates by angle degrees.

This modifies both the position and the rotation of the transform.

using UnityEngine;

//Attach this script to a GameObject to rotate around the target position.
public class Example : MonoBehaviour
{
    //Assign a GameObject in the Inspector to rotate around
    public GameObject target;

    void Update()
    {
        // Spin the object around the target at 20 degrees/second.
        transform.RotateAround(target.transform.position, Vector3.up, 20 * Time.deltaTime);
    }
}

1.2  LookAt()函数

根据官方的文档描述,该函数的功能是,旋转自身,使得当前对象的正z轴指向目标对象target所在的位置。

而对于worldUp的描述是,在完成上面的旋转之后,继续旋转自身,使得当前对象的正y轴朝向与worldUp所指向的朝向一致.

可以使用LookAt函数修正,让相机正确地朝向角色

二、通过鼠标拖拽距离来旋转视角

按下鼠标右键时,每一帧都获取鼠标的位置,通过两帧之间的鼠标位置之差,即可求得两帧之间的鼠标速度。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerTransform : MonoBehaviour
{
    public Transform HeadTransform;
    private float CamDepthSmooth = 200f;
    private Vector3 RelativePos;

    // Start is called before the first frame update
    void Start()
    {
        RelativePos = transform.position - HeadTransform.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = HeadTransform.position + RelativePos;
        DragToRotateView_Distance();
        // 视角的拉近和放远
        if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) ||
            (Input.mouseScrollDelta.y > 0) && Camera.main.fieldOfView <= 80)
        {
            Camera.main.fieldOfView += Input.mouseScrollDelta.y * CamDepthSmooth * Time.deltaTime;
        }
    }

    /*            通过鼠标拖拽距离来旋转视角            */
    Vector3 Point1;
    Vector3 Point2;
    // 旋转每度在一帧中需要拖拽的距离
    int DragDistancePerAngle = 8;
    void DragToRotateView_Distance()
    {
        
        if (Input.GetMouseButtonDown(1))          // 按下右键的瞬间记录起始位置
        {
            Point1 = Input.mousePosition;
            //StartPoint = Point1;
        }

        if (Input.GetMouseButton(1))              // 按下右键每一帧都执行
        {
            Point2 = Input.mousePosition;
            float dx = Point2.x - Point1.x;
            float dy = Point2.y - Point1.y;

            float AngleX = dx / DragDistancePerAngle;
            float AngleY = dy / DragDistancePerAngle;

            transform.RotateAround(HeadTransform.position, Vector3.up, AngleX);
            transform.RotateAround(HeadTransform.position, -transform.right, AngleY);
            transform.LookAt(HeadTransform);

            RelativePos = transform.position - HeadTransform.position;

            Point1 = Point2;
            Point2 = Vector3.zero;
        }
    }
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DayDayUp..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值