I posted this in the forums on accident. Anyways, I have 3 objects I want to pick up and throw. Script works fine for object1. I can pick up object2 and object3, but the 2nd and 3rd IF statements revert back to object1 (checked with all the DEBUG.LOG code). Why is that? I tried an array but I have no idea how to get that to work properly…
Also, I could use pointers on simplifying code. I think I’m overcomplicating a lot of it. Thanks in advance!
#pragma strict
var playerDist : float;
var pickupDistance : float = 2.0;
var throwForce : int = 10;
var playerTrans : Transform;
var objectGoal : Transform;
var objectToBe : GameObject;
var playerAboveObj : float;
static var beingHeld : boolean;
function Start ()
{
playerTrans = gameObject.FindWithTag("Player").transform;
objectGoal = gameObject.Find("Launcher").transform;
beingHeld = false;
}
function simpleWait(waitTime : float)
{
yield WaitForSeconds(waitTime);
}
function pickupObject()
{
playerDist = Vector3.Distance(playerTrans.position, this.gameObject.transform.position);
playerAboveObj = Mathf.Abs((playerTrans.position - this.gameObject.transform.position).y);
if(Input.GetButtonDown("Fire2") && playerDist < pickupDistance && beingHeld == false && playerAboveObj < 0.9)
{
this.gameObject.collider.enabled = false;
this.gameObject.rigidbody.isKinematic = true;
this.gameObject.rigidbody.useGravity = false;
this.gameObject.transform.parent = objectGoal.gameObject.transform;
this.gameObject.transform.localPosition = Vector3(objectGoal.localPosition.x, objectGoal.localPosition.y + 0.5,
objectGoal.localPosition.z + 1);
Debug.Log(this.gameObject.name);
beingHeld = true;
}
if(Input.GetButtonDown("Fire1") && beingHeld == true)
{
Debug.Log("LEFT CLICK");
pickupDistance = 0.0;
beingHeld = true;
StartCoroutine(simpleWait(0.5));
this.gameObject.rigidbody.isKinematic = false;
this.gameObject.rigidbody.useGravity = true;
this.gameObject.collider.enabled = true;
this.gameObject.transform.parent = null;
this.gameObject.rigidbody.AddForce(objectGoal.forward * throwForce, ForceMode.Impulse);
StartCoroutine(simpleWait(0.5));
pickupDistance = 2.0;
beingHeld = false;
Debug.Log(this.gameObject.name);
}
if(Input.GetButtonUp("Fire2") && beingHeld == true)
{
Debug.Log("DROPPED");
this.gameObject.rigidbody.isKinematic = false;
this.gameObject.rigidbody.useGravity = true;
this.gameObject.collider.enabled = true;
this.gameObject.transform.parent = null;
beingHeld = false;
Debug.Log(this.gameObject.name);
}
}
function Update ()
{
pickupObject();
}