# [Unity3D] Character Movement and Animation Switching (Animator)

> State machine (Animator) controls character animation playback, arrow keys control animation switching, code controls character movement, parameter Angular Speed controls turning speed

- Published: 2018-05-26
- Tags: 3D, GamePlay, Unity, Unity3D, Unity Notes
- View HTML version: https://bilibili.cat/en/posts/unity-animator-start
- Translated by AI from 中文: https://bilibili.cat/posts/unity-animator-start

---

## State Machine (Animator)

Controls character animation playback, arrow keys control animation switching

## Code-Controlled Character Movement

```csharp
public NavMeshAgent agent;
public Animator anima;
  
void Update () {
        if (Input.GetMouseButtonDown(0)){   // 按下鼠标左键，1是右键
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);// 获取屏幕坐标，转换射线
            RaycastHit hit; //保存射线碰撞信息
            if (Input.GetMouseButtonDown(0)){   // 按下鼠标左键，1是右键
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);// 获取屏幕坐标，转换射线
            RaycastHit hit; //保存射线碰撞信息
            if(Physics.Raycast(ray, out hit))// 判断碰撞
            {
                // Console.WriteLine(hit.point);
                agent.SetDestination(hit.point);// 传达指令，控制运动
            }
        anima.SetFloat("speed", agent.velocity.magnitude);// 设置Animator中speed的值
        }
```

## Parameter Angular Speed Controls Turning Speed
