- Home /
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?
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.
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
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
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?
Solved with a combination of this and rigidbody.$$anonymous$$ovePosition();
Thanks all!
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
Your answer
Follow this Question
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