• 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
0
Question by AnJoBo · Aug 30, 2018 at 01:55 PM · uinavmeshagentplayer movementnavigationinteraction

How do I close in the distance between the player and object to pick up?

I mostly followed along with the tutorial here to get a chunk of my mobility for my PlayerController script.

The problem I'm running into is that when the player clicks the "Pick Up" button I have that pops up (being positioned when player clicks on an object with the tag of "Pickup") it just picks up (sets active to false) without closing in the distance.

I tried making it similar to the shootDistance variable made in the tutorial linked above that makes the player shoot when the enemy is close enough, but it isn't working...

Here's my code:

 using UnityEngine;
 using UnityEngine.AI;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class PlayerController : MonoBehaviour
 {
     public GameObject matUI;
     public GameObject inventoryManager;
     public GameObject selectionOptions;
     public GameObject dialoguePanel;
     public Text dialogueText;
 
     public bool isMoving;
     public float touchableDistance = 2f;
 
     private NavMeshAgent navMeshAgent;
     private Vector3 destination;
 
     private bool hoverActive;
     private string hoverName;
 
     private float hoverPointX;
     private float hoverPointY;
 
     private GameObject selection;
 
     private bool inRange;
 
     void Awake()
     {
         navMeshAgent = GetComponentInChildren<NavMeshAgent>();
 
         isMoving = false;
         inRange = false;
 
         selectionOptions.SetActive(false);
         dialoguePanel.SetActive(false);
     }
 
     void Update()
     {
         Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit mouseHit;
 
         if (Physics.Raycast(mouseRay, out mouseHit, 100))
         {
             // Checks for click
             if (Input.GetButtonDown("Fire1"))
             {
                 
                 destination = mouseHit.point;
 
                 // If player clicked or taps a UI element **** Doesn't work right on mobile ****
                 if (EventSystem.current.IsPointerOverGameObject() || 
                     EventSystem.current.currentSelectedGameObject != null)
                 {
                     return;
                 }
                 // If player clicks or taps a pickup
                 else if (mouseHit.collider.CompareTag("Pickup"))
                 {
                     selection = mouseHit.collider.gameObject;
                     selectionOptions.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
                     selectionOptions.SetActive(true);
                 }
                 // Just move to selected area
                 else
                 {
                     selectionOptions.SetActive(false);
                     dialoguePanel.SetActive(false);
                     MoveTo(destination);
                 }
             }
         }
         
     }
 
     private void MoveTo(Vector3 destination)
     {
         isMoving = true;
         navMeshAgent.isStopped = false;
         navMeshAgent.destination = destination;
     }
     
     public void Pickup()
     {
         MoveTo(destination);
         if (navMeshAgent.remainingDistance >= touchableDistance)
         {
             navMeshAgent.isStopped = false;
             isMoving = true;
         }
         if (navMeshAgent.remainingDistance <= touchableDistance)
         {
             navMeshAgent.isStopped = true;
             isMoving = false;
             selection.SetActive(false);
         }
         
         selectionOptions.SetActive(false);
     }
 
     public void Inspect()
     {
         dialogueText.text = "Looks useful!";
         dialoguePanel.SetActive(true);
     }
 
 }
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Interactable object as navmesh obstacle.,How can I make an object interactable while it's a navmesh object? 0 Answers

Best way to move a player (touch based) using the event system in Unity 5? 0 Answers

Move through selected buttons one by one 0 Answers

2 canvases working weird with buttons 0 Answers

Strange UI Automatic Navigation behaviour, potentially bug. Can anyone help? 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