Hullo, Been working on a new prototype, and I've been trying to add in a solid First Person object grabber like Half Life 2 (and probably other games) uses. For anyone who hasn't played Half Life 2, it's pretty simple: you can pick up objects in the world, which then are locked in front of your view. You can carry them around, but they're still part of the world in that they will hit the surrounding walls and such. If the object gets too far away from you, it's automatically dropped. Most of this is pretty darn easy, but I can't find a really good way of moving the object properly while having the world influence it (so it needs to be inside the physics system). SpringJoints are really...springy and kinda eratic. If I just use MoveRigidbody to move it directly in front of the camera every frame, it causes eighty billion problems. If I push it up against a wall it will fall through or push the character away (using a rigidbody FPS Walker). If it hits against any other object, it smacks them with a huge amount of force. Can't just use transform.position of course...running out of ideas. I don't want it to be floaty...I want it to basically lock in front of the player's vision, and not get pushed through walls. My entire idea is based off this, so I hope someone can come up with an idea...thanks for any help!
Bump for help...I guess I might just have to code everything manually if there isn't a built-in physics solution.
Couldn't you just parent a gameobject to your fps player object, position it slightly in front of the player model, and use that as a "pickup anchor point", then parent whatever rigidbody is "picked up" to the anchor? Or you could even just parent it directly to the fps player object, assuming you only allow a "pick up" action to happen if the object is in the center of the screen and the player is within a certain distance. But you'd probably want to set IgnoreCollision between the player and the object once its picked up, as it might cause problems if the player is super close when he picks it up.
You'd have to code this yourself. The behaviour of picking up objects in HL2 isn't going to work with physics joints because it's not realistic in the slightest and physics joints, for the most part, model real world behaviour. There's probably a few special cases you'll need to script in to make it feel as "simple and effortless" as HL2.
Hello there! I coded something similar for a prototype on telekinesis, the trick is to use forces to move the object to your wanted position. First of all you're going to need a target-point, if it's in the middle of the camera and you're using a first person set up then this should give you the point: Code (csharp): Vector3 targetPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0)); Then offset the point in your camera's forward direction a bit. This code will give you a position in worldspace which is located in front of your camera. Now that you have a point in worldspace applying the forces is fairly simple. In the next piece of code I presume the heldObject is the object you're holding. Code (csharp): public float mCorrectionForce = 50.0f; public float mStabilizationFactor = 0.5f; public float mPointDistance = 3.0f; void FixedUpdate() { Vector3 targetPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0)); targetPoint += Camera.main.transform.forward * mPointDistance; Vector3 force = targetPoint - heldObject.transform.position; // Applying the force directly would result in a very wobbly experience // Hence we add this check to stabilize the object depending on it's distance from the targetPoint if(force.magnitude < mStabilizationFactor) force = force.normalized * Mathf.Sqrt(force.magnitude); heldObject.rigidbody.AddForce(force * mCorrectionForce); } That will cause your object to move towards the specified point and stabilize once it reaches it. Since the physics engine is the one doing the work the object will not move through walls and will collide properly. The mCorrectionForce variable will depend a bit on the object's weight, you'll have to experiment to find a good value.
Is there a reason parenting wouldn't work? I've never tried it and don't have Unity on hand, just curious.
Hehe, I blame my education! You see at my university the coding-standard most teachers used was to put a lower case m when declaring member-variables. I quite like it, I can quickly see if a variable I'm changing will stay changed for the next time I need it or if it's only a local variable and will dissapear the moment I leave it. Could've been worse. Some of my teachers followed a standard where you indicate the type of the variable in the name. By that standard a float would be declared mfCoolValue. That quickly got tedious.
glRotated() For a while, once I started programming OpenGL, I actually thought they might have misnamed this function. I didn't know why it was "rotated" in the past tense. Then I saw glRotatef() and everything clicked, lol
Okay, so I got around to trying everyone's various solutions... Myx, yours works really well, but it's just far too floaty for my purposes--the player needs to be able to move everything fairly precisely. Since I can't just parent the rigidbody directly, I'm trying a trick right now. Basically when the player grabs an object, it is a deactivated and a dummy is put in it's the place. The dummy has the same mesh filter/collider, but no rigidbody, and is parented to the player. This makes it part of a compound collider with the player's rigidbody...but this causes alot of problems too. I might just see if I can turn the player solely with forces (right now I'm just using the MouseLook, which modifies the transform directly, then set the grabbed object's velocity/angular velocity to the player's. Worth a try!
Lemme change it up a bit. Instead of using force, which when I tested it made the object fly in circles around me. Lets use Lerp. Just Lerp it to us from it's current position. I also tagged in the mouse into it. get it centered in the screen and click, it will draw the object to you. click again, it will throw it. For a better result, when you pick up an object I turned gravity off for that object. then back on when you throw it. Code (csharp): var pointDistance = 3.0; var pullSpeed = 8.0; var throwForce = 40.0; private var mGrabbed : boolean = false; private var heldObject : Transform; function Update() { if(mGrabbed){ var targetPoint = Camera.main.transform.TransformPoint(Vector3.forward * pointDistance); heldObject.position=Vector3.Lerp(heldObject.position, targetPoint, Time.deltaTime * pullSpeed); } if(Input.GetButtonDown("Fire2")){ if(!mGrabbed){ var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0)); var hit : RaycastHit; if (Physics.Raycast (ray, hit)){ print(hit.transform.name); if(hit.rigidbody){ heldObject = hit.transform; heldObject.rigidbody.useGravity=false; mGrabbed = true; } } }else{ heldObject.rigidbody.velocity=Camera.main.transform.forward * throwForce; mGrabbed = false; heldObject.rigidbody.useGravity=true; } } }
@BigMisterB: The problem with that solution is that it will ignore collision since you just set the position directly. Turning off gravity does indeed help and after a closer look at my Telekinesis that's what I do aswell. Something else I did miss from the initial script is the focusing-function which will probably be relevant in this case. It stops objects from orbiting and simply centers them towards the point. Add this piece of code to my earlier script right before the force is added. Code (csharp): heldObject.rigidbody.velocity = force.normalized * heldObject.rigidbody.velocity.magnitude; Since you're simulating picking an object up you might also want to have the script disable rotation for the rigidbody. Play around a bit with the correction force aswell, higher values should reduce "floatiness". On second thought, moving the dampening could also be a good idea. Yes. Lets do that. Hmm, might aswell adjust the whole script while I'm at it. Code (csharp): public float mCorrectionForce = 50.0f; public float mPointDistance = 3.0f; void FixedUpdate() { if(heldObject.contraints != RigidbodyConstraints.FreezeRotation) heldObject.contraints = RigidbodyConstraints.FreezeRotation; if(heldObject.useGravity) heldObject.useGravity = false; Vector3 targetPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0)); targetPoint += Camera.main.transform.forward * mPointDistance; Vector3 force = targetPoint - heldObject.transform.position; heldObject.rigidbody.velocity = force.normalized * heldObject.rigidbody.velocity.magnitude; heldObject.rigidbody.AddForce(force * mCorrectionForce); heldObject.rigidbody.velocity *= Mathf.Min(1.0f, force.magnitude / 2); } There we go. I wouldn't normally place the checks for gravity and rotation in the update, but rather in the function which actually sets heldObject. Don't forget to turn them back on when you release the object!
Alright, after cranking the correction force up a ton, I was able to get that smooth movement needed for my game, this is really great! I also will probably modify it to temporarily reduce the mass of the object I'm grabbing (to normalize the value so ALL objects move the same speed) and so you can't send other objects flying with the one you're holding. Thanks very much for all the help guys!
Hey I'm working on a similar project My setup is a first person controller with an invisible game object (child of the camera) about 3 meters ahead that acts as a "beacon" that the "grabbed" object will follow. When the right mouse button is pressed, an object appears, when the left is pressed, the object drops. Here is the code that is attached to the object that's being placed: Code (csharp): #pragma strict function Start () { } var correctionforce : float = 50.0f; var grabbed : boolean = true; function Update () { var target = GameObject.Find("NAME OF BEACON GAME OBJECT HERE"); var force : Vector3 = target.transform.position - transform.position; if(Input.GetButton("Fire1")) { grabbed = false; print("released"); } if(grabbed == true) { print("grabbed"); if(rigidbody.freezeRotation == false) { rigidbody.freezeRotation = true; } if(rigidbody.useGravity == true) { rigidbody.useGravity = false; print("it works"); } rigidbody.velocity = force.normalized * rigidbody.velocity.magnitude; rigidbody.AddForce(force * correctionforce); rigidbody.velocity *= Mathf.Min(1.0f, force.magnitude / 2); } if(grabbed == false) { rigidbody.useGravity = true; } } I basically re-wrote Myx's script in JavaScript with a few additions. Here's the script attached to the beacon: Code (csharp): var cube : Rigidbody; function Update () { if(Input.GetButtonDown("Fire2")) { print("new"); Instantiate(cube,transform.position,transform.rotation); } } Hope this helps!
I still can't figure out how to not depend on raycasting upon camera view, but rather on empty gameobject line of sight. for example: You put the empty object on the tip of your gun