How do I make my fps character grab objects?

I am making a game and its going to be very simple game where you can open drawers and doors and you can pick up objects. I am new to unity and so I have just been using prefabs but now I’m stuck. Can anyone help me??

Hi,

Make sure that objects that you want to grab are set to "Is trigger" in collider component. Part of the script that you actually need for grabbing and dropping objects is between // ========================== comments. Also make sure that GameObject with "character controller" attached to it is moving over the mesh with "collider" attached to it.

/// This script moves the character controller forward
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to Move or SimpleMove per frame.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;
// ======================================================================
private var grabObj:boolean = false;
private var hitObj:GameObject;
private var hit : RaycastHit;

function Update() {

    if(Physics.Raycast (transform.position,transform.forward, hit, 5)) {
        if(hit.collider.gameObject && Input.GetMouseButtonDown(0) && grabObj == false){
                hitObj = hit.collider.gameObject;
                grabObj = true;
                        }else if(Input.GetMouseButtonDown(0) && grabObj == true){
                            grabObj = false;
                            }

    }

    if(grabObj){
        //Moving object with player, 2 units in front of him cause we want to see it.
    hitObj.transform.position.x = gameObject.transform.position.x;
    hitObj.transform.position.y = gameObject.transform.position.y;
    hitObj.transform.position.z = gameObject.transform.position.z+2;
        }
    // =====================================================================
var controller : CharacterController = GetComponent(CharacterController);

if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}

Regards.

thanks, so i only need the script inside //================================================
and once ive made the script what shall i put it into? the camera? the pickup object?

i found another way. Very simple.

#pragma strict

var TheSystem : Transform;
var Distance : float;
var MaxDistance : float = 10;  

function Update() {



          var hit : RaycastHit;
		if (Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection(Vector3.forward), hit))
		{    
		   if(hit.transform.gameObject.tag == "sword2"){
		
		
			Distance = hit.distance;
			if (Distance < MaxDistance){

 
                if (Input.GetKeyDown(KeyCode.E)) {
                   // show
                renderer.enabled = true;
                Destroy (GameObject.FindWithTag("sword2"));
                     } 
 
               if (Input.GetKeyDown(KeyCode.Backspace)) {
               // hide
               renderer.enabled = false;
                    }
                    
             }
         }
     }
}

Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

Fixed An Inversion Error and a Euler read error while moving. since yesterday it is now working as stated below

ObjectReplyIdAndLock, ObjectGrabIdAndMove

ObjectGrabIdAndLock goes on Main player,… ObjectReplyIdAndMove goes on Moveable Objects remember to add layers in you want to hit in inspector and pick the Headcam Ect

Updated Scripts Since the other week now with full movement And Rotation and Full Inversion Options

This is Complete bar Diagnals and Mouse Rotation as im adding this now, you want to use the option OverideDiagnals and possibly UseDefaultRotation if u hate my defualt.

too add mouse copy the whole auto inversion and paste it underneath and swap the names to the mouse names instead of the default keys its a mission dont attempt it lol. i will do this over the week as still cleaing up the script its pretty large, Please contact at Exvalid@gmail.com To give me job coding.

cheers
Ryan kappes[105509-objectgrabscripts.zip|105509]
Exvalid@gmail.com