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 Razion · Aug 08, 2012 at 11:15 PM · collisionmovementrigidbody

Player-movable rigidbodies w/ collision?

Working with a script that allows players to move (pick up) rigidbodies but can't figure out exactly what direction I should be headed to allow the rigidbodies to still have collision while being moved. Is there a way to move rigidbodies in a predictable manner while still having them be physics aware?

 using UnityEngine;

using System.Collections;

public class ObjectSelection : MonoBehaviour { Transform selectedObj = null; RaycastHit hit; float dist; Vector3 newPos; Camera cam;

 // Use this for initialization
 void Start () {
     cam = Camera.mainCamera;
 }
 
 // Update is called once per frame
 void Update () {
     if(Input.GetMouseButton(0)){
         if(!selectedObj){
             if(Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, 5.0f) && hit.transform.tag == "Block"){
                 Debug.Log ("Selectable Object");
                     if(hit.rigidbody){
                         hit.rigidbody.velocity = Vector3.zero;
                         hit.rigidbody.freezeRotation = true;
                     }
                 selectedObj = hit.transform;
                 dist = Vector3.Distance(selectedObj.position, cam.transform.position);
             }
         } else {
             newPos = cam.transform.position + (cam.transform.forward * 5);
             selectedObj.position = newPos;
         } 
     } else{
         selectedObj = null;
     }
 }

}

Edit: Tried playing around with kinematics to kill time, but that was clearly not a step in the right direction. Any other ideas?

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 Razion · Aug 09, 2012 at 11:27 PM 0
Share

Sorry, should also specify that the rigidbodies essentially phase through other objects while being moved, in case I didn't make that clear in my original post.

2 Replies

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

Answer by Seth-Bergman · Aug 09, 2012 at 11:38 PM

you should child the object to the player, instead of directly adjusting it's transform, which will ignore physics:

 void Start () {
     cam = Camera.mainCamera;
 }
 
 // Update is called once per frame
 void Update () {
     if(Input.GetMouseButton(0)){
         if(!selectedObj){
             if(Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, 5.0f) && hit.transform.tag == "Block"){
                 Debug.Log ("Selectable Object");
                     if(hit.rigidbody){
                         hit.rigidbody.velocity = Vector3.zero;
                         hit.rigidbody.freezeRotation = true;
                     }
                 selectedObj = hit.transform;
                 dist = Vector3.Distance(selectedObj.position, cam.transform.position);
                 selectedObj.parent = cam;
             }
         }
     } else{
         selectedObj.parent = null;
         selectedObj = null;
     }
 }

something like this

Comment
Add comment · Show 3 · 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 Seth-Bergman · Aug 09, 2012 at 11:49 PM 0
Share

http://answers.unity3d.com/questions/176871/Parenting-1-objectRemoving-child-pick-up.html

can't find the thread I wanted, but here's another example anyway

avatar image Razion · Aug 10, 2012 at 10:01 PM 0
Share

Been playing around with this for a while now. It definitely allows me to pick up the object, and when I turn the object rotates around as you would expect...But it doesn't move at all when the player moves. It only responds to rotation.

Sorry for the poor video quality, but here's an example: http://www.youtube.com/watch?v=CLN48XbhGNY&feature=youtu.be

Any ideas?

avatar image Razion · Aug 11, 2012 at 12:23 AM 0
Share

Solved with a combination of this and rigidbody.$$anonymous$$ovePosition();

Thanks all!

avatar image
0

Answer by Lachee1 · Aug 09, 2012 at 11:32 PM

Have you tried the DragRigidbody script? just mess around with that until you have soething you need, i added the ability to move the objects in and out in one of my games. Its a handy script to have and it uses collisions aswell

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

9 People are following this question.

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

Related Questions

How to get transform.Translate to work with rigidbodies 2 Answers

How can I make a non-player character (rigidbody) move when they touch the ground? 1 Answer

Using rigidbody for collisions only, not movements? 1 Answer

Moving a camera (character) based on collision 1 Answer

can not move my enemy with rigidbody 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