Unity Animator not entering entry state - c#

I am trying to make a very simple animation in unity where an element fades in and out based on if a character enters a box collider of an NPC. When running the game, I have my Animator window open, and the blue horizontal line that normally appears under the entry state isn't even showing up. When looking at the previews of my animations the sprite I'm animating fades in and out so I know that isn't the problem. For some reason my entry state isn't even being reached and I can't figure out why.
I have the animation clips set up for an open and close state, I have a script that takes a public Animator and dragged the gameobject with the animator into the respective slot in the editor.
Here's the file that triggers the animation: (PS: I've inserted Debug.Log statements and know for a fact I am entering the if(playerInRange) condition.
public class DialogueTrigger : MonoBehaviour {
[SerializeField] private GameObject visualCue;
[SerializeField] private TextAsset inkJSON;
public Animator animator;
bool playerInRange;
void Awake() {
playerInRange = false;
visualCue.SetActive(false);
}
void Update() {
if (playerInRange) {
animator.SetBool("IsOpen", true);
if (InputManager.GetInstance().GetInteractPressed()) {
Debug.Log(inkJSON.text);
}
}
else {
animator.SetBool("IsOpen", false);
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player") {
playerInRange = true;
}
}
void OnTriggerExit2D(Collider2D other) {
if (other.gameObject.tag == "Player") {
playerInRange = false;
}
}
}

UPDATE: aaaaand of course the second I post on stack overflow I figure it out. I needed to remove the visualCue.SetActive("False") line as that was disabling the sprite I was trying to animate, had that in there from a tutorial I was following earlier.

Related

Checking player is not in scene(Not working)

I have a game that I want a restart button to appear if you die (The game object destroys itself).
This is what I have. This is the script attached to the button:
public GameObject Player;
public GameObject Button;
bool player;
public void RestartGame(){
SceneManager.LoadScene("SampleScene");
}
void Start(){
player = true;
}
void Update(){
if (!Player){
player = false;
}
if(player == true) {
Button.SetActive(false);
}
if(player == false){
Button.SetActive(true);
}
}
And this is the code attached to the object that gets deleted:
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}
When it starts the Button is disabled but when I die nothing appears. I have tried many things like checking if it is == null and all of that stuff but it still doesn't work. My goal is to make the restart button appear once the object is destroyed.
From what I understand you are checking if player get destroyed or not. However you are destroying trigger.
There must be a tag on player object use that. On your trigger object use this code. Pass player and button on inspector.
public GameObject Player;
Public GameObject Button;
void OnTriggerEnter(Collider other)
{
if (other.gameobject.comparetag("Player"))
{
Destroy (Player);
Button.SetActive(true);
}
}
For this to work both objects player and trigger must have colliders
and at least one of them must have rigidbody , and player tag must be Player

Destroying a game object that's part of a prefab once an action key is pressed inside a trigger

so I'm making a 2d platform. My level is made up of a multiple platforms that are all part of a prefab. I want to make it so when my player presses a key (in this case 'E') inside of a collider2d the platform above the player is destroyed and the box resting on the platform falls down.
I've got the detection working for when 'E' is pressed inside of the trigger but can't figure out how to destroy just the single platform in the prefab.
Any help would be appreciated!
public class SwitchController : MonoBehaviour
{
public Collider2D switchCollider;
public Rigidbody2D player;
void Start()
{
switchCollider = GetComponent<Collider2D>();
}
private void OnTriggerStay2D(Collider2D col)
{
// var player = col.GetComponent<PlayerController>();
var actionBtn = PlayerController.action;
if (player)
{
Debug.Log("Collided");
if (Input.GetKeyDown(KeyCode.E))
{
actionBtn = true;
Debug.Log("Action Pressed");
}
}
}
}
If possible, the simplest solution would be to store the platform via the inspector (of the prefab).
Then you would destroy the game-object when needed, like so:
public class SwitchController : MonoBehaviour {
// ...
public GameObject targetPlatform;
private void OnTriggerStay2D(Collider2D col) {
// ...
Destroy(targetPlatform); // Destroy the platform.
}
}
Or, you can raycast upwards from the player
public class SwitchController : MonoBehaviour {
public Collider2D switchCollider;
public Rigidbody2D player;
[SerializeField, Tooltip("The layer mask of the platforms.")]
public LayerMask platformLayerMask;
void Start() {
switchCollider = GetComponent<Collider2D>();
}
private void OnTriggerStay2D(Collider2D col) {
var actionBtn = PlayerController.action;
if (player) {
if (Input.GetKeyDown(KeyCode.E)) {
actionBtn = true;
// Raycast upwards from the player's location.
// (Raycast will ignore everything but those with the same layermask as 'platformPlayerMask')
RaycastHit2D hit = Physics2D.Raycast(player.transform.position, Vector2.up, Mathf.Infinity, platformLayerMask);
if (hit.collider != null) {
// Destroy what it hits.
Destroy(hit.transform.gameObject);
}
}
}
}
}
Compared to the first solution, this solution is more dynamic.
You just have to set the Layers of the platforms in the inspector.

Why does unity keep my Canvas deactivated with OnTriggerEnter()? Acitvate Text with Collider in Untiy2d

I can't activate my Canvas with my Interactable(Coin) Script and the OnTriggerEnter(). I want to activate the Canvas(show my Text) when my Player collides with my Coin. The Script is in my Coin GameObject. My CoinCollider is on IsTrigger.
public string displayText;
public Text textView;
public GameObject Player;
public Canvas textCanvas;
// Start is called before the first frame update
void Start()
{
textCanvas.enabled = false;
}
// Update is called once per frame
void Update()
{
}
void onTriggerEnter(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
textCanvas.enabled = true;
textView.text = displayText;
}
}
I tried making just the Text appear without a Canvas and watching Tutorials.
The problem is that the method name is "OnTriggerEnter2D" not "onTriggerEnter" for 2D collisions. This sho

OnCollisionExit is not being called

Why is OnCollisionExit not being called? I am using both OnCollisionEnter and OnCollisionExit but unfortunately only OnCollisionEnter is being called.
public bool HandleCollided = false;
public void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "RightHandAnchor")
{
HandleCollided = true;
}
}
public void OnCollisionExit(Collision col)
{
if(col.gameObject.name == "RightHandAnchor")
{
HandleCollided = false;
}
}
It's impossible to tell why your code isn't working based the given snippet - this code depends on the configuration of each of the GameObjects' inspector windows in the editor.
Both the GameObject that this script is attached to and the colliding GameObject must have one Collider component attached to each of them (for example, a BoxCollider component or a SphereCollider component). Both Colliders must have their isTrigger checkboxes disabled. The GameObject that this script is attached to must also have a Rigidbody component attached.
In order to debug this situation, add Debug.Log() statements in your functions. This is generally good practice, and it might be that the function is being called but the conditional statement is not true.
Here are some additional ideas on what might be going wrong:
You may be changing the name of the colliding GameObject somewhere else in your code.
You may be destroying the GameObject.
It might be that neither function was being called and HandleCollided was being changed elsewhere in your code.
It might be that the parameter, col, is not what you expect.
public void OnCollisionEnter(Collision col)
{
Debug.Log("Collision Enter!");
Debug.Log(col.gameObject.name);
}
public void OnCollisionExit(Collision col)
{
Debug.Log("Collision Exit!");
Debug.Log(col.gameObject.name);
}
So you said "There are two objects that collides with each other. One has sphere collider attached to it and another has box collider. One of the objects have rigidbody attached as well." On which one is the code? and yes this matters! only 1 of the objects will keep track of the exit meaning that if it's the non-kinematic it will not work.
Unfortunately using OnCollisionExit did not work so instead I used OnTriggerEnter and OnTriggerExit. I activated "isTrigger" for both objects.
public void OnTriggerEnter(Collider col)
{
Debug.Log("entered");
if (col.gameObject.name == "RightHandAnchor")
{
HandleCollided = true;
}
}
public void OnTriggerExit(Collider other)
{
Debug.Log("exit");
if (other.gameObject.name == "RightHandAnchor")
{
print("No longer in contact with " + other.transform.name);
HandleCollided = false;
}
}
You need a non-kinematic rigidbody attached to your object to get the event for OnCollisionExit
Try to use OnCollisionEnter() and OnTriggerExit() !!
Example:
void OnCollisionEnter(Collision collision)
{
if((collision.gameObject.GetComponent<AttributeManager>().attributes & doorType) != 0)
{
this.GetComponent<BoxCollider>().isTrigger = true;
}
}
private void OnTriggerExit(Collider other)
{
this.GetComponent<BoxCollider>().isTrigger = false;
}

how to make image appear when player is dead in unity

I need to make an image pop up when the player has died or crashed but i do not know how to do it, i'm trying to make a game in unity using c#
but i have made a code that will tell show the user an image before they start (tap to start image) and all i want to do is display another one that tell to user to start again
does the code have to be similar to this or do i have to start from scratch?
public class StartScreenScript : MonoBehaviour {
static bool sawOnce = false;
// Use this for initialization
void Start () {
if(!sawOnce) {
GetComponent<SpriteRenderer>().enabled = true;
Time.timeScale = 0;
}
sawOnce = true;
}
// Update is called once per frame
void Update () {
if(Time.timeScale==0 && (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) ) {
Time.timeScale = 1;
GetComponent<SpriteRenderer>().enabled = false;
}
}
}
this code show the an image telling the user to tap the screen and the image then goes away until the user closes the game then comes back on however i want to display a "you are dead image" every time the player dies can someone please help me
p.s this is for a 2d game
Well one way to do it would be using Unity3D's GUI.DrawTexture that given a texture draws it at a given position. Here is a sample call to the method.
GUI.DrawTexture(new Rect(leftAnchor, topAnchor, textureWidth, textureHeight), textureSource);
This is my approach and it works in most cases:
Create a GameObject that will work as a dead screen. Apply a sprite
or whatever telling the user to click to restart.
Add the previous GameObject to the PlayerController so he can
instantiate it.
When the player is dead call PlayerController.ShowDeadScreen()
When the user clicks inside DeadScreen GameObject it will call your
PlayerController.PlayAgain function and destroy itself. So you must handle everything
the game need to be restarted.
PlayerController example code
public class PlayerController : MonoBehaviour {
public GameObject deadScreen;
void Start() { }
void Update() { }
public void ShowDeadScreen()
{
// show DeadScreen GameObject on the center of the screen
GameObject go = Instantiate(deadScreen, new Vector(0, 0, 0), Quaternation.Identity) as GameObject;
go.playerController = this;
}
public void PlayAgain()
{
// handle game restart
}
}
DeadScreen example code
public class DeadScreen : MonoBehaviour {
public PlayerController playerController;
void Start() { }
void Update() { }
void OnMouseDown()
{
// when user clicks inside this GameObject start the game again
playerController.PlayAgain();
Destroy(this.gameObject);
}
}

Categories