created player movement, jump and run faster
This commit is contained in:
@@ -4,16 +4,61 @@ using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float runSpeed = 10f;
|
||||
[SerializeField] private float jumpSpeed = 10f;
|
||||
[SerializeField] private float runFasterFactor = 1.5f;
|
||||
[SerializeField] GameObject ground;
|
||||
|
||||
Rigidbody2D rigidbody;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
||||
rigidbody = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
|
||||
MovePlayer();
|
||||
}
|
||||
|
||||
private void MovePlayer()
|
||||
{
|
||||
Jump();
|
||||
Run();
|
||||
}
|
||||
|
||||
private void Jump()
|
||||
{
|
||||
var playerOnGround = rigidbody.IsTouching(ground.GetComponent<CompositeCollider2D>());
|
||||
|
||||
// Jump
|
||||
if (Input.GetKey(KeyCode.Space) && playerOnGround)
|
||||
{
|
||||
rigidbody.velocity += new Vector2(0, jumpSpeed);
|
||||
rigidbody.velocity = new Vector2(rigidbody.velocity.x,
|
||||
Mathf.Clamp(rigidbody.velocity.y, 0, jumpSpeed));
|
||||
}
|
||||
}
|
||||
|
||||
private void Run()
|
||||
{
|
||||
var actualRunSpeed = runSpeed;
|
||||
|
||||
// Speed Run Increase
|
||||
if (Input.GetKey(KeyCode.W))
|
||||
{
|
||||
actualRunSpeed *= runFasterFactor;
|
||||
}
|
||||
|
||||
// Run
|
||||
if (Input.GetKey(KeyCode.RightArrow))
|
||||
{
|
||||
rigidbody.velocity = new Vector2(actualRunSpeed, rigidbody.velocity.y);
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.LeftArrow))
|
||||
{
|
||||
rigidbody.velocity = new Vector2(actualRunSpeed * -1, rigidbody.velocity.y);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user