SHADE_Y3/TempScriptsFolder/PickAndThrow.cs

98 lines
2.7 KiB
C#

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