上面3个图片就是对弹簧这种运动的基本应用。其实弹簧的原理很简单,公式就是胡克定律,高中的课程中有讲解,这里我们需要把公式转换成Unity里面具体的参数和方法。核心的原理是下面几段代码
float force = 0 - _angle;
_velocity += _springForce * force * Time.deltaTime;
_velocity *= 1 - _damp * Time.deltaTime;
代码第一行是来计算返回默认点的力,默认初始值为0,第二行是来根据力来计算速度的变化,如果不要第三行的话我们可以理解为力量不消耗的弹簧运动,然后现实中却不是这样的能量存在消耗,所以第三行就是来模拟能量的消耗。第一个张图片我们可以理解为一个简单弹簧的移动过程,当我们点击鼠标的时候会给它一个力 这个力主要用来改变速度,所以直接是_velocity+=某个值,第一个应用比较简单我直接贴出代码不讲解了?
using UnityEngine;
using System.Collections;
public class Spring : MonoBehaviour
{
[SerializeField] private float _length;
[SerializeField] private Transform _sway;
private static float _springForce = 10f;
private static float _damp = 0.1f;
private float _velocity;
private float _angle;
private void Start()
{
_velocity = 0f;
_angle = 0f;
_sway.localPosition = new Vector3(0, 0, 0);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
_velocity = 10f;
}
_angle += _velocity * Time.fixedDeltaTime;
float force = 0 - _angle;
_velocity += _springForce * force * Time.deltaTime;
_velocity *= 1 - _damp * Time.deltaTime;
_sway.position = new Vector3(_angle, 0, 0);
}
}
第二个应用可能好多跑酷堆叠游戏可能会用到,这里我讲解一下原理,首先弹簧运动会简化出一个值,这个值如果我们把它跟位置绑定 那么它就是移动,如果跟方向绑定那么就会像钟摆。显然这个应该是像钟摆了,它的移动方式我们可以简化为,最底下那个方块我们称为0号方块 往上索引值越大 ,0号方块是不会运动,1号方块在0号方块的基础上运动,我们这里定义旋转的方向为? _rotateAxis = Vector3.forward;假设每个方块的间距为_offset,那么1号方块方向为
Vector3 t1 = Quaternion.AngleAxis(_angle, _rotateAxis) * Vector3.up * _offset;
_nodeList[1].localRotation = Quaternion.LookRotation(_rotateAxis, t1);
位置应该是0号方块的位置加上一个偏移t1。
_nodeList[1].localPosition = t1 + _nodeList[0].localPosition;
?以此类推,2号方块的位置应该是1号方块的位置加上一个偏移。偏移的角度这里定义为_angle * i
所以后面的节点坐标及方向为
for (int i = 2; i < _nodeList.Count; i++)
{
Vector3 offset1 = Quaternion.AngleAxis(_angle * i, _rotateAxis) * Vector3.up * _offset;
_nodeList[i].localPosition = _nodeList[i - 1].localPosition + offset1;
_nodeList[i].localRotation = Quaternion.LookRotation(_rotateAxis, offset1);
}
所以第二个例子总的代码为:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Stick : MonoBehaviour
{
[SerializeField] private float _offset;
private List<Transform> _nodeList;
private int _index;
private float _totalTime;
private static float _springForce = 40f;
private static float _damp = 1f;
private float _velocity;
private float _angle;
private Vector3 _rotateAxis;
private void Start()
{
_velocity = 0f;
_angle = 0f;
_nodeList = new List<Transform>();
foreach (Transform child in transform)
{
_nodeList.Add(child);
child.transform.localPosition = new Vector3(0, _index * _offset, 0);
_index++;
}
_rotateAxis = Vector3.forward;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
_velocity += 20f;
}
_angle += _velocity * Time.deltaTime;
Vector3 t1 = Quaternion.AngleAxis(_angle, _rotateAxis) * Vector3.up * _offset;
_nodeList[1].localPosition = t1 + _nodeList[0].localPosition;
_nodeList[1].localRotation = Quaternion.LookRotation(_rotateAxis, t1);
float force = 0 - _angle;
_velocity += _springForce * force * Time.deltaTime;
_velocity *= 1 - _damp * Time.deltaTime;
for (int i = 2; i < _nodeList.Count; i++)
{
Vector3 offset1 = Quaternion.AngleAxis(_angle * i, _rotateAxis) * Vector3.up * _offset;
_nodeList[i].localPosition = _nodeList[i - 1].localPosition + offset1;
_nodeList[i].localRotation = Quaternion.LookRotation(_rotateAxis, offset1);
}
}
}
第三个就比较简单了 只是一个单纯的角度和位置变化,比较简单所以我就直接贴出代码了
using UnityEngine;
using System.Collections;
public class Pendulum : MonoBehaviour
{
[SerializeField] private float _length;
[SerializeField] private Transform _sway;
private static float _springForce = 40f;
private static float _damp = 1f;
private float _velocity;
private float _angle;
private void Start()
{
_velocity = 0f;
_angle = 0f;
_sway.localPosition = new Vector3(0, -_length, 0);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
_velocity = 500f;
}
_angle += _velocity * Time.fixedDeltaTime;
float force = 0 - _angle;
_velocity += _springForce * force * Time.deltaTime;
_velocity *= 1 - _damp * Time.deltaTime;
Vector3 dir = Quaternion.Euler(0, 0, _angle) * Vector3.down;
_sway.localPosition = dir * _length;
}
}
|