Creating player movements

This commit is contained in:
Anonymous Raccoon
2018-03-04 15:58:27 +01:00
parent 96b855d73d
commit 5da5db50a4
4 changed files with 79 additions and 19 deletions
+56 -1
View File
@@ -1,8 +1,19 @@
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private int speed = 10;
[SerializeField] private int airSpeed = 5;
[SerializeField] private int jumpForce = 400;
private float jumpDelay = 0;
private bool isGrounded = true;
private bool airControl = false;
private float jumpDirection;
private Rigidbody rb;
private void Start()
{
@@ -11,6 +22,50 @@ public class PlayerMovement : MonoBehaviour
private void Update ()
{
rb.AddForce(new Vector3(Input.GetAxis("Horizontal") , 0, 0));
if (!isGrounded)
{
if (jumpDirection > 0)
{
if (Input.GetAxis("Horizontal") > 0)
airControl = false;
else
airControl = true;
}
else
{
if (Input.GetAxis("Horizontal") > 0)
airControl = true;
else
airControl = false;
}
}
rb.MovePosition(rb.position + new Vector3(Input.GetAxis("Horizontal"), 0, 0) * (airControl ? airSpeed : speed) * Time.deltaTime);
if(jumpDelay == 0)
{
RaycastHit hit;
if (Physics.Raycast(rb.position, Vector3.down, out hit, 1))
{
if(hit.collider.tag != "Player" || hit.collider.tag != "Projectile")
isGrounded = true;
}
}
else
{
jumpDelay--;
}
if (Input.GetAxis("Jump") > 0 && isGrounded)
{
print(Input.GetAxis("Jump"));
jumpDirection = Input.GetAxis("Horizontal");
isGrounded = false;
jumpDelay = Mathf.Round(750 * Time.deltaTime);
rb.AddForce(new Vector3(0, jumpForce * Input.GetAxis("Jump"), 0), ForceMode.Acceleration);
}
airControl = false;
}
}
+19 -14
View File
@@ -202,11 +202,12 @@ GameObject:
serializedVersion: 5
m_Component:
- component: {fileID: 1180913641}
- component: {fileID: 1180913642}
- component: {fileID: 1180913640}
- component: {fileID: 1180913639}
- component: {fileID: 1180913638}
- component: {fileID: 1180913637}
m_Layer: 0
m_Layer: 8
m_Name: Player
m_TagString: Player
m_Icon: {fileID: 0}
@@ -238,7 +239,7 @@ Rigidbody:
m_UseGravity: 1
m_IsKinematic: 0
m_Interpolate: 0
m_Constraints: 0
m_Constraints: 112
m_CollisionDetection: 0
--- !u!23 &1180913639
MeshRenderer:
@@ -294,6 +295,22 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1180913642
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1180913636}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d21adc90a3a0c5498d394337d4f81fb, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 10
airSpeed: 7
jumpForce: 400
isGrounded: 1
airControl: 0
--- !u!1 &1699708122
GameObject:
m_ObjectHideFlags: 0
@@ -370,7 +387,6 @@ GameObject:
- component: {fileID: 1936456414}
- component: {fileID: 1936456413}
- component: {fileID: 1936456412}
- component: {fileID: 1936456416}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
@@ -444,14 +460,3 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1936456416
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1936456411}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d21adc90a3a0c5498d394337d4f81fb, type: 3}
m_Name:
m_EditorClassIdentifier:
+3 -3
View File
@@ -93,10 +93,10 @@ InputManager:
positiveButton: space
altNegativeButton:
altPositiveButton:
gravity: 1000
gravity: 3
dead: 0.001
sensitivity: 1000
snap: 0
sensitivity: 100
snap: 1
invert: 0
type: 0
axis: 0
+1 -1
View File
@@ -13,7 +13,7 @@ TagManager:
- UI
-
-
-
- Ground
-
-
-