Unity物体不规则震动

通过Random.insideUnitSphere 获取对象中心的一个震动范围
以乘法和shakeAmount 进行振幅的调整
结束的时候回归初始位置

public class BlockShake : MonoBehaviour {


  float shakeAmount = 0.05f;//振幅
  bool is_shake;
  Vector3 first_pos;
  
  private void Start()
  {
    first_pos = this.transform.localPosition;
  }
  
  public void StartShake()
  {
    //if (is_shake) return;
    is_shake = true;
  }
  
  public void Update()
  {
    if (!is_shake) return;
    Vector3 pos = first_pos + Random.insideUnitSphere * shakeAmount;
    pos.y = transform.localPosition.y;
    transform.localPosition = pos;
  }

  public void EndShake()
  {
    is_shake = false;
    first_pos.y = transform.localPosition.y;
    transform.localPosition = first_pos;
  }

}

猜你喜欢

转载自blog.csdn.net/qq_33205561/article/details/83069837