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 Mika412 · Aug 12, 2013 at 01:40 PM · positionrotate objectpickupmove object

Picked up object passes through other objects

I want to pick up an object and rotate it. But when i picked it up it passes through other objects. Please help. Here is my code.

 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 :float=1.1; //distance at which you can pick things up
 private var isHolding = false;
  
  
  
 var speed : int;
 var friction : float;
 var lerpSpeed : float;
 private var xDeg : float;
 private var yDeg : float;
 private var fromRotation : Quaternion;
 private var toRotation : Quaternion;
  
  
 function Start(){
  
     SpawnTo=GameObject.FindWithTag("Distance").transform;
     Object1=transform;
  
  
 } 
  
  
  
 function Update () {
     if(Input.GetKeyDown(KeyCode.E)){ //if you press 'e'
         if(Vector3.Distance(SpawnTo.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
  
  
  
         if(Input.GetMouseButton(0)) {
         xDeg += Input.GetAxis("Mouse X") * speed * friction;
         yDeg -= Input.GetAxis("Mouse Y") * speed * friction;
         fromRotation = transform.rotation;
         toRotation = Quaternion.Euler(yDeg,xDeg,0);
         SpawnTo.transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
         }
  
  
  
         }
         else{ //if isHolding isn't true
             SpawnTo.transform.DetachChildren(); //detach child (object) from hand
             Object1.rigidbody.useGravity = false; //add the gravity back on
         }
 }
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
1
Best Answer

Answer by save · Aug 12, 2013 at 01:49 PM

A common solution is to render the object on top of all other objects. This is usually done to all other objects which belongs to the 3d-HUD as well, such as hands and weapons for instance. Either through changing material where the shader is rendered on top or a second camera which only sees such objects. The fastest solution is an additional camera which you parent to the same position where your World Camera is, the trick is that you render it afterwards.

Example of camera depth order:

  1. World Camera

  2. Picked up object Camera

  3. GUI Camera

When you pick up an object, you send the object to [another layer][1] which only the HUD-camera sees. To change layer you use:

 gameObject.layer = layerNumber;

Example for your specific situation:

 // Change Object1's GameObject to layer number 7
 Object1.gameObject.layer = 7;

  • side note, Object1 could equally be typed as a GameObject and you should cache the transform component instead.*

For more information about cameras, please see the [documentation][2], specifically Depth Only.

Depth Only

If you wanted to draw a player's gun without letting it get clipped inside the environment, you would set one Camera at Depth 0 to draw the environment, and another Camera at Depth 1 to draw the weapon alone. The weapon Camera's Clear Flags should be set to to depth only. This will keep the graphical display of the environment on the screen, but discard all information about where each object exists in 3-D space. When the gun is drawn, the opaque parts will completely cover anything drawn, regardless of how close the gun is to the wall. [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject-layer.html [2]: http://docs.unity3d.com/Documentation/Components/class-Camera.html
Comment
Add comment · Show 4 · 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 Wuzseen · Aug 12, 2013 at 01:59 PM 1
Share

It's important to note that physically the items still collide in the game world, but it isn't rendered as such. It is a workaround to handle the clipping issue. If you're worried about collision problems, the solution could be a lot tougher.

avatar image save · Aug 12, 2013 at 02:06 PM 0
Share

Good input @Wuzseen. Using collider.enabled / collider.isTrigger and raycasting or spherecasting on release to confirm space for positioning would be a quick solution.

avatar image Mika412 · Aug 12, 2013 at 02:56 PM 0
Share

Thank you, it worked. But now I have another problem. When I go to the wall it looks like it's not inside it. But when I let go the object, it stays inside.

avatar image save · Aug 12, 2013 at 03:02 PM 0
Share

That's part of the issue we were mentioning in the previous comments. I'd suggest you use raycasting to see where, from the player's point of view, to position a released object.

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

16 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

Related Questions

Cannot rotate an object around the center 5 Answers

Rotate an object back to its old rotation 1 Answer

Camera rotation around player while following. 6 Answers

Will this code move an object 2 Answers

Weapon Pickup and Drop 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