added getright in camera.cxx/.hxx

PickandThrow is done
adjusted thirdPersonCamera and PlayerController to work with each other
This commit is contained in:
Glence 2022-11-02 17:31:57 +08:00
parent 218fc89fc8
commit fc56973645
6 changed files with 182 additions and 43 deletions

View File

@ -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
(

View File

@ -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);
}
}

View File

@ -66,5 +66,7 @@ namespace SHADE
void SetMainCamera();
void LookAt(Vector3 targetPosition);
Vector3 GetForward();
Vector3 GetRight();
};
}

View File

@ -5,26 +5,94 @@ using static PlayerController;
public class PickAndThrow : Script
{
private PlayerController pc;
public uint itemEID;
Transform itemHoldLocation;
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>();
itemHoldLocation = GetComponentInChildren<Transform>();
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 != null && pc.holdItem)
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)
{
if (info.GameObject.Name == "item" && Input.GetKey(Input.KeyCode.E))
}
protected override void onTriggerEnter(CollisionInfo info)
{
pc.holdItem = true;
itemEID = info.GameObject.EntityId;
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;
}
}
}

View File

@ -12,17 +12,17 @@ public class PlayerController : Script
RUNNING,
JUMP,
FALLING,
HOLDING,
CAUGHT,
TOTAL
}
private RigidBody rb;
public RigidBody rb;
private Transform tranform;
private Camera cam;
//to be remove
public float drag = 2.0f;
public bool holdItem = false;
public bool holdItem { get; set; }
[SerializeField]
[Tooltip("The current state fo the raccoon")]
public RaccoonStates currentState = RaccoonStates.IDILE;
@ -41,9 +41,11 @@ public class PlayerController : Script
private float oldForce;
private float maxOldVel;
private bool sprintIncreaseOnce = false;
private int xAxisMove;
private int zAxisMove;
private bool isMoveKeyPress = false;
public float xAxisMove { get; set; }
public float zAxisMove { get; set; }
public bool isMoveKeyPress { get; set; }
[SerializeField]
[Tooltip("curr not working")]
@ -68,12 +70,21 @@ public class PlayerController : Script
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>();
@ -92,6 +103,9 @@ public class PlayerController : Script
protected override void update()
{
if (cam == null)
cam = GetComponentInChildren<Camera>();
//toRemove
if (Input.GetKey(Input.KeyCode.G))
{
@ -117,7 +131,7 @@ public class PlayerController : Script
private void MoveKey()
{
if (Input.GetKey(Input.KeyCode.A))
/* if (Input.GetKey(Input.KeyCode.A))
xAxisMove = -1;
else if (Input.GetKey(Input.KeyCode.D))
xAxisMove = 1;
@ -129,8 +143,43 @@ public class PlayerController : Script
else if (Input.GetKey(Input.KeyCode.S))
zAxisMove = 1;
else
zAxisMove = 0;
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)
@ -169,7 +218,8 @@ public class PlayerController : Script
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
{
currentState = RaccoonStates.RUNNING;
if(!sprintIncreaseOnce)
holdItem = false;
if (!sprintIncreaseOnce)
{
sprintIncreaseOnce = true;
oldForce = moveForce;

View File

@ -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;
}
}
}
}
}
}