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 /
  • Help Room /
avatar image
0
Question by CharlieLama · May 27, 2016 at 09:11 AM · pick up

Picking up and carrying objects

I am trying to pick up an object and spawn it to an empty gameobject in front of my player, but its not working it doesnt even show the print in the console

{

private bool isHolding;

public GameObject spawnTo;

 RaycastHit hit;
 // Use this for initialization
 void Start()
 {

 }

 // Update is called once per frame
 void FixedUpdate()
 {


     if (Input.GetMouseButtonDown(0))
     {

         if (Physics.Raycast(this.transform.position, transform.forward, out hit, 5.0f))
         {
             if (hit.collider.tag == "Moveable" && isHolding == false)
             {
                 print("hit moveable object");
                 //hit.transform.gameObject.transform.position = spawnTo.transform.position;
                 hit.transform.gameObject.transform.parent = spawnTo.gameObject.transform;
                 hit.transform.gameObject.GetComponent<Rigidbody>().isKinematic = false;
                 isHolding = true;
                 // spawnTo
             }
    
         }

         Debug.DrawRay(this.transform.position, transform.forward*5.0f, Color.red);

         if (Input.GetMouseButtonDown(0) && isHolding == true)
         {
             hit.transform.gameObject.transform.parent = null;
             hit.transform.gameObject.GetComponent<Rigidbody>().isKinematic = true;
             isHolding = false;
         }
     }

 }

}

i could pick them up if i changed the second mousebuttondown to mousebuttonUp but i couldnt release it, and after a while the cube just dissapeared. my console still didnt print it though. thanks in advance

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 TheSorm · May 27, 2016 at 09:41 AM 0
Share

Put your mouse button request in Update not in fixed update, because the mous buttens get set on the end of every frame.

1 Reply

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

Answer by CharlieLama · May 30, 2016 at 08:45 AM

Thanks, I didn't know that, But now i did it like this :

void FixedUpdate() {

     if (Input.GetMouseButtonDown(0))
     {

         if (Physics.Raycast(this.transform.position, transform.forward, out hit, 5.0f))
         {
             PickUp();
         }

         Debug.DrawRay(this.transform.position, transform.forward*5.0f, Color.red);

      
     }

 }

and i put both my other if's in void PickUp, but it still doesnt work, it still doesnt even print my, hit moveable object, how? The ray can still detect the object, right?

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 CharlieLama · Jun 02, 2016 at 09:27 AM 0
Share

I fixed it, it was because i clicked to pick it up and then immediately drop it again this now works;

{ public bool isHolding; public GameObject spawnTo; public GameObject camera;

 RaycastHit hit;
  

 // Update is called once per frame
 void Update()
 {
     if (Input.Get$$anonymous$$ouseButtonDown(0) && isHolding)
         Drop();
     else if (Input.Get$$anonymous$$ouseButtonDown(0) && !isHolding)
     {
         if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit, 20.0f))
         {

             Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.black);
             if (hit.collider.tag == "$$anonymous$$oveable")
             {
                 Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.blue);
                 PickUp();
             }
         }
         else
         {
             Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.yellow);
         }
         
     }
     //else Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.red);


     

 }

 void PickUp()
 {
     Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.green);
     //print("hit moveable object");
     // hit.transform.gameObject.transform.position = spawnTo.transform.position;
     hit.transform.gameObject.transform.parent = spawnTo.gameObject.transform;
     hit.transform.gameObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = false;
     isHolding = true;
     // spawnTo
 }

 void Drop()
 {
     print("dropping object");
     hit.transform.gameObject.transform.parent = null;
     hit.transform.gameObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = true;
     isHolding = false;
 }

}

thanks for taking a look though.

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

57 People are following this question.

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

Related Questions

Parent Axis and Child Axis issue 1 Answer

My health potion worked and now it doesn't. 1 Answer

How to make 2 collectibles can only be reached by jumping in roll a ball tutorial 0 Answers

Picking up an object and place it in a bag 2 Answers

Make icon ReEnable after it has been picked up?,Random ReEnable of health pickup after it has been picked up? 0 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