Compare commits

..
Author SHA1 Message Date
Gregory Campbell 2ade04fcba Cleaned up jumping and double jumping implementation 2019-12-05 21:57:29 -05:00
2 changed files with 30 additions and 38 deletions
+15 -12
View File
@@ -484,7 +484,8 @@ MeshRenderer:
m_RayTracingMode: 2 m_RayTracingMode: 2
m_RenderingLayerMask: 1 m_RenderingLayerMask: 1
m_RendererPriority: 0 m_RendererPriority: 0
m_Materials: [] m_Materials:
- {fileID: 2100000, guid: 727f61c75f8320d4ab79aaf01ab76181, type: 2}
m_StaticBatchInfo: m_StaticBatchInfo:
firstSubMesh: 0 firstSubMesh: 0
subMeshCount: 0 subMeshCount: 0
@@ -541,27 +542,29 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
speed: 20 speed: 20
jumpSpeed: 20 jumpHeight: 15
gravity: 5 gravity: 5
rotationSpeed: 20 rotationSpeed: 20
pivot: {fileID: 1402695046} pivot: {fileID: 1402695046}
playerModel: {fileID: 615882415} playerModel: {fileID: 615882415}
--- !u!54 &741217458 --- !u!143 &741217458
Rigidbody: CharacterController:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 741217452} m_GameObject: {fileID: 741217452}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2 serializedVersion: 2
m_Mass: 1 m_Height: 2
m_Drag: 5 m_Radius: 0.5
m_AngularDrag: 0.05 m_SlopeLimit: 45
m_UseGravity: 1 m_StepOffset: 0.3
m_IsKinematic: 0 m_SkinWidth: 0.08
m_Interpolate: 0 m_MinMoveDistance: 0.001
m_Constraints: 80 m_Center: {x: 0, y: 0, z: 0}
m_CollisionDetection: 0
--- !u!1 &876326172 --- !u!1 &876326172
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
+15 -26
View File
@@ -9,36 +9,31 @@ using System.Collections;
public class PlayerController : MonoBehaviour public class PlayerController : MonoBehaviour
{ {
Rigidbody rb; CharacterController characterController;
public float speed; public float speed;
public float jumpSpeed; public float jumpHeight;
public float gravity; public float gravity;
public float rotationSpeed; public float rotationSpeed;
public Transform pivot; public Transform pivot;
public GameObject playerModel; public GameObject playerModel;
private bool extraJump = true; private int jump = 0;
private float distToGround;
private Vector3 moveDirection; private Vector3 moveDirection;
void Start() void Start()
{ {
rb = GetComponent<Rigidbody>(); characterController = GetComponent<CharacterController>();
} }
bool IsGrounded(){ void Update()
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
}
void FixedUpdate()
{ {
float yStore = moveDirection.y; float yStore = moveDirection.y;
//Need to switch to 'raw' when using keyboard //Need to switch to 'raw' when using keyboard
moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal")); moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
if (extraJump == true) if (jump <= 1)
{ {
moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run
} else } else
@@ -48,9 +43,9 @@ public class PlayerController : MonoBehaviour
moveDirection.y = yStore; moveDirection.y = yStore;
if (IsGrounded()) if (characterController.isGrounded)
{ {
extraJump = true; jump = 0;
moveDirection.y = 0.0f; moveDirection.y = 0.0f;
if(Input.GetKey(KeyCode.LeftShift)) if(Input.GetKey(KeyCode.LeftShift))
@@ -60,24 +55,18 @@ public class PlayerController : MonoBehaviour
moveDirection = moveDirection.normalized * speed; moveDirection = moveDirection.normalized * speed;
} }
if (Input.GetButtonDown("Jump")) }
{
moveDirection.y = jumpSpeed;
}
} else {
if (Input.GetButtonDown("Jump") && extraJump == true)
{
moveDirection.y = jumpSpeed;
extraJump = false;
}
if (Input.GetButtonDown("Jump") && jump <= 1)
{
moveDirection.y = jumpHeight;
jump++;
} }
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravity * Time.deltaTime); moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime;
// Move the controller // Move the controller
rb.AddForce(moveDirection * Time.deltaTime); characterController.Move(moveDirection * Time.deltaTime);
//Move the player in different directions based on camera look direction //Move the player in different directions based on camera look direction
//Need to switch to 'raw' when using keyboard //Need to switch to 'raw' when using keyboard