using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class g_1 : MonoBehaviour {
//围绕的中心点
public Transform target;
//距离
public float normalDistance = 10;
//x的速度
private float xSpeed = 150.0f;
//y的速度
private float ySpeed = 150.0f;
//y的最小旋转角度
private int yMinLimit = 30;
//y的最大旋转角度
private int yMaxLimit = 80;
//绕x轴的角度
private float x = 0.0f;
//绕y轴的角度
private float y = 0.0f;
//
private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));
//记录中心点的位置
private Vector3 CameraTarget;
//记录x轴手指滑动的方向
float xm;
//记录y轴手指滑动的方向
float ym;
void Start()
{
CameraTarget = target.position;
float z = target.transform.position.z - normalDistance;
transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);
transform.LookAt(target);
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void Update()
{
if (Input.GetMouseButton(1))
{
xSpeed = 150.0f;
ySpeed = 150.0f;
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
xm = Input.GetAxis("Mouse X");
ym = Input.GetAxis("Mouse Y");
Debug.Log("x:" + x + "y:" + y);
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;
transform.rotation = rotation;
transform.position = position;
transform.LookAt(CameraTarget);
}else {
if (xSpeed >= 0 || ySpeed >= 0)
{
xSpeed = Mathf.Lerp(xSpeed, 0, 2.0f * Time.deltaTime);
ySpeed = Mathf.Lerp(ySpeed, 0, 2.0f * Time.deltaTime);
if (xm > 0)
{
x += xSpeed * 0.02f;
if (ym > 0)
{
y -= ySpeed * 0.02f;
}else if (ym < 0){
y -= -ySpeed * 0.02f;
}
}
if (xm < 0)
{
x += -xSpeed * 0.02f;
if (ym > 0)
{
y -= ySpeed * 0.02f;
}
else if (ym < 0)
{
y -= -ySpeed * 0.02f;
}
}
Debug.Log("x:" + x + "y:" + y);
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;
transform.rotation = rotation;
transform.position = position;
transform.LookAt(CameraTarget);
}
}
}
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辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 通过本文,你将学会如何使用一句代码实现鼠标拖动使Cube旋转 效果预览: 代码如下 再来一份: 下面是笔者收藏的其...
- 为了美观,去掉窗体的标题栏,给窗体的FormBorderStyle属性设置为None,这时,使用鼠标拖动和缩放窗口...
- 01 前几天跟朋友出去逛街,讨论了一些彼此成长过程中的经历。在聊到听话的小孩跟会吵的小孩这个话题时,我特别有感触。...