2022-11-02 00:25:43 +08:00
|
|
|
|
using SHADE;
|
|
|
|
|
using System;
|
|
|
|
|
using static PlayerController;
|
|
|
|
|
|
|
|
|
|
public class PickAndThrow : Script
|
|
|
|
|
{
|
|
|
|
|
private PlayerController pc;
|
2022-11-02 17:31:57 +08:00
|
|
|
|
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;
|
2022-11-02 00:25:43 +08:00
|
|
|
|
public PickAndThrow(GameObject gameObj) : base(gameObj) { }
|
2022-11-02 17:31:57 +08:00
|
|
|
|
|
2022-11-02 00:25:43 +08:00
|
|
|
|
protected override void awake()
|
|
|
|
|
{
|
|
|
|
|
pc = GetScript<PlayerController>();
|
2022-11-02 17:31:57 +08:00
|
|
|
|
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");
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|
|
|
|
|
protected override void update()
|
|
|
|
|
{
|
2022-11-02 17:31:57 +08:00
|
|
|
|
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)
|
2022-11-02 00:25:43 +08:00
|
|
|
|
{
|
2022-11-02 17:31:57 +08:00
|
|
|
|
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}");
|
|
|
|
|
}
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|
2022-11-02 17:31:57 +08:00
|
|
|
|
else if(!pc.holdItem)
|
|
|
|
|
itemRidibody.IsGravityEnabled = true;
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|
|
|
|
|
protected override void onCollisionEnter(CollisionInfo info)
|
|
|
|
|
{
|
2022-11-02 17:31:57 +08:00
|
|
|
|
}
|
|
|
|
|
protected override void onTriggerEnter(CollisionInfo info)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("ENTER");
|
|
|
|
|
if (info.GameObject == item && !pc.holdItem)
|
2022-11-02 00:25:43 +08:00
|
|
|
|
{
|
2022-11-02 17:31:57 +08:00
|
|
|
|
inRange = true;
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-02 17:31:57 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|