Procedural Animation的百科解释:https://en.wikipedia.org/wiki/Procedural_animation
想深入了解UE4引擎中的: control rig, Unity引擎中的:Animation Rigging
最近在学习Procedural Animation相关的只是,先从2D开始。
最好理解的程序化动画就是贪吃蛇的运动,在此基础上改造添加身体部分的运动。
先简单代码,转向目标(鼠标位置)并向目标移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Controller : MonoBehaviour
{
public float _speed = 1f;
private Rigidbody _rigidbody;
// Start is called before the first frame update
void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
float multiplier = 1f;
if (Input.GetKey(KeyCode.LeftShift))
{
multiplier = 2f;
GetComponentInChildren<ProceduralAnimation>().running = true;
}
else
GetComponentInChildren<ProceduralAnimation>().running = false;
if (_rigidbody.velocity.magnitude < _speed * multiplier)
{
float value = Input.GetAxis("Vertical");
if (value != 0)
_rigidbody.AddForce(0, 0, value * Time.fixedDeltaTime * 1000f);
value = Input.GetAxis("Horizontal");
if (value != 0)
_rigidbody.AddForce(value * Time.fixedDeltaTime * 1000f, 0f, 0f);
}
}
}
然后添加触角逻辑:
创建空对象Tentacle,添加LineRenderer 组件并进行设置:
创建脚本Tentacle.cs 并添加到对象上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tentacle : MonoBehaviour
{
public int Length;
public LineRenderer LineRend;
public Vector3[] SegmentPoses;
public Transform TargetDir; // 是头部位置
public float TargetDist;
private Vector3[] SegmentV;
public float SmoothSpeed;
public float TrailSpeed;
// Start is called before the first frame update
void Start()
{
LineRend.positionCount = Length;
SegmentPoses = new Vector3[Length];
SegmentV = new Vector3[Length];
}
// Update is called once per frame
void Update()
{
SegmentPoses[0] = TargetDir.position;
for (int i = 1; i < SegmentPoses.Length; i++)
{
SegmentPoses[i] = Vector3.SmoothDamp(SegmentPoses[i], SegmentPoses[i - 1] + TargetDir.right * TargetDist, ref SegmentV[i], SmoothSpeed + i / TrailSpeed);
}
LineRend.SetPositions(SegmentPo