Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by kjw1998 · Nov 15, 2011 at 07:03 PM · objectsshootergrabpersonfirst

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??

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kjw1998 · Nov 16, 2011 at 07:26 AM 0
Share

hi so i just need the code inside the //============================== and then where do i put it? like in the camera or what?

avatar image Steeley · Apr 28, 2013 at 07:50 AM 0
Share

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

avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mox.du · Nov 16, 2011 at 02:44 PM 0
Share

As you can see, that part is located in player movement script, and of course you should also add it to the player script.

avatar image nilesh026 · Sep 18, 2012 at 06:25 AM 0
Share

please help... i want to move pickup object in front of camera how it possible can u tell me

avatar image
0

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;
                     }
                     
              }
          }
      }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CliffracerX · Nov 23, 2014 at 08:57 PM 0
Share

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.

avatar image
0

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


objectgrabscripts.zip (11.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges