unity鼠标晃动camera的代码
以下是一个示例代码,用于实现当鼠标晃动时摄像机跟随移动:
using UnityEngine;
public class CameraShake : MonoBehaviour
{
public float shakeAmount = 0.7f; // 晃动的幅度
public float shakeDuration = 1.0f; // 晃动的持续时间
private Vector3 originalPosition;
private float shakeTimer = 0.0f;
void Start()
{
originalPosition = transform.localPosition;
}
void Update()
{
if (Input.GetMouseButtonDown(0)) // 当鼠标左键按下时开始晃动
{
StartShake();
}
if (shakeTimer > 0)
{
// 生成一个随机的晃动偏移量
Vector3 randomOffset = Random.insideUnitSphere * shakeAmount;
// 将晃动偏移量应用到摄像机的位置上
transform.localPosition = originalPosition + randomOffset;
shakeTimer -= Time.deltaTime;
}
else
{
// 恢复摄像机的原始位置
transform.localPosition = originalPosition;
}
}
void StartShake()
{
shakeTimer = shakeDuration;
}
}
这个代码可以将摄像机的位置随机晃动一段时间,当鼠标左键按下时开始晃动。你可以将这个脚本添加到摄像机对象上,然后调整晃动的幅度和持续时间来满足你的需求
原文地址: http://www.cveoy.top/t/topic/hAHs 著作权归作者所有。请勿转载和采集!