- Home /
Dragged object keeping it's original rotation.
I'm trying to make the character pick up and drag an object, when he rotates the object it should keep facing the character/camera while keeping it's original rotation. I've already tried Transform.LookAt but that makes the object's forward vector face the camera.
Here's my code (Drag() decides the final position):
var grabbed : Transform;
var grabDistance : float = 10.0f;
var grabLayerMask : int;
var grabOffset : Vector3; //delta between transform transform position and hit point
var useToggleDrag : boolean; // Didn't know which style you prefer.
function Update () {
if (useToggleDrag){
UpdateToggleDrag();
} else {
UpdateHoldDrag();
}
}
// Toggles drag with mouse click
function UpdateToggleDrag () {
if (Input.GetMouseButtonDown(0)){
Grab();
} else {
if (grabbed) {
Drag();
}
}
}
// Drags when user holds down button
function UpdateHoldDrag () {
if (Input.GetMouseButton(0)) {
if (grabbed){
Drag();
} else {
Grab();
}
} else {
if(grabbed){
//restore the original layermask
grabbed.gameObject.layer = grabLayerMask;
}
grabbed = null;
}
}
function Grab() {
if (grabbed){
grabbed = null;
} else {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)){
if(hit.collider.gameObject.GetComponent.<Rigidbody>() != null) {
grabbed = hit.transform;
if(grabbed.parent){
grabbed = grabbed.parent.transform;
}
//set the object to ignore raycasts
grabLayerMask = grabbed.gameObject.layer;
grabbed.gameObject.layer = 2;
//now immediately do another raycast to calculate the offset
if (Physics.Raycast(ray, hit)){
grabOffset = grabbed.position - hit.point;
} else {
//important - clear the gab if there is nothing
//behind the object to drag against
grabbed = null;
}
}
}
}
}
function Drag() {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)){
grabbed.position = hit.point + grabOffset;
}
}
"and when he rotates the object should keep facing the character/camera while keeping it's original rotation"
I think I understand what you mean, but your description is erroneous if so. Say I pick up a globe with Africa facing me, and turn myself around 180 degrees. Africa is still facing me, but the globe has turned, just with me. I assume this is what you want, correct?
Sorry for the typo. Yes, this is exactly what I want. And I already tried what you suggested, it didn't work.
I'm not convinced I grasp what you're doing (you're dragging only "against" the surface immediately behind the dragged object?). Are you trying to tack your object to the environment?
Could you have another go at explaining your scenario? Screenshots, diagrams, or videos can be helpful.
When I pick an object up from any angle, it has to rotate with me when I turn the camera. That's it.
Here's a screenshot:
So when it does interact, should rotation be allowed? In other words, if you bump this dragged object against another body, the physically accurate thing to do would be to cease enforcing the tracking behavior you're implementing and allow the simulated rotation. Either reacquire the new tracking solution when the collision ceases, or "snap back to" the original tracking. (I've seen both behaviors in games before).
Never$$anonymous$$d that enforcing the position of a non-kinematic rigidbody can have disasterous consequences on the purity of the physics simulation. You could easily create a scenario where dragging an object causes others to skyrocket into the distance. This is not as trivial as it might appear.
In most FPS situations where you see this, I'd wager a grabbed item is parented to the player to achieve the looking-at-Africa effect you're seeking. I get why you can't do this, because you're trying to simultaneously place the grabbed object away from the player at whatever location you're picking with the cursor or crosshair. Bit tricky.
In any event, you're wanting to maintain the specific tracking you had when the drag operation started, so you need to deduce and maintain the original orientation between the object and player... Hmm.
If it were me, I'd try this:
Create an empty helper object. This object will LookAt the camera, and reside at the end of your raycast (where you want the dragged object's position). When your drag operation begins, place the helper object at the point where you hit the dragged object and parent the dragged object to the helper. (Parenting and rigidbodies normally do NOT play nice, but you're already futzing with the physis sim in a dangerous way, so anything goes). Un-parent when the operation ends.
Give this a try; if nothing else, maybe it'll shed some light on what you can and can't do in this situation and give you some fresh ideas.
Your answer
Follow this Question
Related Questions
Bullet mouse control 2 Answers
How can I manipulate an object with the mouse? 1 Answer
OnMouseUp() Click Display Effect. 1 Answer
Dragging movement Speed 1 Answer