- Home /
How can you make it that the FPS can pickup a object and move it around like in most source games
I am having trouble making a object so you can pickup a object say pressing E or clicking on it with the mouse. I have tried the DragRigidBody script and I have tried the Drag object Script and looked heaps of tutorials but nothing I do seems to work. Can anyone give me a step by step on how to do this.
Answer by JuanseCoello · Mar 13, 2014 at 10:44 PM
I have read some tutorials of how to add a pickup to the characters hand, but for that you must have a character so that the pickup would be attached to the hand. You need to create a new script with this code.
using System; using System.Collections;
public class Pickup : MonoBehaviour { public Vector3 Offset = new Vector3(0f, 0f,0f); // Move the pickup slightly to match the hand public Vector3 ScalingFactorWhenHeld = new Vector3(0.4f, 0.4f, 0.4f); // Allows to scale the object smaller so that the player can hold it in its hand.
private Vector3 InitialPosition; // Scales the pickup position when player dies.
private Quaternion InitialRotation;
private bool inRange = false;
bool isHeld = false; // Tells if the object is being held.
private Animation ItemAnim; // It is useful if the object has an animation
private SphereCollider sphereCollider;
void Awake()
{
itemAnim = transform.Findchaild("PICKUPNAME").animation;
sphereCollider = GetComponent("PICKUPNAME") as SphereCollider;
initialPosition = transform.position; // Records initial position of the pickup
initialRotation = transform.rotation;
}
void OntriggerEnter() // It is used when the pickup has volume
{
inRange = true;
}
void OntriggerExit()
{
inRange = false;
}
void LateUpdate() // You need another script called TP_Controller to make the statement isholding
{
if (inRange && !TP_Controller.Instance.isHolding && Input.GetKeyDown(KeyCode.E))
{
itemAnim.Stop;
TP_Controller.Instance.IsHolding = true;
TP_Controller.Instance.ObjectHeld = this;
isHeld = true;
TP_Contorller.Instance.Use();
}
if (isHeld) // if pickup is on the hand set the sphere collider to 0, TargetHand is the bone of the hand
{
sphereCollider.radius = 0;
transform.localScale = ScalingFactorWhenHeld;
transform.position = TP_Controller.Instance.TargetHand.position + Offset;
transform.rotation = Quaternion.Euler(TP_Controller.Instance.TragetHand.rotation.eualerAngles.x,
TP_Controller.Instance.TargetHand.rotation.eulerAngels.y + 90,
TP_Controller.Instance.TragetHand.rotation.eualerAngles.z)
}
}
public void Reset()
{
isHeld = false;
transform.position = initialPosition;
transform.rotation = initialRotation;
transform.localScale = new Vector3(1, 1, 1);
sphereCollider.radius = 1; // Is used to reset the pickup to 1 agan, because when it is hels the collider is set to 0 to avoid collisions.
itemAnim.Play();
}
}
I found this script in a series of tutorials calles Unity 3rd person setup from 3d buzz it explains everything you need to know to create a character, and you can tweak it so you can play wih it and change it to a FPS, I highly recommend it, it was very useful to me and I dont speak english. But for the script I gave you you need to first create Another script called TP_Controller wich holds a character state called isHeld, that means that the character holds something in its hand. And therefore you go, you can ask me any question.
Problems: 1. You just gave me a script you didn't tell me how to use it 2. The script keeps co$$anonymous$$g up with errors no matter what I do 3. I did make it clear I wanted a step by step on how to do this
Please if you can fix your mistakes or if you could give me the link to the video you found this at
Your answer
Follow this Question
Related Questions
Rotating an Object while Moving 0 Answers
Endless runner, road segments spawn speed 0 Answers
Physics2D and curved movement 1 Answer
How to drag object to snap? or move it only in 0.5f 1 Answer