#but interaction would require the

1 messages · Page 1 of 1 (latest)

crimson anvil
#

thread

#

you need to do this in two halves

stoic raft
#

Ok that's easier to track thanks

crimson anvil
#

one: decide if you are currently looking at a valid interactable

#

so, raycast, try to get the component, and then check if you can interact with that component

#

if all of those are true, store that component into newInteractable

#

if any of those fail, store null into it

#

compare this to lastInteractable

#

once you're done, set lastInteractable = newInteractable;

stoic raft
#

Ok checking now

crimson anvil
#

so it should all be done before you even do anything with lastInteractable

stoic raft
#

if i'm following, the TryGet would be the last thing checked in the operation?

crimson anvil
#

no. you can't check if CanInteract is true before you get the IInteractable component.

#
  • see if the raycast hits anything
  • if it does, see if that object has an IInteractable component
  • if it does, see if CanInteract is true
stoic raft
#

Would this be the proper format for the function itself?

        public bool CanInteract(GameObject interactedObject)
        {
            return interactedObject; 
        }
crimson anvil
#

that literally just tells you if interactedObject is a valid object

#

You've written this several times now

#

iInteract.CanInteract(hit.collider.gameObject);

#

This does nothing.

#

You're just asking if you can interact and then doing nothing with that information.

stoic raft
#

ah, is it more like if(iInteract.CanInteract(hit.collider.gameObject)){

crimson anvil
#

sure

#

although I feel like CanInteract should take something else as an argument...

#

hit.collider.gameObject is just the game object that the IInteractable is on

#

that's not very interesting information

#

I'd expect something more like this

#
if (player.CanInteract(iInteract))
#

asking the player if they are able to interact with this thing

stoic raft
#

is that still a bool function?

#

also, is this task more advanced? it doesn't seem beginner friendly

crimson anvil
#

it's got a bool return type, so sure

#

you can always fill in more complex logic later

stoic raft
#

when you say player do you mean the player class contains a CanInteract type function?
rather than callback from the interacted interface?

#

rn its iInteract.CanInteract() which is called by the TryGetComponent

crimson anvil
#

Yes. It seems reasonable to me that the player would know if it can or can't interact with something.

#

I guess you could also ask the interactable if you can interact with it

#

Either way works.

stoic raft
#

hmm good point, which one is less prone to error and more extendable you think?

crimson anvil
#

Maybe the latter.

#

Just so that you don't have tons of logic in the player class

stoic raft
#

ah so interface is probably best, that makes sense

crimson anvil
#

a door might check if you have a key in your inventory

stoic raft
#

what would be the proper function syntax for the CanInteract? is it Iinteractable?

crimson anvil
#

well, this is a method on IInteractable, right?

#

so it shouldn't take itself

stoic raft
#

is it still GameObject then?

crimson anvil
#

i don't know how your game works so I can't say what to actually pass it right now

#

probably a Player or something

#

if you have no idea what to put there, then just get rid of it for now

#

you can add this logic later when you have a better idea of what will and won't let you interact with something

stoic raft
#

Ah, so the player input manager class throws out a raycast which hits an object with the Iinteractable interface, which triggers the CanInteract