PlayerController and PickAndThrow #167
|
@ -244,9 +244,12 @@ namespace SHADE
|
|||
SHCollider* collider = &component->GetCollider(i);
|
||||
auto cursorPos = ImGui::GetCursorPos();
|
||||
|
||||
//collider->IsTrigger
|
||||
|
||||
if (collider->GetType() == SHCollider::Type::BOX)
|
||||
{
|
||||
SHEditorWidgets::BeginPanel(std::format("{} Box Collider #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y });
|
||||
SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger");
|
||||
auto box = reinterpret_cast<SHBoundingBox*>(collider->GetShape());
|
||||
SHEditorWidgets::DragVec3
|
||||
(
|
||||
|
@ -257,6 +260,7 @@ namespace SHADE
|
|||
else if (collider->GetType() == SHCollider::Type::SPHERE)
|
||||
{
|
||||
SHEditorWidgets::BeginPanel(std::format("{} Sphere Collider #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y });
|
||||
SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger");
|
||||
auto sphere = reinterpret_cast<SHBoundingSphere*>(collider->GetShape());
|
||||
SHEditorWidgets::DragFloat
|
||||
(
|
||||
|
|
|
@ -123,5 +123,14 @@ namespace SHADE
|
|||
|
||||
}
|
||||
|
||||
Vector3 Camera::GetRight()
|
||||
{
|
||||
auto system = SHSystemManager::GetSystem<SHCameraSystem>();
|
||||
SHVec3 forward, up, right;
|
||||
system->GetCameraAxis(*GetNativeComponent(), forward, right, up);
|
||||
return Convert::ToCLI(right);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -66,5 +66,7 @@ namespace SHADE
|
|||
void SetMainCamera();
|
||||
void LookAt(Vector3 targetPosition);
|
||||
Vector3 GetForward();
|
||||
Vector3 GetRight();
|
||||
|
||||
};
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
using SHADE;
|
||||
using System;
|
||||
|
||||
public class CameraFix : Script
|
||||
{
|
||||
public CameraFix(GameObject gameObj) : base(gameObj) { }
|
||||
|
||||
private Transform tranform;
|
||||
public Vector3 pos = Vector3.Zero;
|
||||
public Vector3 rot = Vector3.Zero;
|
||||
protected override void awake()
|
||||
{
|
||||
tranform = GetComponent<Transform>();
|
||||
if (tranform == null)
|
||||
Debug.LogError("tranform is NULL!");
|
||||
else
|
||||
{
|
||||
tranform.LocalPosition = pos;
|
||||
tranform.LocalEulerAngles = rot;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
using SHADE;
|
||||
using System;
|
||||
using static PlayerController;
|
||||
|
||||
public class PickAndThrow : Script
|
||||
{
|
||||
private PlayerController pc;
|
||||
public GameObject item;
|
||||
public Vector3 throwForce = new Vector3(200.0f, 300.0f, 200.0f);
|
||||
private Transform itemTransform;
|
||||
private RigidBody itemRidibody;
|
||||
private Transform raccoonHoldLocation;
|
||||
private float lastXDir;
|
||||
private float lastZDir;
|
||||
private bool inRange = false;
|
||||
public PickAndThrow(GameObject gameObj) : base(gameObj) { }
|
||||
|
||||
protected override void awake()
|
||||
{
|
||||
pc = GetScript<PlayerController>();
|
||||
raccoonHoldLocation = GetComponentInChildren<Transform>();
|
||||
if (raccoonHoldLocation == null)
|
||||
Debug.Log("CHILD EMPTY");
|
||||
itemTransform = item.GetComponent<Transform>();
|
||||
if (itemTransform == null)
|
||||
Debug.Log("Item transform EMPTY");
|
||||
itemRidibody = item.GetComponent<RigidBody>();
|
||||
if (itemRidibody == null)
|
||||
Debug.Log("Item rb EMPTY");
|
||||
}
|
||||
protected override void update()
|
||||
{
|
||||
if (!pc.isMoveKeyPress)
|
||||
{
|
||||
if (pc.xAxisMove != 0)
|
||||
{
|
||||
lastXDir = pc.xAxisMove;
|
||||
lastZDir = 0.0f;
|
||||
}
|
||||
if (pc.zAxisMove != 0)
|
||||
{
|
||||
lastXDir = 0.0f;
|
||||
lastZDir = pc.zAxisMove;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lastXDir = pc.xAxisMove;
|
||||
lastZDir = pc.zAxisMove;
|
||||
}
|
||||
|
||||
if (pc != null && inRange && !pc.holdItem && Input.GetKey(Input.KeyCode.E))
|
||||
pc.holdItem = true;
|
||||
|
||||
if (pc != null && itemRidibody != null && itemTransform!= null && pc.holdItem)
|
||||
{
|
||||
itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition;
|
||||
itemRidibody.IsGravityEnabled = false;
|
||||
itemRidibody.LinearVelocity = Vector3.Zero;
|
||||
itemRidibody.AngularVelocity = Vector3.Zero;
|
||||
|
||||
if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
|
||||
{
|
||||
pc.holdItem = false;
|
||||
itemRidibody.IsGravityEnabled = true;
|
||||
itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir));
|
||||
itemRidibody.LinearVelocity += pc.rb.LinearVelocity;
|
||||
Debug.Log($"x: {itemRidibody.LinearVelocity.x} z: {itemRidibody.LinearVelocity.z}");
|
||||
}
|
||||
}
|
||||
else if(!pc.holdItem)
|
||||
itemRidibody.IsGravityEnabled = true;
|
||||
}
|
||||
protected override void onCollisionEnter(CollisionInfo info)
|
||||
{
|
||||
}
|
||||
protected override void onTriggerEnter(CollisionInfo info)
|
||||
{
|
||||
Debug.Log("ENTER");
|
||||
if (info.GameObject == item && !pc.holdItem)
|
||||
{
|
||||
inRange = true;
|
||||
}
|
||||
}
|
||||
protected override void onTriggerStay(CollisionInfo info)
|
||||
{
|
||||
Debug.Log("STAY");
|
||||
}
|
||||
protected override void onTriggerExit(CollisionInfo info)
|
||||
{
|
||||
Debug.Log("EXIT");
|
||||
if (info.GameObject == item && !pc.holdItem)
|
||||
{
|
||||
inRange = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,319 @@
|
|||
using SHADE;
|
||||
using System;
|
||||
|
||||
//in air controls?
|
||||
|
||||
public class PlayerController : Script
|
||||
{
|
||||
public enum RaccoonStates
|
||||
{
|
||||
IDILE,
|
||||
WALKING,
|
||||
RUNNING,
|
||||
JUMP,
|
||||
FALLING,
|
||||
CAUGHT,
|
||||
TOTAL
|
||||
}
|
||||
|
||||
public RigidBody rb;
|
||||
private Transform tranform;
|
||||
private Camera cam;
|
||||
|
||||
//to be remove
|
||||
public float drag = 2.0f;
|
||||
public bool holdItem { get; set; }
|
||||
[SerializeField]
|
||||
[Tooltip("The current state fo the raccoon")]
|
||||
public RaccoonStates currentState = RaccoonStates.IDILE;
|
||||
|
||||
//Movement variables============================================================
|
||||
[SerializeField]
|
||||
[Tooltip("Max vel for walking")]
|
||||
public float maxMoveVel = 2.0f;
|
||||
[SerializeField]
|
||||
[Tooltip("how much force is apply for walking")]
|
||||
public float moveForce = 50.0f;
|
||||
[SerializeField]
|
||||
[Tooltip("increase the moveForce and maxMoveVel by its amt")]
|
||||
public float sprintMultiplier = 2.0f;
|
||||
|
||||
private float oldForce;
|
||||
private float maxOldVel;
|
||||
private bool sprintIncreaseOnce = false;
|
||||
|
||||
public float xAxisMove { get; set; }
|
||||
public float zAxisMove { get; set; }
|
||||
|
||||
public bool isMoveKeyPress { get; set; }
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("curr not working")]
|
||||
public float rotationFactorPerFrame = 1.0f;
|
||||
|
||||
//Jumping vars==================================================================
|
||||
[SerializeField]
|
||||
[Tooltip("max height of the jump")]
|
||||
public float maxJumpHeight = 4.0f;
|
||||
[SerializeField]
|
||||
[Tooltip("max amt of time it will take for the jump")]
|
||||
public float maxJumpTime = 0.75f;
|
||||
[SerializeField]
|
||||
[Tooltip("increase gravity when falling")]
|
||||
public float fallMultipler = 2.0f;
|
||||
private float initialJumpVel;
|
||||
private bool isGrounded = true;
|
||||
private float gravity = -9.8f;
|
||||
private float groundGravity = -0.5f;
|
||||
|
||||
public PlayerController(GameObject gameObj) : base(gameObj) { }
|
||||
|
||||
protected override void awake()
|
||||
{
|
||||
|
||||
isMoveKeyPress = false;
|
||||
holdItem = false;
|
||||
//rigidbody check
|
||||
rb = GetComponent<RigidBody>();
|
||||
if (rb == null)
|
||||
Debug.LogError("RigidBody is NULL!");
|
||||
else
|
||||
{
|
||||
rb.IsGravityEnabled = false;
|
||||
rb.FreezeRotationX = true;
|
||||
rb.FreezeRotationY = true;
|
||||
rb.FreezeRotationZ = true;
|
||||
rb.Drag = drag;
|
||||
}
|
||||
|
||||
//Transform check
|
||||
tranform = GetComponent<Transform>();
|
||||
if(tranform == null)
|
||||
Debug.LogError("tranform is NULL!");
|
||||
|
||||
//Jump setup
|
||||
float timeToApex = maxJumpTime / 2;
|
||||
gravity = (-2 * maxJumpHeight) / MathF.Pow(timeToApex, 2);
|
||||
initialJumpVel = (2 * maxJumpHeight) / timeToApex;
|
||||
|
||||
//toRemove
|
||||
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
|
||||
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
protected override void update()
|
||||
{
|
||||
if (cam == null)
|
||||
cam = GetComponentInChildren<Camera>();
|
||||
|
||||
//toRemove
|
||||
if (Input.GetKey(Input.KeyCode.G))
|
||||
{
|
||||
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
|
||||
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
|
||||
}
|
||||
|
||||
MoveKey();
|
||||
|
||||
|
||||
//Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
|
||||
}
|
||||
|
||||
protected override void fixedUpdate()
|
||||
{
|
||||
//Rotation();
|
||||
Move();
|
||||
Sprint();
|
||||
Jump();
|
||||
Gravity();
|
||||
}
|
||||
|
||||
|
||||
private void MoveKey()
|
||||
{
|
||||
/* if (Input.GetKey(Input.KeyCode.A))
|
||||
xAxisMove = -1;
|
||||
else if (Input.GetKey(Input.KeyCode.D))
|
||||
xAxisMove = 1;
|
||||
else
|
||||
xAxisMove = 0;
|
||||
|
||||
if (Input.GetKey(Input.KeyCode.W))
|
||||
zAxisMove = -1;
|
||||
else if (Input.GetKey(Input.KeyCode.S))
|
||||
zAxisMove = 1;
|
||||
else
|
||||
zAxisMove = 0;*/
|
||||
|
||||
|
||||
xAxisMove = 0;
|
||||
zAxisMove = 0;
|
||||
if (Input.GetKey(Input.KeyCode.W))
|
||||
{
|
||||
Vector3 camerAixs = cam.GetForward();
|
||||
camerAixs.y = 0;
|
||||
camerAixs.Normalise();
|
||||
xAxisMove = camerAixs.x;
|
||||
zAxisMove = camerAixs.z;
|
||||
}
|
||||
if (Input.GetKey(Input.KeyCode.S))
|
||||
{
|
||||
Vector3 camerAixs = cam.GetForward();
|
||||
camerAixs.y = 0;
|
||||
camerAixs.Normalise();
|
||||
xAxisMove = -camerAixs.x;
|
||||
zAxisMove = -camerAixs.z;
|
||||
}
|
||||
if (Input.GetKey(Input.KeyCode.A))
|
||||
{
|
||||
Vector3 camerAixs = cam.GetRight();
|
||||
camerAixs.y = 0;
|
||||
camerAixs.Normalise();
|
||||
xAxisMove = -camerAixs.x;
|
||||
zAxisMove = -camerAixs.z;
|
||||
}
|
||||
if (Input.GetKey(Input.KeyCode.D))
|
||||
{
|
||||
Vector3 camerAixs = cam.GetRight();
|
||||
camerAixs.y = 0;
|
||||
camerAixs.Normalise();
|
||||
xAxisMove = camerAixs.x;
|
||||
zAxisMove = camerAixs.z;
|
||||
}
|
||||
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
|
||||
|
||||
if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded)
|
||||
currentState = RaccoonStates.WALKING;
|
||||
|
||||
if (!isMoveKeyPress && isGrounded)
|
||||
currentState = RaccoonStates.IDILE;
|
||||
}
|
||||
|
||||
private void Move()
|
||||
{
|
||||
if (rb != null)
|
||||
{
|
||||
rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, moveForce * zAxisMove));
|
||||
|
||||
if (isMoveKeyPress)
|
||||
{
|
||||
if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel)
|
||||
{
|
||||
Vector3 v = rb.LinearVelocity;
|
||||
v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel);
|
||||
rb.LinearVelocity = v;
|
||||
}
|
||||
if (rb.LinearVelocity.z > maxMoveVel || rb.LinearVelocity.z < -maxMoveVel)
|
||||
{
|
||||
Vector3 v = rb.LinearVelocity;
|
||||
v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel);
|
||||
rb.LinearVelocity = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Sprint()
|
||||
{
|
||||
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
|
||||
{
|
||||
currentState = RaccoonStates.RUNNING;
|
||||
holdItem = false;
|
||||
if (!sprintIncreaseOnce)
|
||||
{
|
||||
sprintIncreaseOnce = true;
|
||||
oldForce = moveForce;
|
||||
moveForce *= sprintMultiplier;
|
||||
|
||||
maxOldVel = maxMoveVel;
|
||||
maxMoveVel *= sprintMultiplier;
|
||||
}
|
||||
}
|
||||
|
||||
if (Input.GetKeyUp(Input.KeyCode.LeftShift))
|
||||
{
|
||||
if(isMoveKeyPress)
|
||||
currentState = RaccoonStates.WALKING;
|
||||
sprintIncreaseOnce = false;
|
||||
moveForce = oldForce;
|
||||
maxMoveVel = maxOldVel;
|
||||
}
|
||||
}
|
||||
|
||||
//press and hold jump
|
||||
private void Jump()
|
||||
{
|
||||
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)
|
||||
{
|
||||
if (Input.GetKeyDown(Input.KeyCode.Space) && isGrounded && rb != null)
|
||||
{
|
||||
currentState = RaccoonStates.JUMP;
|
||||
Vector3 v = rb.LinearVelocity;
|
||||
v.y = initialJumpVel * 0.5f;
|
||||
rb.LinearVelocity = v;
|
||||
}
|
||||
}
|
||||
|
||||
if(rb != null && !isGrounded && (rb.LinearVelocity.y < 0.0f || Input.GetKeyUp(Input.KeyCode.Space)))
|
||||
currentState = RaccoonStates.FALLING;
|
||||
}
|
||||
|
||||
private void Rotation()
|
||||
{
|
||||
Vector3 poitionToLookAt;
|
||||
poitionToLookAt.x = xAxisMove;
|
||||
poitionToLookAt.y = 0.0f;
|
||||
poitionToLookAt.z = zAxisMove;
|
||||
|
||||
if (tranform != null)
|
||||
{
|
||||
Quaternion currentRotation = tranform.LocalRotation;
|
||||
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(poitionToLookAt, new Vector3(0.0f, 1.0f, 0.0f));
|
||||
tranform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * (float)Time.DeltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Gravity()
|
||||
{
|
||||
if (rb != null)
|
||||
{
|
||||
//check player vel.y if its close to zero its on the ground
|
||||
if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f))
|
||||
isGrounded = true;
|
||||
else
|
||||
isGrounded = false;
|
||||
|
||||
Vector3 v = rb.LinearVelocity;
|
||||
|
||||
if (isGrounded)
|
||||
v.y = groundGravity;
|
||||
else if (currentState == RaccoonStates.FALLING)
|
||||
{
|
||||
float prevYVel = v.y;
|
||||
float newYVel = v.y + (gravity * fallMultipler * (float)Time.DeltaTime);
|
||||
float nextYVel = (prevYVel + newYVel) * 0.5f;
|
||||
v.y = nextYVel;
|
||||
}
|
||||
else
|
||||
{
|
||||
float prevYVel = v.y;
|
||||
float newYVel = v.y + (gravity * (float)Time.DeltaTime);
|
||||
float nextYVel = (prevYVel + newYVel) * 0.5f;
|
||||
v.y = nextYVel;
|
||||
}
|
||||
|
||||
rb.LinearVelocity = v;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected override void onCollisionEnter(CollisionInfo info)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -19,10 +19,13 @@ namespace SHADE_Scripting
|
|||
|
||||
protected override void awake()
|
||||
{
|
||||
AddComponent<Transform>();
|
||||
|
||||
if(!GetComponent<Camera>())
|
||||
{
|
||||
AddComponent<Camera>();
|
||||
}
|
||||
GetComponent<Camera>().SetMainCamera();
|
||||
if (!GetComponent<CameraArm>())
|
||||
{
|
||||
AddComponent<CameraArm>();
|
||||
|
@ -31,25 +34,28 @@ namespace SHADE_Scripting
|
|||
}
|
||||
|
||||
protected override void update()
|
||||
{
|
||||
if (Input.GetMouseButton(Input.MouseCode.RightButton))
|
||||
{
|
||||
CameraArm arm = GetComponent<CameraArm>();
|
||||
if(arm)
|
||||
if (arm)
|
||||
{
|
||||
Vector2 vel = Input.GetMouseVelocity();
|
||||
arm.Pitch -= vel.y * turnSpeedPitch * Time.DeltaTimeF;
|
||||
arm.Yaw += vel.x * turnSpeedYaw * Time.DeltaTimeF;
|
||||
|
||||
if(arm.Pitch > pitchClamp)
|
||||
if (arm.Pitch > pitchClamp)
|
||||
{
|
||||
arm.Pitch = pitchClamp;
|
||||
}
|
||||
else if(arm.Pitch < -pitchClamp)
|
||||
else if (arm.Pitch < 0)
|
||||
{
|
||||
arm.Pitch = -pitchClamp;
|
||||
arm.Pitch = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue