zoukankan      html  css  js  c++  java
  • 塔防游戏的相机

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System;
    
    public class Cameracontroller : MonoBehaviour {
    
        public float mouseSpeed=0.05f;
        public float rotateSpeed=100;
        public float scrollwheelSpeed=200;
        public float moveSpeed=0.1f;
        public float lerpSpeed = 0.5f;
        public float lookSpeed=6;
        Vector3 mousePos;
        public static Action<GameObject> selectTower;
        public static Action<GameObject> exitSelectTower;
        [HideInInspector]
        public GameObject currSelectObj;
    
        void LateUpdate() {
    
            MoveRotation();
            DragMove();
            ScrollMove();
            CheckTower();
            MoveToTower();
            RotateToTower();
        }
        /// <summary>
        /// 鼠标右键控制移动旋转
        /// </summary>
        private void MoveRotation()
        {
            if (Input.GetKeyDown(KeyCode.Mouse1))
            {
                ResetTower();
            }
            if (Input.GetKey(KeyCode.Mouse1))
            {
                float mouseX = Input.GetAxis("Mouse X");
                float mouseY = Input.GetAxis("Mouse Y");
                transform.Rotate(Vector3.up, Time.deltaTime * mouseX * rotateSpeed, Space.World);
                transform.Rotate(transform.right, -Time.deltaTime * mouseY * rotateSpeed, Space.World);
                Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
                Vector3 ms = new Vector3(input.x, 0, input.y) * moveSpeed;
                transform.Translate(ms, Space.Self);
            }
        }
        /// <summary>
        /// 拖拽中键移动
        /// </summary>
        private void DragMove()
        {
            if (Input.GetKeyDown(KeyCode.Mouse2))
            {
                ResetTower();
                mousePos = Input.mousePosition;
            }
    
    
            if (Input.GetKey(KeyCode.Mouse2))
            {
                Vector3 deltPos = Input.mousePosition - mousePos;
                deltPos = -deltPos;
                transform.Translate(deltPos * 0.1f, Space.Self);
                mousePos = Input.mousePosition;
            }
    
        }
        /// <summary>
        /// 滚动中键移动
        /// </summary>
        private void ScrollMove()
        {
            float scrollwhell = Input.GetAxis("Mouse ScrollWheel");
            transform.Translate(Vector3.forward * Time.deltaTime * scrollwhell * scrollwheelSpeed);
           
        }
        /// <summary>
        /// 选择塔
        /// </summary>
        private void CheckTower()
        {
    
            RaycastHit hit = default(RaycastHit);
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000))
            {
                if (Input.GetKeyUp(KeyCode.Mouse0))
                {
                    if (hit.collider.tag == "TowerBase")
                    {
                        currSelectObj = hit.collider.gameObject;
                        if (selectTower != null)
                        {
                            selectTower(currSelectObj);
    
                        }
                        toMove = true;
                        toRot = true;
                    }
                }
                
            }
        }
        /// <summary>
        /// 移动到塔
        /// </summary>
        bool toMove = false;
        private void MoveToTower()
        {
            if (!toMove)
                return;
            transform.position = Vector3.Lerp(transform.position, currSelectObj.transform.position, Time.deltaTime* lerpSpeed);
            if (Vector3.Distance(currSelectObj.transform.position, transform.position)<10)
                toMove = false;
        }
        /// <summary>
        /// 选择向塔
        /// </summary>
        bool toRot = false;
        private void RotateToTower()
        {
            if (!toRot)
                return;
            Quaternion lookAt = Quaternion.LookRotation((currSelectObj.transform.position - transform.position).normalized);
            transform.rotation = Quaternion.Lerp(transform.rotation, lookAt, Time.deltaTime * lerpSpeed * 6);
            float angle = Quaternion.Angle(transform.rotation, lookAt);
            if (angle<1)
                toRot = false;
        }
        /// <summary>
        /// 设置选择塔为null
        /// </summary>
        /// <param name="go"></param>
        public void ResetTower()
        {
            if (exitSelectTower != null)
               exitSelectTower(currSelectObj);
    
            currSelectObj = null;
        }
    }
  • 相关阅读:
    零基础学python-2.15 回到我们的游戏 加入for以及列表
    零基础学python-2.14 for循环语句
    零基础学python-在3.x版本之后的print()不换行
    零基础学python-2.13 回到我们的游戏 加入循环
    CSV文件读取
    jmeter 测试webservice协议soap接口
    jmeter-plugins-manager.jar插件安装
    JDBC协议(jmeter链接mysql)
    xftp连接centos7
    xshell连接centos7
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14564664.html
Copyright © 2011-2022 走看看