Hello all,
I have been working in a script to pickup objects and manipulating them in fps (pretty much like in Half life). I found some threats and made the following script. It works fine but after spending a lot of time trying to update the rotation of the picked objects according to the player’s direction, still I haven’t achieved anything.
Anybody please can give me a clue on how to do this?
var dist : float;
var initPosition : Vector3;
var initTargetPosition;
var ob : Transform;
var pickObject : boolean = false;
function FixedUpdate () {
if(Input.GetButtonDown("Fire1")){
//boolean to move the picked object
pickObject = true;
//cast a ray from the player camera to the center of the screen
var ray = camera.main.ScreenPointToRay(Vector3(Screen.width/2,Screen.height/2));
var hit : RaycastHit;
if(Physics.Raycast(ray,hit)) {
//point in which the ray make contact with the object
target = hit.point;
//the distance from the camera to the object
dist = hit.distance;
//name and transform of the picked object
//hitObject = hit.transform.gameObject.name;
ob = hit.transform;
//Debug.DrawLine(ray.origin,hit.point);
}
else {
target = (ray.origin + ray.direction * 100);
}
//do the object has a rigidbody? - swuitch off physics behavior
if(ob.rigidbody != null ob.tag == "Pickable")
{
ob.rigidbody.isKinematic = true;
}
initPosition = camera.main.ScreenToWorldPoint(Vector3(Screen.width/2,Screen.height/2,dist));
initTargetPosition = ob.position;
}
if(Input.GetButtonUp("Fire1")) {
pickObject = false;
if(ob.rigidbody != null)
{
ob.rigidbody.isKinematic = false;
}
}
if(pickObject ob.tag == "Pickable") {
var currentPosition : Vector3 = camera.main.ScreenToWorldPoint(Vector3(Screen.width/2,Screen.height/2,dist));
var targetTranslation = currentPosition - initPosition;
ob.position = initTargetPosition + targetTranslation;
}
}
My english is a little confusing sometimes so I made this drawing to help me explain the problem[/img]

Well, the easiest way would be to have the object added into a local variable for what handles your turning, and have it rotate the object by the same amounts as well.
Alternatively, you could parent the object to the player, which would make it automatically move and rotate with you. You’d just have to set its parent to null when it’s released.
try something like the attached code. I haven’t tested this and you will have to make changes to get it to work correctly. Just something off the top of my head.
var dist : float;
var initPosition : Vector3;
var initTargetPosition;
var pickedob : Transform = null;
var pickedParent : Transform;
function FixedUpdate ()
{
if(Input.GetButtonDown("Fire1"))
{
pickUp();
}
else if(Input.GetButtonUp("Fire1"))
{
if(pickedob != null)
putDown();
}
}
function putDown()
{
if(pickedob.rigidbody != null)
{
pickedob.rigidbody.isKinematic = false;
}
// restore the pickedob.parent to the correct parent
pickedob.parent = pickedParent;
// you will probable have to reset the position also
// pickedob.position = Vector3(x,y,z);
pickedob = null;
}
function pickUp()
{
//cast a ray from the player camera to the center of the screen
var ray = camera.main.ScreenPointToRay(Vector3(Screen.width/2,Screen.height/2));
var hit : RaycastHit;
if(Physics.Raycast(ray,hit))
{
//the distance from the camera to the object
dist = hit.distance;
//transform of the picked object
var hitobject = hit.transform;
if((hitobject .tag == "Pickable") (hitobject .rigidbody != null))
{
pickedob = hitobject;
//does the object has a rigidbody? - switch off physics behavior
pickedob.rigidbody.isKinematic = true;
pickedParent = pickedob.parent;
// set the pickedob.parent = player
// for pickedobjs position do something like this: pickedob.position = Vector3(dist,0,0);
// the position vector setup will be dependent on how the player is set up. dist may be along the x,y,z axis
// you will have to play with these values to get it right
}
}
}
Wuau!!, I never thought in parenting the object to the player. Sounds logical and very easy to implement… Thank you very much!!
I agree that parenting the object to a handler GO would be the easiest solution. Rotating and moving the handler would apply as expected to the object.
Many, many thanks for the code as well
The parenting approach works as a charm!
Many thanks again