0
\$\begingroup\$

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.");
}

}

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Philipp
    Commented Dec 8, 2018 at 11:15
  • \$\begingroup\$ I understand but some of those scripts are not. What should I do? Help please :( Thanks in advance. \$\endgroup\$
    – J.Wujeck
    Commented Dec 8, 2018 at 12:44
  • \$\begingroup\$ 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. \$\endgroup\$
    – JeffUK
    Commented Jan 9, 2019 at 12:19

2 Answers 2

1
\$\begingroup\$

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.gameObject;

}
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();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ 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? \$\endgroup\$
    – J.Wujeck
    Commented Jan 11, 2019 at 14:13
0
\$\begingroup\$

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.

\$\endgroup\$
9
  • \$\begingroup\$ 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? \$\endgroup\$
    – J.Wujeck
    Commented Dec 8, 2018 at 14:03
  • \$\begingroup\$ @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. \$\endgroup\$
    – Philipp
    Commented Dec 10, 2018 at 9:53
  • \$\begingroup\$ 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? \$\endgroup\$
    – J.Wujeck
    Commented Dec 10, 2018 at 10:04
  • \$\begingroup\$ @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? \$\endgroup\$
    – Philipp
    Commented Dec 10, 2018 at 10:17
  • \$\begingroup\$ 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. \$\endgroup\$
    – J.Wujeck
    Commented Dec 10, 2018 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .