Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by skioddard · Apr 13, 2012 at 02:32 AM · grabholdpick up

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Apr 13, 2012 at 04:17 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
         }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skioddard · Apr 14, 2012 at 02:02 AM 0
Share

Thank you!

avatar image skioddard · Apr 14, 2012 at 03:00 AM 0
Share

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?

avatar image
0

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);
 
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darkcookie · Apr 14, 2012 at 05:02 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges