| using DG.Tweening; |
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| using UnityEngine.EventSystems; |
| |
| public class CameraC : MonoBehaviour |
| { |
| public Transform targetModel; |
| |
| public float speed = 20; |
| |
| public float distance_v; |
| public float distance_h; |
| |
| public float rotation_H_speed = 1; |
| public float rotation_V_speed = 1; |
| |
| public float move_H_speed = 1; |
| public float move_V_speed = 1; |
| |
| public static float current_rotation_H; |
| public static float current_rotation_V; |
| |
| |
| public static float maxUpAngle = 85; |
| public static float maxDownAngle = 5; |
| |
| |
| public static float transMaxUp = 400; |
| public static float transMaxDown = 20; |
| |
| |
| public static float zoomMax = 400; |
| public static float zoomMin = 5; |
| |
| public static float zoomSpeed = 15; |
| |
| |
| public static BaseModel selectModel; |
| [Logger] |
| private IWebLogger logger; |
| |
| public static bool isRotation = false; |
| |
| private Vector3 mForward; |
| |
| |
| private void Start() |
| { |
| current_rotation_H = transform.localEulerAngles.y; |
| current_rotation_V = transform.localEulerAngles.x; |
| } |
| |
| |
| void Update() |
| { |
| #region 按键移动 |
| |
| if (Input.GetKey(KeyCode.A)) |
| { |
| transform.Translate(Vector3.left * speed * Time.deltaTime); |
| } |
| |
| if (Input.GetKey(KeyCode.D)) |
| { |
| transform.Translate(Vector3.right * speed * Time.deltaTime); |
| } |
| |
| if (Input.GetKey(KeyCode.W)) |
| { |
| transform.Translate(Vector3.forward * speed * Time.deltaTime); |
| } |
| |
| if (Input.GetKey(KeyCode.S)) |
| { |
| transform.Translate(Vector3.back * speed * Time.deltaTime); |
| } |
| |
| if (Input.GetKey(KeyCode.Q)) |
| { |
| transform.Translate(Vector3.up * speed * Time.deltaTime); |
| } |
| |
| if (Input.GetKey(KeyCode.E)) |
| { |
| transform.Translate(Vector3.down * speed * Time.deltaTime); |
| } |
| |
| #endregion |
| |
| |
| if (Input.GetAxis("Mouse ScrollWheel") != 0) |
| { |
| |
| float wheel = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomSpeed; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (Vector3.Distance(transform.position, targetModel.position) < zoomMin && wheel > 0) return; |
| if (Vector3.Distance(transform.position, targetModel.position) > zoomMax && wheel < 0) return; |
| mForward = targetModel.position - Camera.main.transform.position; |
| transform.position += mForward * wheel; |
| } |
| |
| |
| if (Input.GetKeyUp(KeyCode.F)) |
| { |
| if (selectModel != null) |
| { |
| transform.LookAt(selectModel.transform); |
| } |
| } |
| } |
| |
| private void LateUpdate() |
| { |
| |
| if (Input.GetMouseButtonUp(0)) |
| { |
| |
| Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); |
| RaycastHit hit; |
| if (Physics.Raycast(ray, out hit, 2000)) |
| { |
| Debug.DrawLine(ray.origin, hit.point, Color.red, 0.5f); |
| |
| BaseModel baseModel = hit.collider.gameObject.GetComponent<BaseModel>(); |
| if (baseModel != null) |
| { |
| selectModel = baseModel; |
| |
| logger.Log("id:" + baseModel.Uuid + " prefabName:" + baseModel.BaseData.PrefabName + |
| " position:" + baseModel.BaseData.Transform.position + |
| " modelType:" + baseModel.modelType); |
| |
| |
| if (!baseModel.BaseData.IsNoClick) |
| { |
| Sender sender = new Sender(); |
| sender.eventName = BaseEvent.click; |
| sender.souce = gameObject; |
| EventCenter.Instance.TriggerEvent(BaseEvent.click, sender, baseModel.Uuid); |
| } |
| |
| } |
| } |
| } |
| |
| |
| if (Input.GetMouseButton(1)) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| var mouse_x = Input.GetAxis("Mouse X"); |
| var mouse_y = -Input.GetAxis("Mouse Y"); |
| |
| |
| transform.RotateAround(targetModel.transform.position, Vector3.up, mouse_x * 5); |
| |
| |
| |
| float rotatedAngle = transform.eulerAngles.x + mouse_y * rotation_H_speed; |
| |
| |
| if (rotatedAngle < maxDownAngle) |
| { |
| transform.RotateAround(targetModel.position, transform.right, (mouse_y * rotation_H_speed) + (maxDownAngle - rotatedAngle)); |
| } |
| else if (rotatedAngle > maxUpAngle) |
| { |
| transform.RotateAround(targetModel.position, transform.right, (mouse_y * rotation_H_speed) - (rotatedAngle - maxUpAngle)); |
| } |
| else |
| { |
| transform.RotateAround(targetModel.position, transform.right, mouse_y * rotation_H_speed); |
| } |
| |
| } |
| |
| |
| if ( Input.GetMouseButton(0)) |
| { |
| Vector3 pos = new Vector3(-Input.GetAxis("Mouse X") * move_H_speed, -Input.GetAxis("Mouse Y") * move_V_speed, 0f); |
| float y = pos.y + transform.position.y; |
| |
| if (y > transMaxDown && y < transMaxUp) |
| { |
| this.transform.Translate(pos); |
| } |
| } |
| |
| } |
| |
| public float CheckAngle(float value) |
| { |
| float angle = value - 180; |
| |
| |
| if (angle > 0) |
| return angle - 180; |
| |
| |
| return angle + 180; |
| } |
| |
| |
| private static float ClampAngle(float angle, float min, float max) |
| { |
| if (angle < -360) |
| angle += 360; |
| if (angle > 360) |
| angle -= 360; |
| return Mathf.Clamp(angle, min, max); |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)