120 lines
3.2 KiB
C#
120 lines
3.2 KiB
C#
using SHADE;
|
|
using System;
|
|
using static PlayerController;
|
|
|
|
public class PickAndThrow : Script
|
|
{
|
|
public Vector3 holdPosition = new Vector3(0.0f, 1.0f, 0.0f);
|
|
public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f);
|
|
public GameObject item { get; set; }
|
|
private PlayerController pc;
|
|
private Camera cam;
|
|
private Transform itemTransform;
|
|
private RigidBody itemRidibody;
|
|
private Transform raccoonHoldLocation;
|
|
private float lastXDir;
|
|
private float lastZDir;
|
|
private bool inRange = false;
|
|
|
|
protected override void awake()
|
|
{
|
|
pc = GetScript<PlayerController>();
|
|
raccoonHoldLocation = GetComponentInChildren<Transform>();
|
|
if (raccoonHoldLocation == null)
|
|
Debug.Log("CHILD EMPTY");
|
|
else
|
|
raccoonHoldLocation.LocalPosition = holdPosition;
|
|
}
|
|
protected override void update()
|
|
{
|
|
CalculateDir();
|
|
|
|
if (pc != null && pc.holdItem && itemRidibody != null && itemTransform != null)
|
|
{
|
|
itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition;
|
|
itemRidibody.IsGravityEnabled = false;
|
|
itemRidibody.LinearVelocity = Vector3.Zero;
|
|
itemRidibody.AngularVelocity = Vector3.Zero;
|
|
|
|
if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
|
|
{
|
|
pc.holdItem = false;
|
|
inRange = false;
|
|
itemRidibody.IsGravityEnabled = true;
|
|
itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir));
|
|
itemRidibody.LinearVelocity += pc.rb.LinearVelocity;
|
|
ResetItemObject();
|
|
}
|
|
}
|
|
else if(!pc.holdItem && itemRidibody != null)
|
|
itemRidibody.IsGravityEnabled = true;
|
|
|
|
if (pc != null && !pc.holdItem && inRange && Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
|
|
{
|
|
pc.holdItem = true;
|
|
RetrieveItemComponets();
|
|
}
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
protected override void onCollisionEnter(CollisionInfo info)
|
|
{
|
|
}
|
|
protected override void onTriggerEnter(CollisionInfo info)
|
|
{
|
|
//Debug.Log("ENTER");
|
|
if (info.GameObject.GetScript<Item>() != null && !pc.holdItem)
|
|
{
|
|
item = info.GameObject;
|
|
inRange = true;
|
|
}
|
|
}
|
|
protected override void onTriggerStay(CollisionInfo info)
|
|
{
|
|
//Debug.Log("STAY");
|
|
}
|
|
protected override void onTriggerExit(CollisionInfo info)
|
|
{
|
|
//Debug.Log("EXIT");
|
|
if (info.GameObject.GetScript<Item>() != null && !pc.holdItem)
|
|
{
|
|
inRange = false;
|
|
}
|
|
}
|
|
|
|
} |