-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerControl.cs
More file actions
40 lines (37 loc) · 1008 Bytes
/
PlayerControl.cs
File metadata and controls
40 lines (37 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
private Rigidbody rBody;
private Animation ani;
// Start is called before the first frame update
void Start()
{
rBody = GetComponent<Rigidbody>();
ani = GetComponent<Animation>();
}
// Update is called once per frame
void Update()
{
//垂直轴
float vertical = Input.GetAxis("Vertical");
//水平轴
float horizontal = Input.GetAxis("Horizontal");
//方向
Vector3 dir = new Vector3(horizontal, 0, vertical);
if(dir != Vector3.zero)
{
//改变方向
transform.rotation = Quaternion.LookRotation(dir);
//移动
rBody.velocity = dir * 3;
//移动动画
ani.Play("run");
}
else
{
ani.Play("idle");
}
}
}