-
Notifications
You must be signed in to change notification settings - Fork 0
/
US_Player.cs
59 lines (43 loc) · 1.64 KB
/
US_Player.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
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
using UnityEngine.UI;
[RequireComponent(typeof(US_Controller2D))]
public class US_Player : MonoBehaviour
{
public float jumpHeight = 4;
public float timeToJumpApex = .4f;
public float accelerationTimeAirborne = .2f;
public float accelerationTimeGrounded = .1f;
Vector3 velocity;
float velocityXSmoothing;
public float moveSpeed = 6;
float jumpVelocity;
float gravity;
US_Controller2D controller;
private void Start()
{
controller = GetComponent<US_Controller2D>();
gravity = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2);
jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
print("Gravity: " + gravity + " Jump Velocity: " + jumpVelocity);
}
private void Update()
{
if (controller.collisions.above || controller.collisions.below)
{
velocity.y = 0;
}
if (Input.GetButtonDown("Jump") && controller.collisions.below)
{
velocity.y = jumpVelocity;
Debug.Log("Jump");
}
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
float targetVelocityX = input.x * moveSpeed;
velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}/** END OF CLASS */