- Home /
Question by
jakobdv · Feb 10, 2014 at 01:40 AM ·
objectholdridgidbodypick up
TK script: selecting all objects
I am working on a telekinesis script, it has been working very well on single objects. However, when I try to apply it to multiple objects, every object in the scene that has the script attached becomes picked up, even thought this should only be happening to one. Here is the script:
#pragma strict
var object: GameObject;//the object the script is attached to
var PickUp: GameObject;// an empty game object placed in front of the camera that the object will be parented to
private var Distance : int;
private var MaxDistance : int;
var ObjectWeight : int; //Determines how close the player must be to something in order to pick it up
function Update () {
MaxDistance = ObjectWeight/2;
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 0));
var cameraTransform = PickUp.transform;
var Pick = object.transform;
if (Input.GetMouseButtonDown(0)){
if (Physics.Raycast (ray, hit))
{
Distance = hit.distance;
if (Distance <= MaxDistance){
Pick.transform.position = Vector3(cameraTransform.position.x,cameraTransform.position.y,cameraTransform.position.z);
Pick.parent = cameraTransform;
Pick.transform.localEulerAngles = cameraTransform.transform.localEulerAngles;
Destroy (rigidbody);
if the mouse button is clicked while pointing at the object, then pick it up
}
}
}
if (Input.GetMouseButtonUp(0)){
Pick.parent = null;
gameObject.AddComponent(Rigidbody);
//if mouse is let go, then just drop the object
}
if (Input.GetKeyDown(KeyCode.E)){
Pick.parent = null;
gameObject.AddComponent(Rigidbody);
gameObject.rigidbody.AddForce(gameObject.transform.forward * 1000);
//If e is pressed, then throw the object
}
}
any way that i can make it so this script only applies to one object at a time? any help would be GREATLY appreciated!
Comment
Your answer
Follow this Question
Related Questions
I can't make my object grounded and to move it 2 Answers
Pick up objects 2 Answers
objects to move when picked up 0 Answers
Playing a range of sounds from an object 0 Answers
How to draw GUI Text from code 1 Answer