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 Dragonfly3r · Apr 19, 2015 at 09:03 PM · c#collisioncollidergravitygun

Allow picked up object to collide with game level GameObjects

I'm currently producing a gravity gun similar to that in half life in C#. The player right clicks on an object to pick it up and it will make itself a child of an GameObject which will act as the state for carrying the object.

The parent that the object is assigned to has only a box collider and is essentially an invisible cube that the object being picked up sticks to.

The problem I'm experiencing however is that even though the object being picked up has a box collider and a rigidbody, the block is still able to be pushed through the Game level GameObjects and I have no idea how I can stop this from happening.

When the player chooses to drop the picked up object via clicking the right mouse button again then it will collide with the game level GameObjects just not when its being picked up and made an child.

I created a small workaround in which I previous had pickup.collider.isTrigger set to false on everything but since removed that as it was useless. And currently at the moment in time if you push the picked up object through an gameObject in the game level it will simply bounce out. However the way I want it is the player is unable to push the carried object through the game level GameObjects in the first place and just simply collides with it.

Any ideas?

pickUp Code

 public Quaternion   objectRot;
 public Vector3      objectPos;

 public GameObject   pickObj;
 public GameObject   pickref;

 public bool         canpick = true;
 public bool         picking = false;

   // Use this for initialization
 void Start ()
 {
     pickref = GameObject.FindWithTag("pickupref");
     pickObj = pickref;
 }

 // Update is called once per frame
 void Update ()
 {
     objectPos = transform.position;

     objectRot = transform.rotation;

     //Method here picks up the object with the right mouse button
     if (Input.GetMouseButtonDown (1) && canpick)
     {
         Debug.Log ("Object Picked Up");
         picking = true;

         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

         RaycastHit hit;

         if (Physics.Raycast (ray, out hit, 10) && hit.collider.gameObject.tag == "pickup")
         {
             pickObj = hit.collider.gameObject;
             hit.rigidbody.useGravity = false;
             hit.rigidbody.isKinematic = true;
             hit.transform.parent = gameObject.transform;
             hit.transform.position = objectPos;
             hit.transform.rotation = objectRot;
         }
     }
     
     if (Input.GetMouseButtonUp (1) && picking)
     {
         picking = false;
         canpick = false;
     }

     //This acts as a small work-around by reclicking and detecting that you haven't picked anything it will
     else if (Input.GetMouseButtonUp(1) && !picking)
     {
         canpick = true;
     }

     //Method here drops the object if it has been picked up by clicking the right mouse button
     if (Input.GetMouseButtonDown (1) && !canpick && pickObj.GetComponent<PickedUpObject> ().refuseThrow != true)
     {
         canpick = true;
         pickObj.rigidbody.useGravity = true;
         pickObj.rigidbody.isKinematic = false;
         pickObj.transform.parent = null;
         
         pickObj = pickref;

    

I've also included the code for what goes on the object being picked up in case people want to see what I'm applying to objects that are being picked up.

PickedUpObject Code

 public bool refuseThrow = false;

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag != "Player" && other.gameObject.tag != "pickto")
     {
         refuseThrow = true;
     }
 }

 void OneTriggerExit(Collider other)
 {
     if (other.gameObject.tag != "Player" && other.gameObject.tag != "pickto")
     {
         refuseThrow = false;
     }
 }
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

1 Reply

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

Answer by AlwaysSunny · Apr 19, 2015 at 09:02 PM

Didn't read the whole script, but this is not a valid way to mimic the effect observed in HL2. HL2's gravity gun did not switch the objects to kinematic (different engine, but same idea).

This can be observed when the gravity gun's towed object butts against a surface, and in edge-cases when the towed object goes nuts and spazzes out because it can't find a suitable position to occupy.

Any recreation of this feature would involve a "suggested" point, determined by the forward direction of the player. The non-kinematic towed object then uses traditional methods to attempt to come to rest at that point. When it can't, a state of equilibrium is achieved as it butts against obstacles, and occasional spasmodic hiccups will occur when it can't settle on a place to be.

The alternative to disallowing clipping would involve finding the suggestion line's hit point in the environment, extruding from that normal, checking an area around that extruded point based on the towed object's bounding box size, and placing the towed object at that position. Either way comes with its own set of problems, quirks, features.

Hope this helps,

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ParticleSystem collision checking. 2 Answers

How do I “freeze” an object after a collision? 3 Answers

Distribute terrain in zones 3 Answers

Why does the player jump is not so high if he touch 2 objects at the same time as if it touch only 1 object? 1 Answer

How to make player collide with ground? 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