-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation_controller.cs
47 lines (41 loc) · 1.22 KB
/
animation_controller.cs
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
41
42
43
44
45
46
47
using UnityEngine;
using System.Collections;
using System;
public class player_animation : MonoBehaviour {
public float moveSpeed = 6.0f;
public float jumpForce = 700.0f;
private Vector2 moveDir;
private Animator anim;
private Rigidbody2D rb;
// Use this for initialization
void Start ()
{
moveDir = Vector2.zero;
anim = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update ()
{
float moveX = Input.GetAxis ("Horizontal");
moveDir = new Vector2 (moveX, 0);
moveDir.Normalize ();
anim.SetFloat ("Speed", Math.Abs(moveX));
//horizontal move control
transform.position += new Vector3 (moveDir.x, moveDir.y, 0.0f)*moveSpeed*Time.deltaTime;
if (moveX > 0)
{
transform.localScale = new Vector3(Math.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
else if (moveX < 0)
{
transform.localScale = new Vector3(-Math.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
//jump control
if (rb.velocity.y >= -0.01f && rb.velocity.y <= 0.01f && Input.GetKeyDown (KeyCode.Space))
{
rb.AddForce(new Vector2(0, jumpForce));
}
anim.SetFloat ("vSpeed", rb.velocity.y);
}
}