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

Best method for Interacting with objects 3rd Person?

Discussion in 'Scripting' started by Karasu416, Apr 7, 2020.

  1. Karasu416

    Karasu416

    Joined:
    Nov 17, 2013
    Posts:
    6
    I'm relatively new to the coding side of Unity and I'm currently trying to figure out what the best way to go about setting up an Interaction system in my project and was wondering if someone could help point me in the right direction to learn.

    For context: I am currently making a 3rd person RPG with action elements and have a 3rd person camera setup with Cinemachine. I currently started implementing my inventory/item system using ScriptableObjects, however at the moment I simply pick them up by walking over them, removing them from the world and adding them into the inventory.

    I'm trying to switch to more of a 'press button to pickup/open/use' system instead of picking up automatically.

    Upon looking for tutorials and videos on this I've seen this done with ray casts from a first person camera(not sure how viable that is with a third person camera) hit the interact-able object and then having to press the action key if that is happening, I've seen another person use an empty game object tied to the front of the player character with a box collider and interaction script on it to trigger the 'press button to do x' dialogue when they come in contact.

    This has basically been the extent of what I've found. As I said I'm pretty new to the coding side of things so if anyone has any recommendations I appreciate it, or have any solid tutorial links on setting something like this up.

    Thank you for your time
     
    Freznosis likes this.
  2. Freznosis

    Freznosis

    Joined:
    Jul 16, 2014
    Posts:
    294
    Please help this innocent fellow.
     
    ackast3 and Karasu416 like this.
  3. SigmundIII

    SigmundIII

    Joined:
    Mar 9, 2017
    Posts:
    2
    You can do exactly as you said.
    • First you set in your player object an empty GameObject that represents from where the player looks (i've simply made it's rotation equal to the third person camera rotation).
    • Then you Raycast from that point in it's forward direction with a max distance equal to the interactable distance. You can also add a LayerMask to prevent non-related objects from blocking your raycast.
    • Now you make the check, if the object can be interacted with, you notice the player with the text and you check for the keypress
    Here's an example with some code:
    Code (CSharp):
    1. canInteract = false;
    2. Ray ray = new Ray(lookPoint.position, lookPoint.forward);
    3.  
    4. if(Physics.Raycast(ray, out RaycastHit hit, interactDistance, interactionMask)) {
    5.         canInteract = true;
    6.  
    7.         if(Input.GetKeyDown(KeyCode.X) {
    8.             //Interact with the object
    9.             //In here you can also insert a check to the tag if needed
    10.         }
    11. }
    canInteract is a global variable that you can use to see if the player as the possibility to interact with something
    lookPoint is the reference to the Transform of the point from where you raycast of wich i spoken before

    You can make a function out of this, put it in the Player script and call it in the update method.

    In case of small objects it can be difficult to interact with them, so i would probably give them a trigger collider bigger than them, so that you can hit them with more ease.

    I hope this is useful, have a good time developing!
     
    Last edited: Apr 28, 2020
  4. vuuk

    vuuk

    Joined:
    Jan 26, 2019
    Posts:
    2
    What about , overlapBox, overlapCapsule in front of player , than checking distance for closest object, than returning that obj etc.?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    "They're GREAT! I eat three bowls of them every morning for breakfast."

    Please don't hijack old topics to ask weird obtuse questions.

    If you have a problem, here is how to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
  6. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    98
    I think he was suggesting using Physics.OverlapBox to solve OP's problem. Which seems like a valid suggestion. It's not a weird obtuse question -- it is a viable alternative approach to the thread's question.

    This thread shows up in Google when looking for third person controllers. It was asked this year. It doesn't look like a hijack of an old thread.

    Seems like you're getting a little off topic compared to a proposal to use colliders to detect and interact with objects in a with a third person controller.

    To respond to the suggestion:
    - I'm using Physics.OverlapSphere to detect creatures in a moment in time. For example, casting a spell at a distance. So I don't have a collider moving through space like a missile type projectile, but rather I've got a magic explosion spell in the distance, so I use overlap sphere to determine what monsters should be affected.

    - In this case, simple colliders and triggers may work better. Stick a trigger collider in front of the character and when an interactable object enters the collider, highlight it and suggest it as a viable option to pickup after pressing the key.

    - If the goal isn't to highlight the object as you get close, and you only need to respond to a keypress, then a raycast or a Physics.OverlapCapsule to respond to that immediate keypress are probably both good options to try.
     
    Chris128 and Judgeking like this.