Unity: Pushed working movement script

Fixed issues like character not jumping
This commit is contained in:
Santi 2024-04-29 23:19:13 +01:00
parent 94b71fc610
commit 35dd1d8902
2 changed files with 71 additions and 58 deletions

View File

@ -358,8 +358,9 @@ GameObject:
- component: {fileID: 765999531} - component: {fileID: 765999531}
- component: {fileID: 765999530} - component: {fileID: 765999530}
- component: {fileID: 765999535} - component: {fileID: 765999535}
- component: {fileID: 765999534}
- component: {fileID: 765999536} - component: {fileID: 765999536}
- component: {fileID: 765999537}
- component: {fileID: 765999538}
m_Layer: 0 m_Layer: 0
m_Name: Capsule m_Name: Capsule
m_TagString: Player m_TagString: Player
@ -439,7 +440,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 765999529} m_GameObject: {fileID: 765999529}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.21045552, y: 2.21, z: 0.096935526} m_LocalPosition: {x: 0, y: 2, z: -0.5}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
@ -447,24 +448,6 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 3 m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!143 &765999534
CharacterController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 765999529}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Height: 2
m_Radius: 0.5
m_SlopeLimit: 45
m_StepOffset: 0.3
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &765999535 --- !u!114 &765999535
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -483,7 +466,7 @@ MonoBehaviour:
jumpForce: 2.5 jumpForce: 2.5
gravity: 9.8 gravity: 9.8
throwForce: {fileID: 765999536} throwForce: {fileID: 765999536}
controller: {fileID: 0} controller: {fileID: 765999538}
moveDirection: {x: 0, y: 0, z: 0} moveDirection: {x: 0, y: 0, z: 0}
forwardsKey: 119 forwardsKey: 119
backwardsKey: 115 backwardsKey: 115
@ -510,7 +493,40 @@ MonoBehaviour:
previousObject: {fileID: 0} previousObject: {fileID: 0}
tempObject: {fileID: 0} tempObject: {fileID: 0}
throwForce: 5 throwForce: 5
interactKey: 102 --- !u!54 &765999537
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 765999529}
serializedVersion: 2
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 0
m_Interpolate: 0
m_Constraints: 112
m_CollisionDetection: 0
--- !u!143 &765999538
CharacterController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 765999529}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Height: 2
m_Radius: 0.5
m_SlopeLimit: 45
m_StepOffset: 0.3
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0, z: 0}
--- !u!1 &876529135 --- !u!1 &876529135
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -12,8 +12,8 @@ public class Movement : MonoBehaviour
public float gravity = 9.8f; public float gravity = 9.8f;
public PickUpObject throwForce; public PickUpObject throwForce;
public CharacterController controller; private CharacterController controller;
public Vector3 moveDirection = Vector3.zero; private Vector3 moveDirection = Vector3.zero;
[Header("Keybinds")] [Header("Keybinds")]
public KeyCode forwardsKey = KeyCode.W; public KeyCode forwardsKey = KeyCode.W;
@ -34,46 +34,43 @@ public class Movement : MonoBehaviour
float backwards = Convert.ToSingle(Input.GetKey(backwardsKey)); float backwards = Convert.ToSingle(Input.GetKey(backwardsKey));
float left = Convert.ToSingle(Input.GetKey(leftKey)); float left = Convert.ToSingle(Input.GetKey(leftKey));
float right = Convert.ToSingle(Input.GetKey(rightKey)); float right = Convert.ToSingle(Input.GetKey(rightKey));
moveDirection = new Vector3(right - left, 0, forwards - backwards); if (controller.isGrounded)
if (!controller.isGrounded)
{ {
moveDirection.y -= gravity * Time.deltaTime; moveDirection = new Vector3(right - left, 0, forwards - backwards);
controller.Move(moveDirection * Time.deltaTime); // Change the direction the player is facing based on the direction they are moving
return; if (right - left != 0 || forwards - backwards != 0)
}
throwForce.throwForce = 4.0f;
// Change the direction the player is facing based on the direction they are moving
if (moveDirection != Vector3.zero)
{
Quaternion ToRotation = Quaternion.LookRotation(new Vector3(right - left, 0, forwards - backwards));
throwForce.throwForce = 8.5f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
}
if (Input.GetKey(sprintKey))
{
moveDirection *= sprintSpeed;
jumpForce = 4.0f;
if(moveDirection != Vector3.zero)
{ {
throwForce.throwForce = 20.0f; Quaternion ToRotation = Quaternion.LookRotation(moveDirection);
throwForce.throwForce = 8.5f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
}
else
{
throwForce.throwForce = 4.0f;
} }
}
else
{
moveDirection *= speed;
jumpForce = 2.5f;
}
if (Input.GetKeyDown(jumpKey)) if (Input.GetKey(sprintKey))
{ {
moveDirection.y = jumpForce; moveDirection *= sprintSpeed;
jumpForce = 4.0f;
if(right - left != 0 || forwards - backwards != 0)
{
throwForce.throwForce = 20.0f;
}
}
else
{
moveDirection *= speed;
jumpForce = 2.5f;
}
if (Input.GetKeyDown(jumpKey))
{
moveDirection.y = jumpForce;
}
} }
moveDirection.y -= gravity * Time.deltaTime; moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime); controller.Move(moveDirection * Time.deltaTime);
} }
} }