# Press f to interact with NPCs or objects

How do I change the method used for initiating the interaction. Instead of clicking object, check if within a certain radius of an object when you hit a key on your keyboard.

If within 1 unit, the player gets a message that they can press f to interact. When f is pressd, get the object they're close to and handle the interaction the same way.

PS. I'm using box collider.

Here are the script that I found. I tried this but once I start the game and press F key, its working. But I want the player to press the F key once the character is inside the collider of an object.

WorldInteraction

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

public class WorldInteraction : MonoBehaviour {
NavMeshAgent playerAgent;

void Start() {
playerAgent = GetComponent<NavMeshAgent>();
}

void Update() {
if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) {
GetInteraction();
}
}

void GetInteraction() {
Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit interactionInfo;
if(Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity)) {
GameObject interactedObject = interactionInfo.collider.gameObject;
if (interactedObject.tag == "Interactable Object") {
interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
} else {
playerAgent.destination = interactionInfo.point;
}
}
}


}

Interactable

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

public class Interactable : MonoBehaviour {
public NavMeshAgent playerAgent;

public virtual void MoveToInteraction(NavMeshAgent playerAgent) {
this.playerAgent = playerAgent;
playerAgent.destination = this.transform.position;

Interact();
}

public virtual void Interact() {
Debug.Log ("Interacting with base class.");
}


}

NPC

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

public class NPC : Interactable {

public override void Interact() {
Debug.Log ("Interacting with NPC.");
}


}

• You said you "found" this script. Do you understand how the script works? I am asking because sometimes we have new users who expect to be able to use Unity without any programming knowledge whatsoever. When we try to help such people assuming they are programmers, communication usually fails because they don't understand what we are talking about. Dec 8 '18 at 11:15
• I understand but some of those scripts are not. What should I do? Help please :( Thanks in advance. Dec 8 '18 at 12:44
• That script is still reliant on you clicking on an object, it raycasts from the mouse, waits for left click, etc. etc. so will not do what you're asking for. Jan 9 '19 at 12:19

The script uses a raycast hit to detect whether the interactible object is visible or not.

If you want to limit the interaction range, you can check interactionInfo.distance after Physics.Raycast. This allows you to verify whether the object is close enough to interact with or not. Or you can limit the range of the raycast in the first place by replacing Mathf.Infinity with your desired maximum interaction range.

• Btw, I used CharacterController for movement of my character, i used WASD to move. I have updated my script. I removed the NavMeshAgent and also the parameter in MoveToInteraction function. Now, how can I interact with NPC by pressing a key? Dec 8 '18 at 14:03
• @J.Wujeck Changing from indirect control via NavMeshAgent to direct control via key input shouldn't change anything about initiating the interaction. The Interact() method should still be called. Under the condition, of course that the raycast finds an interactable object. Dec 10 '18 at 9:53
• I tried to change the Mathf.Infinity into float value (4.0f), but there's a little bit problem. For example my character is behind the interactable object. So if the distance of the player is greater than 4.0f from the interactable object, why the two object is not interacting? Dec 10 '18 at 10:04
• @J.Wujeck Maybe I thought that's what you wanted: Allow interaction with the object the player points at, but only when within a certain radius. Maybe I misunderstood you? Dec 10 '18 at 10:17
• Yeah that's what I wanted. Sorry for my previous comment there's a typo. **So if the distance of the player is less than 4.0f from the interactable object, why the two object is not interacting? Sometimes the two object are not interacting even if the set distance is 4.0f. Dec 10 '18 at 10:54

You could use a boxcollider set as trigger, then do something like the below in your trigger enter and exit functions. No raytracing necessary, i.e. it works when you're not looking directly at the interacted object etc.

private GameObject interactedObject;

void OnTriggerEnter(Collider c){
interactedObject = c.parent;

}
void OnTriggerExit(){
interactedObject = null;

}


then you can use if(interactedObject!=null){....} wherever you want to do something different if you're interacting.

e.g.

void Update() {
if (Input.GetKey(KeyCode.F) && interactedObject != null) {
GetInteraction();
}
}

• Error. UnityEngine.Collider does not contain a definition for parent. What should I do? Jan 11 '19 at 14:06
• What If I have 3 gameobject tags. For example: Interactable Object, Pickupable Object and Portal tag. On those tags, I will use different keys to get interact with the object. The keys are: F, LeftControl and Enter. F key is for Interactable Object, LeftControl for Pickupable Object and Enter for Portal Object. How would I do that? Jan 11 '19 at 14:13