I have animation Clip for swinging sword. At a specific frame, I added Event. I want when player swing sword, in that case only the enemy could die.
So I added the following OnTriggerEnter Code
void OnTriggerEnter(Collider col)
{
hit = true;
if (hit)
{
if (col.GetComponent<Collider>().tag == "enemy")
{
Destroy(col.gameObject);
}
}
}
When I try to add function OnTriggerEnter (in animation Clip) as animation Event, it is asking me to pass Collider parameter, Which i am not able to add.
Here is the screen shot of Add Event
Please help me , how can I add event with collider ( as parameter ) at a specific frame.. Thanks
Collision and Trigger events are called on their own on every frame.
Call another public method from animation event for your task and put necessary Boolean or Enum there to control collision, trigger after swinging the sword.
public void SwingingSword()
{
isSwingingSword = true; // make it false when not swinging the sword.
}
void OnTriggerEnter(Collider col)
{
hit = true; // not sure what it's job here
if (isSwingingSword && hit)
{
if (col.GetComponent<Collider>().tag == "enemy")
{
Destroy(col.gameObject);
}
}
}
Related
I'm working on a school project, and I need help with this trigger2dstay thing. I'm trying to make it were when my player's collider tag enters the triggers collider, an image pops up which works, but I'm also trying to make it were when the play does the same thing and presses E, it will trigger a animation, but when my 2d player walks in the trigger and presses E, nothing happens. pressing E only works when you are moving and pressing it, and not staying still.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class buttonele : MonoBehaviour
{
public GameObject Obje;
public GameObject blockers;
public GameObject eledoorn;
public GameObject eledormation;
bool Unlock;
// Start is called before the first frame update
void Start()
{
Obje.SetActive(false);
eledormation.SetActive(true);
Unlock = false;
}
void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "Player")
{
Unlock = true;
Obje.SetActive(true);
}
if (Unlock == true && Input.GetKeyDown(KeyCode.E))
{
Destroy(blockers);
Destroy(eledoorn);
eledormation.GetComponent<Animator>().Play("eleopen");
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
Obje.SetActive(false);
}
}
}
The following code means that your player enters another player's collider, do you really want that?
if (other.tag == "Player")
To solve this problem, test the logic of the code. If you want your player to be able to use the E key by entering a certain area, you need to enter the tag of that place in this field. If you want the boxes to open, give them a special tag (Item for e.g) and change the code as follows.
if (other.CompareTag("Item"))
{
// do something..
}
I am creating a game in unity where the player can drag one object (a card) over another (an enemy). If they drag the card over the enemy I want to run some code, and if not I want to return the card to its initial position. I placed a collider on both objects but it isnt working the way it should. I am assuming this is because the object is getting moved back to its initial location before the physics engine sees the collision, but I don't know how to fix this. I am deactivating the collider on the card while moving it to avoid having it trigger collisions until the player has placed it using the onmousedown and onmouseup events. Any tips on how to fix this behavior? can I force the physics engine to check collisions with the onmouseup event?
I know the collisions are working because when I turn off the return to initial position behavior the game functions as expected.
How about useing the collider as trigger and do not use rigidbodys or anything.
Then if there is a trigger enter event set bool as true. If there trigger exit reset the bool to false.
Now if you "Release" the card check if bool is true or false.
true: Set the cards position to the player or what you want
false: Reset the cards position to the start
Now beeing a little mroe fancy you can set a lighted border around the card when bool is active (just check in update)
Example:
public class Card : MonoBehaviour {
private bool isHolding;
private bool isHovering;
public Vector3 startPos;
public void Start() {
startPos = transform.position;
}
public void Update() {
// Code where you check if the card is Clicked and Moved by the player
// If so set isHolding = true
// dont enter the check if holding blablabla when animation stuff is happening
if (doAnimationStuff) {
// do animation
// Destroy Object
return;
}
// Code to check if isHolding == false
if (!isHolding) {
if (!isHovering) {
transform.position = startPos;
} else {
doAnimationStuff = true;
}
}
}
private void OnTriggerEnter(Collider other) {
// Check if other is a player
// if so set isHovering = true
}
private void OnTriggerExit(Collider other) {
// Check if other is a player
// if so set isHovering = false
}
}
Score does not reach 0 once the player dies.
I tried all different kinds of methods and changing values but nothing seemed to work.
{
//when the player's box collider collides with another box collider
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
//scene moves to a game over screen
SceneManager.LoadScene("Player Death");
//Reset score everytime player dies
void resetScore()
{
PlayerPrefs.SetInt ("Score", 0);
}
}
}
// Start is called before the first frame update
void Start()
{ }
// Update is called once per frame
void Update()
{
}
}
It was just building the score up from where it left off and didnt do as i programmed.
You are declaring a new method resetScore() when a player dies, but you're never calling it.
What you should do is declare the method outside of OnTriggerEvent() and call it when a player dies:
//when the player's box collider collides with another box collider
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
//scene moves to a game over screen
SceneManager.LoadScene("Player Death");
//Reset score everytime player dies
resetScore();
}
}
// Start is called before the first frame update
void Start()
{ }
// Update is called once per frame
void Update()
{
}
void resetScore()
{
PlayerPrefs.SetInt("Score", 0);
}
Have you tried to reset the score right when the player dies? I assume you reset it on game start. Try doing it right after the player dies, or use it in a constructor when creating a new game/player. It isn´t clear how you implemented it, it would help if you post more pieces of code.
[EDIT]
Firstly, you are declaring the ResetScore Method, but you don´t call it to execute. So you need to declare it outside the "OnTriggerEnter" method, and then call it inside the method like this :
ResetScore();
Secondly, Why don´t you try to use property instead of SetInt?
public int Score { get; set;}
Then you can change it like this :
if (other.gameObject.tag == "Player")
{
Player.Score = 0;
//scene moves to a game over screen
SceneManager.LoadScene("Player Death");
//Reset score everytime player dies
}
So here it is, Iam making a game for my thesis. The game is called hacky sack, Iam having a problem when my trigger is true because the collision for my character and the object is passing through and if idon`t have a trigger it was just basically hitting it even when my Player is running.
And can you guys help me on how the Object/Sack going up randomly when it hit. So here is my code :
public void Sipa()
{
if (canSipa == true)
{
_pitcha.GetComponent<Rigidbody2D>().AddForce(new Vector2(-400, 1000));
}
}
}
And here is for my object
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player")
{
_player.GetComponent<PlayerManager>().canSipa = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
_player.GetComponent<PlayerManager>().canSipa = false ;
}
}
}
add a second collider, slightly larger then the first, mark it trigger. leave the smaller inner collider for physics. you "foot" should enter the trigger and activiate it, then collide with the inner collider. make sure the collider marked trigger is the larger one.
I am trying to make a chat dialogue in unity which should show up when colliding with an object and press a key. The chat dialogue should remain until collision exit. The issue is that the chat dialogue shows up on key press when in collision but turns false in the same second so it flickers. the bool is being reset instead of remaining true while in collision.
hitColliders = Physics.OverlapSphere(transform.position, 5f);
int i = 0;
while (i < hitColliders.Length) {
if (hitColliders [i].gameObject.tag == "gameobjectCollision") {
if (Input.GetKey (KeyCode.F)) {
Debug.Log ("pressed F");
DialogueController.showConversation = true;
}
break;
}
else {
Debug.Log ("Conversation Off");
DialogueController.showConversation = false;
}
i++;
}
Also, script is being attached properly and mesh collider is set with colliding game object. Thanks.
You could do the following
Generate an event from OnTriggerEnter.
Create an ChatDialogueHandler Script and an add listener to that event.
Enable the chat dialogue box when event is raised and disable the chat dialogue box after some specific time.
You should really think about doing this off of triggers and not collision. So making a separate trigger box for each NPC that you can talk to in your game. And then make use of OnTriggerEnter and OnTriggerExit in your player script. OnTriggerEnter you can allow the conversation to start by a button click, and then OnTriggerExit you can turn off the coversation.
public void Update()
{
if (inInteractRange && Input.GetKeyDown(KeyCode.E))
{
DialogueSystemManager.Instance.StartDialogueForNpc(interactableNPCName);
}
}
private void OnTriggerEnter(Collider col)
{
inInteractRange = true;
}
private void OnTriggerExit(Collider col)
{
inInteractRange = false;
DialogueSystemManager.Instance.CloseDialogueWindow();
}