Mouse Click inside OnTriggerEnter - unity3d

Video URL for easy understanding - http://tinypic.com/r/28jdyyq/9
In this video, you can see my problem, when the sword touches the enemy.. enemy gets destroyed.. But i want when i mouseclick (or hit), then only ..enemy should destroy..
void OnTriggerEnter(Collider col)
{
if (col.GetComponent<Collider>().tag == "enemy")
{
Destroy(col.gameObject);
}
}
This is my code, i have enemy and Player with sword (with collider) , everything is perfect, i want when i click mousebutton then only sword should kill enemy,
But, What is happening when i bring my player (with sword) near enemy and sword touches enemy, it is killing enemy without i hit by sword.
I tried the below code also by adding mouse click event inside Trigger , but nothing happens. Any idea Please
void OnTriggerEnter(Collider col)
{
if (Input.GetButtonDown("Fire1")){
if (col.GetComponent<Collider>().tag == "enemy"){
Destroy(col.gameObject);
}
}
}
Here is code for Swing -
if (Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("hit");
}
Here hit is trigger in animation controller and make transition to the animation clip

you can use Animation Events to make true a Boolean when the sword rises in animation and turn it to false when the sword goes down and check that Boolean when OnTriggerEnter is called
make a variable like hit set it to true and false via animation Event
public bool hit;
void OnTriggerEnter(Collider col)
{
if (hit){
if (col.GetComponent<Collider>().tag == "enemy"){
Destroy(col.gameObject);
}
}

Related

how do i make sword work with enemy collider

here is my current code:
public GameObject enemy;
void OnCollisionEnter(UnityEngine.Collision collisionInfo)
{
if (collisionInfo.collider.tag == "sword")
{
Debug.Log("works");
enemy.SetActive(false);
}
else
{
Debug.Log("doesnt work");
}
}
i have attached this to the enemy and also i tried a different script attached to the sword
void OnTriggerStay(Collider col)
{
if (Input.GetButtonDown("Fire1"))
{
if (col.GetComponent<Collider>().tag == "enemy")
{
Destroy(col.gameObject);
}
}
}
both codes don't work, it seems that the problem isnt with the sword collision cus i have also added the tag to another gameobject and it doesnt work. i looked online but havent found anything that works so fat
update: it seems that i have forgot you need a rigid body for collision detection. simple mistake but it made my code not work!
For collision enter to work, both the sword and the enemy colliders need to have the "IsTrigger" box unticked.
For on triggerstay is the opposite, both need to have that box ticked.
Basically, Triggers do not collide they call that OnTriggerEnter function, while non-triggers use the OnCollisionEnter.

Movement stops whenever player touches ground

I am trying to learn the basics of unity collision and I want the movement of the player to stop whenever it touches an obstacle but the movement stops whenever it touches the ground. I tagged the obstacle with a tag called obstacle and left the ground untagged but whenever the player touches the ground it stops all movement. Does anyone know how to fix this? Heres my code: `using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
public Movement playerMovement;
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.tag == "Obstacle");{
playerMovement.enabled = false;
}
}
}
The code you've written stops the movement of the player whenever its collides with something. The code wouldn't even compile because of the "{" at the end.
void OnCollisionEnter(Collision collisionInfo)
{
// returns true or false in the console depending on whether player its colliding with the obstacle
// code won't compile because of the "{" at the end
Debug.Log(collisionInfo.collider.tag == "Obstacle");{
playerMovement.enabled = false;
}
To fix that you can use an if statement and remove the "{" at the end like shown below.
It is also recommended to use CompareTag when working with tags.
void OnCollisionEnter(Collision collisionInfo)
{
if(collisionInfo.collider.CompareTag("Obstacle"))
{
playerMovement.enabled = false;
}
}

Player disappears on Game mode in Uniity 2D while switching scenes

I'm making a function that it switches 2 scenes.
When the player enters the trigger, automatically unity switches the scene and in the "Scene view", the player is there, as I wanted. But in the game view, the player dissappears, I don't know why.
I tried to remove a script that it's function is teleport the player to a specific point of the new scene, and it worked.
But obviously I don't want that, because unity automatically teleports the player to a random point, how can I fix that?
Here are the scripts:
TeleportScript:
public class EnterScene : MonoBehaviour
{
public string transitionName; //Also 1-1
void Start()
{
if (transitionName == PlayerController.sharedInstance.areaTransitionName)
{
PlayerController.sharedInstance.transform.position = transform.position; //Moves the player to GameObject position
}
}
// Update is called once per frame
void Update()
{
}
}
https://pastebin.com/jdSPhR3s <-----SceneLoader
https://pastebin.com/KcwHVBfQ <----PlayerController
And a screenshot of my problem:
Problem

Transmiting a parameter in multiplayer

I am working on a Unity 2D fps multiplayer game. Whenever a player presses Space they instantiate a bullet and when a bullet hits any player it has to destroy itself and push the player in the bullet direction. My code looks like this:
void OnTriggerEnter2D(Collider2D col)
{
if (isServer == false)
return;
if (col.gameObject.tag != "bullet")
return;
CmdTrigger(col.gameObject);
}
[Command]
void CmdTrigger(GameObject col)
{
RpcTrigger(col);
}
[ClientRpc]
void RpcTrigger(GameObject col)
{
Rigidbody2D rb;
rb = col.GetComponent<Rigidbody2D>();
if (rb.velocity.x > 0)
RpcExplode(Mathf.Sign(1));
else
RpcExplode(Mathf.Sign(-1));
//Network.Destroy(col);
}
The problem is that when a bullet collides a player I can't transmit well the GameObject to the other players so every player can destroy the bullet on their client. When a bullet hits a player it doesn't destroy itself, it just passes through and the Console shows me this error: "NullRefrenceException: Object reference not set to an instance of an object", and if I click on it, it brings me to this line:
rb = col.GetComponent<Rigidbody2D>();
Here is my character control full code: https://pastebin.com/L1DEmQv1
Any ideas? It is very important for me to fix it as soon as possible. Thanks.

Recommended Approach to multiple colliders

I am have some difficulty with what is probably a very silly thing. I have an enemy gameobject that depending on where it is hit (collision) - either it, or the play dies. I think the simplest way to describe this is by using the classic Super Mario Bros. game as an example.
As you all know, if the player runs into the enemy - the player will lose - UNLESS he jumps on top of the enemy's head, in which case the enemy should die.
My initial idea was to create two colliders on the gameobject:
Blue border represents a BoxCollider2D - that if collided with - will cause player to lose (notice it is slightly lower from the top)
Green border represents a BoxCollider2D on a child gameobject - that if collided with - will cause the gameobject to die.
The following is a simplified version of the code I used:
// Collider #1
public void OnCollisionEnter2D(Collision2D collision)
{
// Trigger 'Game-Over' logic
}
// Collider #2
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy(this.gameObject);
}
}
This kind-of works, however momentarily after colliding with Collider #1, Collider #2 is also trigger - and while the enemy is destroyed, the player also loses.
I have been playing with the RigidBody2D values to prevent the player from entering the 2nd collider when hitting the enemy from the top - but apparently with that force / speed, the colliders may be slightly inaccurate (or maybe I'm just doing it wrong?).
I have looked into RayCasts but this seems too complex for something that me appears rather trivial (casting rays on all four sides and four vertices of the player - assuming that the player has a box collider).
What I have resorted to 'for the moment' is a a single collider with a simple piece of code that I am unhappy with, and doesn't always work:
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
float yVelocity = collision.gameObject.transform.rigidbody2D.velocity.y;
if (yVelocity < 0)
{
Debug.Log("Enemy will lose..." + yVelocity);
Destroy(this.gameObject);
}
else
{
// Trigger 'Game-Over' logic
}
}
}
I'm currently working in 2D mode, but solutions for 3D mode (while maybe more complicated than necessary for my question) will also be considered.
Thanks guys.
as a game developer you always have many ways to solve a problem or make a gameplay.
first of all i have to say you should make a polygon collider 2d fo you objects and chracters. just colliding pictures is not very good as i see you used box cilliders in your game.
a good choice can be that you attach and empty object ot you player and set its position under foots of you player and check of enemy hit that enemy dies else if enemy hit main character object, player dies.
another choice can be when o objects collide check y position of 2 objects. of player was higher he kiils, else enemy kills the player.
if you think more you will find more answers.
you have to examin diffrent ways and find most efficient.
I think it will be easy to disable the other collider when one is triggered. You can easily enable/disable colliders with collider.enabled = true; or collider.enabled = false;
// Collider #1
public void OnCollisionEnter2D(Collision2D collision)
{
// Trigger 'Game-Over' logic
// collider2.enabled = false;
}
// Collider #2
public void OnCollisionEnter2D(Collision2D collision)
{
// collider1.enabled = false;
if (collision.gameObject.tag == "Player")
{
Destroy(this.gameObject);
}
}
This way it will be pretty lightweight and easy to implement.
One way of implementing what you want is to put each collider in its child own game object and use the IsTouching() method.
void OnTriggerEnter2D(Collider2D other){
if(GameObject.Find("Top Trigger").GetComponent<BoxCollider2D>().IsTouching(other)){
Destroy(transform.gameObject)
}
if(GameObject.Find("Bottom Trigger").GetComponent<BoxCollider2D>().IsTouching(other)){
Destroy(other.gameObject)
}
}