zoukankan      html  css  js  c++  java
  • Unity3D鼠标控制摄像机“左右移动控制视角+WASD键盘控制前后左右+空格键抬升高度”脚本

    //键盘控制
    using
    UnityEngine; using System.Collections; public class CameraControl : MonoBehaviour { // Use this for initialization private GameObject gameObject; float x1; float x2; float x3; float x4; void Start () { gameObject = GameObject.Find ("Main Camera"); } // Update is called once per frame void Update () { //空格键抬升高度 if (Input.GetKey (KeyCode.Space)) { transform.position = new Vector3(transform.position.x,transform.position.y + 1,transform.position.z); } //w键前进 if(Input.GetKey(KeyCode.W)) { this.gameObject.transform.Translate(new Vector3(0,0,50*Time.deltaTime)); } //s键后退 if(Input.GetKey(KeyCode.S)) { this.gameObject.transform.Translate(new Vector3(0,0,-50*Time.deltaTime)); } //a键后退 if(Input.GetKey(KeyCode.A)) { this.gameObject.transform.Translate(new Vector3(-10,0,0*Time.deltaTime)); } //d键后退 if(Input.GetKey(KeyCode.D)) { this.gameObject.transform.Translate(new Vector3(10,0,0*Time.deltaTime)); } } }

    鼠标移动视角

    using UnityEngine;
    using System.Collections;
     
     
    [AddComponentMenu("Camera-Control/Mouse Look")]
    public class MouseLook : MonoBehaviour {
     
        public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
        public RotationAxes axes = RotationAxes.MouseXAndY;
        public float sensitivityX = 15F;
        public float sensitivityY = 15F;
     
        public float minimumX = -360F;
        public float maximumX = 360F;
     
        public float minimumY = -60F;
        public float maximumY = 60F;
     
        float rotationY = 0F;
     
        void Update ()
        {
            if (axes == RotationAxes.MouseXAndY)
            {
                float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                
                transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
            }
            else if (axes == RotationAxes.MouseX)
            {
                transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
            }
            else
            {
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                
                transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
            }
        }
        
        void Start ()
        {
            // Make the rigid body not change rotation
            if (GetComponent<Rigidbody>())
                GetComponent<Rigidbody>().freezeRotation = true;
        }
    }
  • 相关阅读:
    交叉编译OpenCV的教程——基于aarch64-linux-gnu的交叉编译器
    Day01:我的Python学习之路
    将中文库导入到ARM板子中以解决中文显示乱码的教程
    Linux环境下挂载SD卡的教程
    Ubuntu下压缩与解压各种文件的命令
    Ubuntu14.04环境下Qt5.5以上版本无法输入中文的解决教程
    编程之美:队列中的最大最小值
    leetcode:Compare Version Numbers
    leetcode:Search for a Range
    csapp:无符号数可能造成的程序bug
  • 原文地址:https://www.cnblogs.com/h694879357/p/12557357.html
Copyright © 2011-2022 走看看