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 alonr · Nov 23, 2017 at 03:33 PM · physicsrigidbodypickup

Rigidbody item pickup problem

Hello so in using the attached script the problem that this script work only while the pick up button is pressed i want it to work on click to pick up and hover and on click to release Any idea?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class GrabObjectClass
 {
     public bool m_FreezeRotation;
     public float m_PickupRange = 3f;
     public float m_ThrowStrength = 50f;
     public float m_distance = 3f;
     public float m_maxDistanceGrab = 4f;
 }
 
 [System.Serializable]
 public class ItemGrabClass
 {
     public bool m_FreezeRotation;
     public float m_ItemPickupRange = 2f;
     public float m_ItemThrow = 45f;
     public float m_ItemDistance = 1f;
     public float m_ItemMaxGrab = 2.5f;
 }
 
 [System.Serializable]
 public class DoorGrabClass
 {
     public float m_DoorPickupRange = 2f;
     public float m_DoorThrow = 10f;
     public float m_DoorDistance = 2f;
     public float m_DoorMaxGrab = 3f;
 }
 
 [System.Serializable]
 public class TagsClass
 {
     public string m_InteractTag = "Interact";
     public string m_InteractItemsTag = "InteractItem";
     public string m_DoorsTag = "Door";
 }
 
 public class DragRigidbodyUse : MonoBehaviour
 {
 
     public GameObject playerCam;
 
     public string GrabButton = "Grab";
     public string ThrowButton = "Throw";
     public string UseButton = "Use";
     public GrabObjectClass ObjectGrab = new GrabObjectClass();
     public ItemGrabClass ItemGrab = new ItemGrabClass();
     public DoorGrabClass DoorGrab = new DoorGrabClass();
     public TagsClass Tags = new TagsClass();
 
     private float PickupRange = 3f;
     private float ThrowStrength = 50f;
     private float distance = 3f;
     private float maxDistanceGrab = 4f;
 
     private Ray playerAim;
     private GameObject objectHeld;
     private bool isObjectHeld;
     private bool tryPickupObject;
 
     void Start()
     {
         isObjectHeld = false;
         tryPickupObject = false;
         objectHeld = null;
     }
 
     void FixedUpdate()
     {
         if (Input.GetButton(GrabButton))
         {
             if (!isObjectHeld)
             {
                 tryPickObject();
                 tryPickupObject = true;
             }
             else {
                 holdObject();
             }
         }
         else if (isObjectHeld)
         {
             DropObject();
         }
 
         if (Input.GetButton(ThrowButton) && isObjectHeld)
         {
             isObjectHeld = false;
             objectHeld.GetComponent<Rigidbody>().useGravity = true;
             ThrowObject();
         }
 
         if (Input.GetButton(UseButton))
         {
             isObjectHeld = false;
             tryPickObject();
             tryPickupObject = false;
             Use();
         }
     }
 
     private void tryPickObject()
     {
         Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
         RaycastHit hit;
 
         if (Physics.Raycast(playerAim, out hit, PickupRange))
         {
             objectHeld = hit.collider.gameObject;
             if (hit.collider.tag == Tags.m_InteractTag && tryPickupObject)
             {
                 isObjectHeld = true;
                 objectHeld.GetComponent<Rigidbody>().useGravity = false;
                 if (ObjectGrab.m_FreezeRotation)
                 {
                     objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
                 }
                 if (ObjectGrab.m_FreezeRotation == false)
                 {
                     objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
                 }
                 /**/
                 PickupRange = ObjectGrab.m_PickupRange;
                 ThrowStrength = ObjectGrab.m_ThrowStrength;
                 distance = ObjectGrab.m_distance;
                 maxDistanceGrab = ObjectGrab.m_maxDistanceGrab;
             }
             if (hit.collider.tag == Tags.m_InteractItemsTag && tryPickupObject)
             {
                 isObjectHeld = true;
                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
                 if (ItemGrab.m_FreezeRotation)
                 {
                     objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
                 }
                 if (ItemGrab.m_FreezeRotation == false)
                 {
                     objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
                 }
                 /**/
                 PickupRange = ItemGrab.m_ItemPickupRange;
                 ThrowStrength = ItemGrab.m_ItemThrow;
                 distance = ItemGrab.m_ItemDistance;
                 maxDistanceGrab = ItemGrab.m_ItemMaxGrab;
             }
             if (hit.collider.tag == Tags.m_DoorsTag && tryPickupObject)
             {
                 isObjectHeld = true;
                 objectHeld.GetComponent<Rigidbody>().useGravity = true;
                 objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
                 /**/
                 PickupRange = DoorGrab.m_DoorPickupRange;
                 ThrowStrength = DoorGrab.m_DoorThrow;
                 distance = DoorGrab.m_DoorDistance;
                 maxDistanceGrab = DoorGrab.m_DoorMaxGrab;
             }
         }
     }
 
     private void holdObject()
     {
         Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
 
         Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
         Vector3 currPos = objectHeld.transform.position;
 
         objectHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * 10;
         
         if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
         {
             DropObject();
         }
     }
 
     private void DropObject()
     {
         isObjectHeld = false;
         tryPickupObject = false;
         objectHeld.GetComponent<Rigidbody>().useGravity = true;
         objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
         objectHeld = null;
     }
 
     private void ThrowObject()
     {
         objectHeld.GetComponent<Rigidbody>().AddForce(playerCam.transform.forward * ThrowStrength);
         objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
         objectHeld = null;
     }
 
     private void Use()
     {
         objectHeld.SendMessage("UseObject", SendMessageOptions.DontRequireReceiver); //Every script attached to the PickupObject that has a UseObject function will be called.
         objectHeld = null;
     }
 }
Comment
Add comment
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

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

95 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 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

Rigidbody Position Changes don't work when moving. 0 Answers

Character Controller children of a car: Inertia effect, delay effect, what??? 1 Answer

How to limit the motion direction of a GameObject? 1 Answer

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

How to make Rigidbody.AddForce less delayed in Unity3D? 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