created player movement, jump and run faster
This commit is contained in:
20
Assets/Scripts/CameraMovement.cs
Normal file
20
Assets/Scripts/CameraMovement.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraMovement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] GameObject player;
|
||||
[SerializeField] float offsetY = 0;
|
||||
[SerializeField] float interpolate = 0.2f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
transform.position = Vector2.Lerp(transform.position, player.transform.position + new Vector3(0, offsetY, 0), .2f);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CameraMovement.cs.meta
Normal file
11
Assets/Scripts/CameraMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c78e7c2063ef1e54180d19959c5872f5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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