- Home /
Other
Collider Question
So im wondering if there's a way to make a collider component on a player and a collider on a stationary object but at the same time the stationary object cannot stop the player but still detects the overlap of the other colider is there a way to do this?
Answer by KjipGamer · May 18, 2018 at 08:53 PM
Yes you can. All you need to do is go to the inspector, and check the "Is Trigger" box.
Then to check for an overlap simply make a method void OnTriggerEnter(Collider other){ }
on the object. You can access your player object by writing the following line in the same script under the OntriggerEnter void. other.gameObject
Alright but im getting confused about that last part.
This is an example script, which is sitting on your object that "Is Trigger". In this example, when you touch the object with your player, it gets teleported back to the spawn point.
Void OnTriggerEnter(Collider other){
Debug.Log("Collision Happened");
other.gameObject.transform.position = spawnPoint.transform.position;
}
The "other.gameObject.transform.position" is your player in this case.
Also do you want me to set is trigger on the stationary object or player?
Oh ok I see but this is a bit difficult to implement because When the player goes through the collider it activates a text box with T$$anonymous$$Pro Console code and stops the player movement code so the player can't move while the text box is happening
Well you could just have a public bool textIsBeingDisplayed = false;
and in the method it would just update to something like this:
Void OnTriggerEnter(Collider other){
Debug.Log("Collision Happened");
textIsBeingDisplayed = true;
}
And then access it from the other script you have running.
Well ok I think I got it here's my code and Its still not detecting the collision
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextboxCollision : $$anonymous$$onoBehaviour {
public GameObject Player;
public GameObject TextBox;
public Playermove P$$anonymous$$;
public Image TextBoxImage;
public GameObject TextBoxText;
public Sprite TextBoxSprite;
// Use this for initialization
void Start () {
Player = GameObject.Find ("Player");
TextBox = GameObject.Find ("TextBox");
TextBoxImage = TextBox.GetComponent<Image> ();
TextBoxText = GameObject.Find ("TextBoxTEXT");
//Prevent Text untill collision
TextBoxText.SetActive (false);
//Set Text Box Image to nothing
TextBoxImage.sprite = null;
}
void OnTriggerEnter(Collider other) {
//activate text and disable player movement and switch the text box image
TextBoxText.SetActive (true);
TextBoxImage.sprite = TextBoxSprite;
P$$anonymous$$.speed = 0;
P$$anonymous$$.StopJ = false;
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Collider2D not working? 2 Answers
Enemy's parts are visible through the walls c# 0 Answers
Interesting collision question 0 Answers