Product Chat / E to interact to a model

Author
Message
Matteo99x
User Offline
Joined: 1st Jul 2024
Location:
Posted: 1st Jul 2024 15:54
Hi i'm new on GameGuru, i want to know how i can make a text "PRESS E TO INTERACT or something like that" on a Model and after the player press it, the model disappear, how i can do something like that? Can you help me please?
PM
Matteo99x
User Offline
Joined: 1st Jul 2024
Location:
Posted: 2nd Jul 2024 13:17
I make the text, now i have to understand how to make the model disappear when press "E" and how to make another model spawn in the same place... i'm starting with that

if GetScancode() == 18
Spawn Soldier
PM
telephonehymn
User Offline
Joined: 17th Jul 2024
Location:
Posted: 17th Jul 2024 05:40
I have the code for your reference
using UnityEngine;
using UnityEngine.UI;

public class InteractableObject : MonoBehaviour
{
public Text interactText; // Assign the UI Text element in the Inspector

void Start()
{
interactText.gameObject.SetActive(false); // Hide the text initially
}

void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 5f))
{
if (hit.transform == transform)
{
interactText.gameObject.SetActive(false); // Hide text after interaction
gameObject.SetActive(false); // Hide the model
}
}
}
}

void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
interactText.gameObject.SetActive(true); // Show the text when the player is near
}
}

void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
interactText.gameObject.SetActive(false); // Hide the text when the player leaves
}
}
}
boxing random
PM

Login to post a reply

Server time is: 2024-07-20 17:47:29
Your offset time is: 2024-07-20 17:47:29