- Home /
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??
Answer by Mox.du · Nov 15, 2011 at 08:57 PM
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.
hi so i just need the code inside the //============================== and then where do i put it? like in the camera or what?
Thanks, this really helps, but one problem which may be easily fixed, is that while it stays 2 away from the camera, it does so along one axis, and never rotates, is there anyway to make it stay 2 away with relation to the angle of the camera? Also, how do I choose which objects I want the character to pick up, at the moment it can pick up the whole landscape... which isn't very good
Answer by kjw1998 · Nov 16, 2011 at 07:28 AM
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?
As you can see, that part is located in player movement script, and of course you should also add it to the player script.
please help... i want to move pickup object in front of camera how it possible can u tell me
Answer by eliasmarca · Jul 28, 2013 at 03:50 AM
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;
}
}
}
}
}
The OP is talking about picking up and moving objects around, like in Amnesia, not picking up an item and putting it in your inventory.
Answer by exvalid · Oct 20, 2017 at 07:10 PM
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 kappeslink text Exvalid@gmail.com
Your answer
![](https://koobas.hobune.stream/wayback/20220613043731im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can i make a first person shooter MMO with unity? 2 Answers
How To Make Basic AI? 3 Answers
Error in code I cannot fix 1 Answer
How to make a world model apear seperate from a player model 0 Answers
shooter adjust where fired 0 Answers