- Home /
Help editing a pickup script
Hello, this is the script I am using:
var mouseOverColor = Color.blue; private var originalColor : Color; function Start () { originalColor = renderer.sharedMaterial.color; } function OnMouseEnter () { renderer.material.color = mouseOverColor; }
function OnMouseExit () { renderer.material.color = originalColor; }
function OnMouseDown () { var screenSpace = Camera.main.WorldToScreenPoint(transform.position); var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z)); while (Input.GetMouseButton(0)) { var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset; transform.position = curPosition; yield; } }
The script currently lets me grab an object if I look at it and hold the mouse button. I am trying to edit it so that I can hold and object by looking at it and pressing E. If the player presses E again, the he will drop it (like in source games). I have tried everything I can imagine, but nothing worked. Can someone help me?
Thanks.
Please Edit your question , format it so the script is readable e.g.
var mouseOverColor = Color.blue;
private var originalColor : Color;
function Start ()
{
originalColor = renderer.shared$$anonymous$$aterial.color;
}
function On$$anonymous$$ouseEnter ()
{
renderer.material.color = mouseOverColor;
}
function On$$anonymous$$ouseExit ()
{
renderer.material.color = originalColor;
}
function On$$anonymous$$ouseDown ()
{
var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.Get$$anonymous$$ouseButton(0))
{
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
transform.position = curPosition;
yield;
}
}
The script currently lets me grab an object if I look at it and hold the mouse button. I am trying to edit it so that I can hold and object by looking at it and pressing $$anonymous$$ If the player presses E again, the he will drop it (like in source games). I have tried everything I can imagine, but nothing worked. Can someone help me?
Thanks.
Answer by darkcookie · Apr 13, 2012 at 04:22 AM
var SpawnTo : Transform; //your hand for example, attack an object to your character that you want the position of what you picked up to go to
var Object1 : Transform; //what your picking up, the object that you want to move
var dist = 5; //distance at which you can pick things up
private var isHolding = false;
function Update () {
if(Input.GetKeyDown(KeyCode.E)){ //if you press 'e'
if(Vector3.Distance(transform.position, Object1.position) < dist) //if distance is less than dist variable
{
isHolding = !isHolding; //changes isHolding var from false to true
}
}
if(isHolding == true){
Object1.rigidbody.useGravity = false; //sets gravity to not on so it doesn't just fall to the ground
Object1.parent = SpawnTo; //parents the object
Object1.transform.position = SpawnTo.transform.position; //sets position
Object1.transform.rotation = SpawnTo.transform.rotation; //sets rotation
}
else{ //if isHolding isn't true
SpawnTo.transform.DetachChildren(); //detach child (object) from hand
Object1.rigidbody.useGravity = true; //add the gravity back on
}
}
Two more questions:
1.How can I make this work for multiple objects? I tried editing the object1 varible to "var Object1 : GameObject.FindWithTag("$$anonymous$$etal");" but it said I was missing a semicolon.
2.How do I make sure the player can only pick up one object at a time? Would I use a boolean script?
Answer by darkcookie · Apr 14, 2012 at 05:01 AM
private var isHighlighted : boolean = false;
var SpawnTo : Transform; //your hand for example, attack an object to your character that you want the position of what you picked up to go to
var Object1 : Transform; //what your picking up, the object that you want to move
var Object2: Transform;//second object you wantto move
var Object3: Transform;//third object you want to move
private var Alreadyholding= false;
var dist = 5; //distance at which you can pick things up
private var isHolding = false;
private var gameCharacter : Transform;
private var distance : float;
function Start() {
gameCharacter = GameObject.Find("3rd Person Controller").GetComponent(Transform);
}
function OnMouseEnter() {
if(distance <= 4.0) {
CreateInfoName();
renderer.material.color = Color.red;
isHighlighted = true;
}
}
function OnMouseExit() {
renderer.material.color = Color.white;
Destroy(GameObject.Find("infoName"));
isHighlighted = false;
}
function Update () {
distance = Mathf.Sqrt((gameCharacter.position - transform.position).sqrMagnitude);
if(Input.GetKey("e") && isHighlighted == true){
if(Vector3.Distance(transform.position, Object1.position) < dist) //if distance is less than dist variable
{
isHolding = !isHolding; //changes isHolding var from false to true
}
if(isHolding == true){
Object1.rigidbody.useGravity = false; //sets gravity to not on so it doesn't just fall to the ground
Object1.parent = SpawnTo; //parents the object
Object1.transform.position = SpawnTo.transform.position; //sets position
Object1.transform.rotation = SpawnTo.transform.rotation; //sets rotation
}
else{ //if isHolding isn't true
SpawnTo.transform.DetachChildren(); //detach child (object) from hand
Object1.rigidbody.useGravity = true; //add the gravity back on
}
}
}
function CreateInfoName(){
var infoName = new GameObject("infoName");
infoName.AddComponent(GUIText);
infoName.GetComponent(GUIText).text = gameObject.name;
infoName.transform.position = Vector3(0.5, 0.5, 0);
infoName.GetComponent(GUIText).alignment = TextAlignment.Center;
infoName.GetComponent(GUIText).anchor = TextAnchor.LowerCenter;
infoName.GetComponent(GUIText).pixelOffset = Vector2 (0, 25);
}
this method uses raycast but insures that only 1 object is held... ok this should work & also sets a little description on the cube that tells u what the item is called.....all you have to do is put this script in an empty ..or in the object u want 2 pick up ....and the just fill in the variables.....any more problems pls just make a new question ...its more likely 2 get answered faster...ohh and dont forget itsvery important that you make an empty and place it where the player is supose to be holding the object....then drag the empty game object into the player(parent it)...finaly drag the empty into the variable "spawn to" ....
Your answer
![](https://koobas.hobune.stream/wayback/20220613070252im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody2D hanging/grabbing another Rigidbody2D 0 Answers
Grab and throw object immediately 1 Answer
Unity 3d hold/grab/lift/throw/choke? 0 Answers
Holding object not working. Raycast, and first person. 1 Answer
Method for grabbing rigidbody 2 Answers