Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CupOfThings · Jan 02, 2016 at 08:49 AM · scripting problempickupholdplace

Pick up, drop and place tagged objects

Hi, with my script I am able to pick up and drop objects tagged as "Pick" with the E key but the bottom of half of the script does not work. What I am trying to do is allow the player to not only pick up and drop an object but place it where they choose to with the Q key, if an object has been placed it will stay in its position regardless of gravity but still contain its collider.

However, when the Q key is pressed I get the message in the console "Object placed" and I receive no errors but the object that is being held does nothing, can someone help me out with this? thanks.

EDIT

I've managed to get the picked up object to detach from the player when "Q" is pressed however the object drops to the ground since its gravity turns back on even though near the bottom of my script it sets the objects gravity to false. I found out that this is due to the code under the "else" that sets the objects gravity to true despite what any other line of code tells it to do. Is there a way I can set lines 36 & 37 so that they only initiate once when they are told to do so? I've also updated my code.

 var hitObject : GameObject;
 var SpawnTo : Transform;
 var hit : RaycastHit;
 
 var runOnce : boolean = false;
 var pickedUp = false;
 
 function Update(){
 if(Input.GetKeyDown(KeyCode.E)){
 
 if(Physics.Raycast(transform.position,transform.forward,hit,3)){
 if(hit.collider.gameObject.tag == "Pick"){
 
 pickedUp = !pickedUp;
   }
  }
 }
 
 if(pickedUp == true){
 hitObject = hit.collider.gameObject;
 hitObject.rigidbody.useGravity = false;
 hitObject.rigidbody.isKinematic = true;
 hitObject.rigidbody.detectCollisions = false;
 hitObject.transform.parent = SpawnTo.transform;
 hitObject.transform.position = SpawnTo.transform.position;
 hitObject.GetComponent(RotateObject).enabled = true;
 
 if(!runOnce){
 runOnce = true;
 print("ooonnnccceee");
 hitObject.transform.localEulerAngles = new Vector3(0,0,0);
 
 }}else{
 
 SpawnTo.transform.DetachChildren();
 hitObject.rigidbody.useGravity = true;
 hitObject.rigidbody.isKinematic = false;
 hitObject.rigidbody.detectCollisions = true;
 hitObject.GetComponent(RotateObject).enabled = false;
 runOnce = false;
 }
 
 if(Input.GetKeyDown(KeyCode.Q) && pickedUp == true){
 
 print("Object placed");
 SpawnTo.transform.DetachChildren();
 hitObject.GetComponent(RotateObject).enabled = false;
 pickedUp = false;
  }
 }
Comment
Add comment · Show 4
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 Firedan1176 · Jan 04, 2016 at 02:56 AM 1
Share

replace the last line with:

 hitObject.GetComponent<RotateObject>().enabled = false;
avatar image CupOfThings Firedan1176 · Jan 08, 2016 at 07:20 PM 0
Share

Sorry mate this gives me a bunch of errors :/

avatar image vintar · Jan 09, 2016 at 09:29 PM 0
Share

you set :

hitObject.rigidbody.useGravity = false; hitObject.rigidbody.is$$anonymous$$inematic = true;

after dropping the object. This will just make it stay where it is, change to :

hitObject.rigidbody.useGravity = true; hitObject.rigidbody.is$$anonymous$$inematic = false;

avatar image CupOfThings vintar · Jan 11, 2016 at 11:51 PM 0
Share

Setting the objects gravity to false is what I want so that it stays in its place, ive updated my question and code.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

pick up item script issue 3 Answers

Help. Newbie trying to pick up, put down, and throw two kinds of boxes in a 2D space. Thought it would be easy 1 Answer

Help needed for begginer. With scripting items. 1 Answer

Picking up and dropping objects with parenting 1 Answer

Can't increase my score after OnTriggerEnter occurs. 1 Answer


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