Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Moving Object by Pressing Buttons Using NavMesh

Discussion in 'Scripting' started by TheOneRaven, Nov 21, 2019.

  1. TheOneRaven

    TheOneRaven

    Joined:
    Oct 28, 2016
    Posts:
    15
    Hello!
    Currently I am working on a project and I want to move the car. Inside the car there are buttons as can be seen in the image. When a user presses a button he needs to go to the location with the same letter.

    I have created a NavMesh as seen in the other image over which the car should move.

    My question is, how do I create a button that sends the car to the right location on the NavMesh? Currently the buttons are just 3D objects/cubes.

    Not sure if this is all the information you need :) I have barely any experience with coding so I could really use some help!

    Thank you!

    CarInside.png NavMesh.png
     
  2. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    563
    I am a noob, but what I would do is a ray cast, and use this hit.collision information to move from there
     
  3. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    563
    Something Like this:

    Something like this;
    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2.     RaycastHit hit;
    3.  
    4.     if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
    5.        {
    6.           UnitScriptName raycastHitUnitReference = hit.collider.gameObject.GetComponent<UnitScriptName>();        
    7.        }
    Note; add A script on each one of those boxes called UnitScriptName, also add a trigure collider on them.

    Just a noob here, but thats where I would start
     
  4. TheOneRaven

    TheOneRaven

    Joined:
    Oct 28, 2016
    Posts:
    15
    Hi! Thanks for your reply, so basically I add "hitboxes" with collisions on the point where I want the car to go, and then check if along the way the car hits the box that has the right letter to make it stop?

    In the code you are using Input.mousePosition, this relates to where the person clicks right? Would this work for the buttons in the car?

    Thank again!
     
  5. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    563
    Actually I was just talking about how to press the buttons in the car. lol
    I dont really know how your car moves, I was assuming you had some sort of waypoints and depending on which button you press, you assigned a different way point for your navmesh.

    mNavMeshAgent.destination = UnitScriptName.wayPointsTransform.position;
     
    Last edited: Nov 21, 2019
  6. TheOneRaven

    TheOneRaven

    Joined:
    Oct 28, 2016
    Posts:
    15
    oh haha, yeah you don't get them more incompetent than me around here. That makes more sense! Alright thank you :) I will dive into it! Have a nice day :)
     
  7. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    563
    I have only started 2 months ago myself, this is just a suggestion with my limited experience.

    Also, Never get intimidated. And always ask questions, it helps others also not just you. So dont feel ashamed to ask questions no matter how "incompetent" you may feel. Trust me I feel it too.. but I mean I made like 200 questions in the last 2 months in this forum lol
     
  8. TheOneRaven

    TheOneRaven

    Joined:
    Oct 28, 2016
    Posts:
    15
    Thank you for the kind words, I will make sure to push through and ask questions ;) I really appreciate it :)
     
    Mortalanimal likes this.