unity版本为:2018.3.14
1、找到带动画模型
获取途径:
① unity官方有免费模型和付费模型。
② 有mmd转unity的转换器。
我使用的是“Little Heroes Mega Pack”素材包,里面可以自己设置主角的衣服和发型。
2、设计互动动作
好多材质包自带动作动画(Animation)。
百度搜“unity animation”等可以查到更具体的教程,简单说一下就是:
① 像创建C# Script一样,创建一个Animator Controller,双击点开,拖曳素材自带的Animation到编辑窗口中。
② 设置触发的动画状态转换的变量Parameter。

③ 创建Script,编程实现修改对应的Parameter状态。
④ 将Script拖曳给角色。
参考资料:https://blog.csdn.net/qq_39646949/article/details/86225527
3、将窗口背景设置为透明和可以拖曳
设置为透明,参考资料:https://blog.csdn.net/dark00800/article/details/70314432
注意要把相机的背景颜色设置为黑色。
设置为可以拖曳,参考资料:https://blog.csdn.net/xu1989101/article/details/52620546
直接将两个脚本各自放到相机对象中即可。
4、添加角色互动,实现转头看鼠标的效果
① 将角色的Animator设置为disable。
② 将Animation的update mode设置为:Animate Physics。
③ 用负反馈控制转头的方向和大小,这个C#脚本放在角色的脖子(neck)上,代码如下(但是没有添加上下的点头,只有左右横摇):
void Update()
{
mousePositionOnScreen = Input.mousePosition;
if (!Globle.isEnableAnimator) // 没有在播放动画
{
// 转头
mousePositionOnScreen.x = mousePositionOnScreen.x < 0 ? 0 : mousePositionOnScreen.x;
wx0 = (mousePositionOnScreen.x - Screen.width / 2) / (Screen.width / 2) * 60;
if (GetAbs(wx0 - wx) >= 0.00001)
{
ax = wx0 - wx;
wx += ax;
transform.Rotate(new Vector3(ax, 0, 0));// * Time.deltaTime
}
}
else
{
wx = 0;
ax = 0;
wx0 = 0;
wy = 0;
ay = 0;
wy0 = 0;
}
}
还添加了鼠标移出边框角色就进入IDLE状态,播放IDLE状态的动画。
5、脚本代码
由于代码和角色模型强耦合,使用别的角色可能会出错。
控制角色的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Romeo : MonoBehaviour
{
private Animator animator;
private float timer = 1.0f;
private bool isTimerStart = false; // 是否打开了伪定时器
private bool isPlaying = false; // 是否在播放动画
// Start is called before the first frame update
void Start()
{
animator = this.GetComponent<Animator>();
Globle.isEnableAnimator = false;
animator.enabled = false;
//animator.SetBool("Nod Head", true);
}
// Update is called once per frame
void Update()
{
if(isTimerStart && Globle.isEnableAnimator) // 伪定时器,每帧检查
{
timer -= Time.deltaTime;
if (timer < 0)
{
isPlaying = false;
Globle.isEnableAnimator = false;
animator.enabled = Globle.isEnableAnimator;
isTimerStart = false;
}
}
if (Input.GetMouseButtonDown(1)) // 右键按下
{
isPlaying = true;
Globle.isEnableAnimator = true;
animator.enabled = Globle.isEnableAnimator;
animator.SetBool("Dash", true);
isTimerStart = false;
}
if (Input.GetMouseButtonUp(1)) // 右键释放
{
isPlaying = true;
animator.SetBool("Dash", false);
timer = 2.0f; // 开始定时
isTimerStart = true;
}
if (Input.GetMouseButtonDown(0)) // 左键单击
{
isPlaying = true;
Globle.isEnableAnimator = true;
animator.enabled = Globle.isEnableAnimator;
animator.SetTrigger("Jump");
timer = 2.0f; // 开始定时
isTimerStart = true;
}
// 鼠标移出边,框进入IDLE动画
Vector3 mousePositionOnScreen = Input.mousePosition;
if (mousePositionOnScreen.x < Screen.width * (-1/4) || mousePositionOnScreen.x > Screen.width * 5/4 || mousePositionOnScreen.y < Screen.height * (-1) / 4 || mousePositionOnScreen.y > Screen.height * 5/4)
{
Globle.isEnableAnimator = true;
animator.enabled = true;
}
else
{
if (!isPlaying)
{
Globle.isEnableAnimator = false;
animator.enabled = false;
}
}
}
}
控制脖子转动的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NeckController : MonoBehaviour
{
public float ax, ay, az;
public float wx, wy, wz;
public float wx0, wy0, wz0;
Vector3 mousePositionOnScreen;
// Start is called before the first frame update
void Start()
{
ax = 2;
ay = 2;
az = 2;
wx = 0;
wy = 0;
wz = 0;
wx0 = 0;
wy0 = 0;
wz0 = 0;
}
// Update is called once per frame
void Update()
{
mousePositionOnScreen = Input.mousePosition;
if (!Globle.isEnableAnimator) // 没有在播放动画
{
// 转头
mousePositionOnScreen.x = mousePositionOnScreen.x < 0 ? 0 : mousePositionOnScreen.x;
wx0 = (mousePositionOnScreen.x - Screen.width / 2) / (Screen.width / 2) * 60;
if (GetAbs(wx0 - wx) >= 0.00001)
{
ax = wx0 - wx;
wx += ax;
transform.Rotate(new Vector3(ax, 0, 0));// * Time.deltaTime
}
// 抬头没有做出来
//float mouse_y = mousePositionOnScreen.y;
//transform.Rotate(new Vector3(0, 0, ay));// * Time.deltaTime
}
else
{
wx = 0;
ax = 0;
wx0 = 0;
wy = 0;
ay = 0;
wy0 = 0;
}
Debug.Log("wy0: " + wy0 + ",wy: " + wy);
}
float GetAbs(float x)
{
return x > 0 ? x : -x;
}
float GetLimit(float x, float lim)
{
if (x >= lim) x = lim;
if (x <= -1*lim) x = -1*lim;
return x;
}
}
此外还需要添加一个全局变量,用于控制是否播放动画(这里应该还可以优化)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Globle : MonoBehaviour
{
public static bool isEnableAnimator = true;
public static float wx = 0;
}
完整工程里面还涉及模型,就不放上来了……
还有很多BUG或许日后会改,如果有2.0版本的话。
6、最终效果
左键点击跳跃

右键长按奔跑

头部鼠标左右跟随

还有拖曳功能。
整体在桌面上的效果如下:

7948




被折叠的 条评论
为什么被折叠?



