2022-11-02 00:25:43 +08:00
|
|
|
|
using SHADE;
|
|
|
|
|
using System;
|
|
|
|
|
using static PlayerController;
|
|
|
|
|
|
|
|
|
|
public class PickAndThrow : Script
|
|
|
|
|
{
|
2022-11-10 13:06:37 +08:00
|
|
|
|
public Vector3 holdPosition = new Vector3(0.0f, 1.0f, 0.0f);
|
2022-11-04 17:35:09 +08:00
|
|
|
|
public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f);
|
2022-11-13 21:56:28 +08:00
|
|
|
|
public GameObject item { get; set; }
|
2022-11-04 17:31:53 +08:00
|
|
|
|
private PlayerController pc;
|
|
|
|
|
private Camera cam;
|
2022-11-02 17:31:57 +08:00
|
|
|
|
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
|
|
|
|
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");
|
2022-11-03 01:50:02 +08:00
|
|
|
|
else
|
2022-11-10 13:06:37 +08:00
|
|
|
|
raccoonHoldLocation.LocalPosition = holdPosition;
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|
|
|
|
|
protected override void update()
|
|
|
|
|
{
|
2022-11-13 21:56:28 +08:00
|
|
|
|
CalculateDir();
|
2022-11-02 17:31:57 +08:00
|
|
|
|
|
2022-11-10 13:06:37 +08:00
|
|
|
|
if (pc != null && pc.holdItem && itemRidibody != null && itemTransform != null)
|
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;
|
2022-11-04 17:31:53 +08:00
|
|
|
|
inRange = false;
|
2022-11-02 17:31:57 +08:00
|
|
|
|
itemRidibody.IsGravityEnabled = true;
|
|
|
|
|
itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir));
|
|
|
|
|
itemRidibody.LinearVelocity += pc.rb.LinearVelocity;
|
2022-11-13 21:56:28 +08:00
|
|
|
|
ResetItemObject();
|
2022-11-02 17:31:57 +08:00
|
|
|
|
}
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|
2022-11-04 17:31:53 +08:00
|
|
|
|
else if(!pc.holdItem && itemRidibody != null)
|
2022-11-02 17:31:57 +08:00
|
|
|
|
itemRidibody.IsGravityEnabled = true;
|
2022-11-13 21:56:28 +08:00
|
|
|
|
|
|
|
|
|
if (pc != null && !pc.holdItem && inRange && Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
|
|
|
|
|
{
|
|
|
|
|
pc.holdItem = true;
|
|
|
|
|
RetrieveItemComponets();
|
|
|
|
|
}
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|
2022-11-10 13:06:37 +08:00
|
|
|
|
|
|
|
|
|
private void ResetItemObject()
|
|
|
|
|
{
|
|
|
|
|
itemRidibody = null;
|
|
|
|
|
itemTransform = null;
|
|
|
|
|
item = new GameObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RetrieveItemComponets()
|
|
|
|
|
{
|
|
|
|
|
//get the transform of the given item
|
|
|
|
|
if (item.GetScript<Item>() != null && itemTransform == null && itemRidibody == null)
|
|
|
|
|
{
|
|
|
|
|
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-13 21:56:28 +08:00
|
|
|
|
private void CalculateDir()
|
|
|
|
|
{
|
|
|
|
|
if (cam == null)
|
|
|
|
|
cam = GetComponentInChildren<Camera>();
|
|
|
|
|
else if (cam != null)
|
|
|
|
|
{
|
|
|
|
|
Vector3 camerAixs = cam.GetForward();
|
|
|
|
|
camerAixs.y = 0;
|
|
|
|
|
camerAixs.Normalise();
|
|
|
|
|
lastXDir = camerAixs.x;
|
|
|
|
|
lastZDir = camerAixs.z;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-10 13:06:37 +08:00
|
|
|
|
|
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)
|
|
|
|
|
{
|
2022-11-04 17:31:53 +08:00
|
|
|
|
if (info.GameObject.GetScript<Item>() != null && !pc.holdItem)
|
2022-11-02 00:25:43 +08:00
|
|
|
|
{
|
2022-11-04 17:31:53 +08:00
|
|
|
|
item = info.GameObject;
|
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)
|
|
|
|
|
{
|
2022-11-04 17:31:53 +08:00
|
|
|
|
//Debug.Log("STAY");
|
2022-11-02 17:31:57 +08:00
|
|
|
|
}
|
|
|
|
|
protected override void onTriggerExit(CollisionInfo info)
|
|
|
|
|
{
|
2022-11-04 17:31:53 +08:00
|
|
|
|
//Debug.Log("EXIT");
|
|
|
|
|
if (info.GameObject.GetScript<Item>() != null && !pc.holdItem)
|
2022-11-02 17:31:57 +08:00
|
|
|
|
{
|
|
|
|
|
inRange = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 00:25:43 +08:00
|
|
|
|
}
|