Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bdweidner · Oct 12, 2017 at 03:43 PM · objectpickuprange3rd person controllerdrop

How do I allow 3rd Person Character Picking Up Objects within a range?

I am creating a 3rd person puzzle game and I am able to have the character pickup and drop an object that has this script with the 'K' key. The problem is, I can't figure out how to have the object be picked up only when the character gets within a certain range. If I don't do this, any object with this scrip will teleport to the front of the character from any place on the game.

So how do I allow objects that can be picked up only be picked up when the character is close to them?

Here is the script that allows the object to be picked up and dropped using the 'K' key.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Pickable : MonoBehaviour { public GameObject item; public GameObject tempParent; public Transform guide; bool carrying;

 // Use this for initialization
 void Start()
 {
     item.GetComponent<Rigidbody>().useGravity = true;
 }

 // Update is called once per frame
 void Update()
 {
     if (carrying == false)
     {
         if (Input.GetKeyDown(KeyCode.K))
         {
             pickup();
             carrying = true;
         }
     }
     else if (carrying == true)
     {
         if (Input.GetKeyDown(KeyCode.K))
         {
             drop();
             carrying = false;
         }
     }
 }

 void pickup()
 {
     item.GetComponent<Rigidbody>().useGravity = false;
     item.GetComponent<Rigidbody>().isKinematic = true;
     item.transform.position = guide.transform.position;
     item.transform.rotation = guide.transform.rotation;
     item.transform.parent = tempParent.transform;
 }

 void drop()
 {
     item.GetComponent<Rigidbody>().useGravity = true;
     item.GetComponent<Rigidbody>().isKinematic = false;
     item.transform.parent = null;
     item.transform.position = guide.transform.position;
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Oct 12, 2017 at 04:27 PM

 public float range = 5 ;

 // ....

  if (carrying == false)
  {
      if (Input.GetKeyDown(KeyCode.K) && (guide.transform.position - transform.position).sqrMagnitude < range * range) 
      {
          pickup();
          carrying = true;
      }
  }
  else if (carrying == true)
  {
      if (Input.GetKeyDown(KeyCode.K))
      {
          drop();
          carrying = false;
      }
  }
Comment
Add comment · Show 3 · 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 bdweidner · Oct 12, 2017 at 08:14 PM 0
Share

Thank you for the help, I implemented what you put and now it has a range, but it is always fixed at a certain distance no matter how I change the range variable. It looks wrong seeing the object teleport from an unreasonable distance. Any suggestions on how to fix this so that the character has to be right in front of the object to pick it up?

avatar image bdweidner bdweidner · Oct 12, 2017 at 08:16 PM 0
Share

Just changed range * range to range / range and it fixed my problem. Thank you @Hellium for all the help!

avatar image Crown-Michaela · May 28, 2020 at 02:25 PM 0
Share

Hi! sorry i've been looking everywhere for a script that lets my 3d character pick up an item so i copied this one except i can't figure out how to set it up. The item i wish to pick up is an apple, i gave it a rigidbody, collider and added the script. What should i put in as the guide? and does this script use animations? @Hellium @bdweidner

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

123 People are following this question.

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

Related Questions

Passing an object to a function in OnDrop 0 Answers

Hello, Need help with a problem 1 Answer

Pick up, Carry, Throw Object - Mobile Controls 0 Answers

Object Reference Error 1 Answer

Random Object Selection 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