Unity3D实现鼠标控制视角转动

前面,学了物体的移动功能,现在来学一下C#实现鼠标控制摄像机(视角)移动。

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的嘉兴网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

代码如下:

C#脚本(在Unity 5.5.1 下能运行):

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


public class MouseView : MonoBehaviour {

 public enum RotationAxes
 {
 MouseXAndY = 0,
 MouseX = 1,
 MouseY = 2
 }

 public RotationAxes m_axes = RotationAxes.MouseXAndY;
 public float m_sensitivityX = 10f;
 public float m_sensitivityY = 10f;

 // 水平方向的 镜头转向
 public float m_minimumX = -360f;
 public float m_maximumX = 360f;
 // 垂直方向的 镜头转向 (这里给个限度 最大仰角为45°)
 public float m_minimumY = -45f;
 public float m_maximumY = 45f;

 float m_rotationY = 0f;


 // Use this for initialization
 void Start () {
 // 防止 刚体影响 镜头旋转
 if (GetComponent()) {
  GetComponent ().freezeRotation = true;
 }
 }

 // Update is called once per frame
 void Update () {
 if (m_axes == RotationAxes.MouseXAndY) {
  float m_rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * m_sensitivityX;
  m_rotationY += Input.GetAxis ("Mouse Y") * m_sensitivityY;
  m_rotationY = Mathf.Clamp (m_rotationY, m_minimumY, m_maximumY);

  transform.localEulerAngles = new Vector3 (-m_rotationY, m_rotationX, 0);
 } else if (m_axes == RotationAxes.MouseX) {
  transform.Rotate (0, Input.GetAxis ("Mouse X") * m_sensitivityX, 0);
 } else {
  m_rotationY += Input.GetAxis ("Mouse Y") * m_sensitivityY;
  m_rotationY = Mathf.Clamp (m_rotationY, m_minimumY, m_maximumY);

  transform.localEulerAngles = new Vector3 (-m_rotationY, transform.localEulerAngles.y, 0);
 }
 }
}

调用时,只需把该脚本绑定给物体即可。这里是绑定摄像机,以摄像机为第一人称视角转动。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


名称栏目:Unity3D实现鼠标控制视角转动
网页URL:http://bzwzjz.com/article/pcpojs.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 商城网站建设 成都网站建设 成都网站建设 网站建设公司 网站设计 成都响应式网站建设 成都网站建设公司 响应式网站设计方案 成都网站建设公司 成都响应式网站建设公司 成都网站制作 成都企业网站建设 手机网站设计 成都网站制作 成都定制网站建设 成都网站设计 成都网站制作 重庆外贸网站建设 成都网站设计 重庆企业网站建设 成都品牌网站设计 定制网站建设