public class ShakeRun : MonoBehaviour
{
//float rawY; //弹动的点
float startY; // 物体一开始的点
float curForce; //力量
float speed = 20; //定值改编运动速度
float currentY; //物体要运动的点
float curVelocity; //速度
[Range(0.000001f, 0.9999999f), SerializeField]
float attenFac = 0.2f;
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
startY = this.transform.position.y;
SetTaily(startY + 1);
}
this.transform.position = new Vector3(this.transform.position.x,currentY,this.transform.position.z);
if (Input.GetKey(KeyCode.W))
{
CheckForce();
}
}
void CheckForce()
{
SetForce();
curVelocity += curForce * Time.deltaTime * speed;
currentY += curVelocity * Time.deltaTime * speed;
curVelocity -= curVelocity * attenFac * Time.deltaTime * speed;
}
void SetForce()
{
curForce = (startY - currentY) * 0.8f;
Debug.Log(curForce);
}
void SetTaily(float f)
{
currentY = f;
SetForce();
curVelocity = curForce;
}
}