If "OnTriggerEnter" then "another OnTriggerEnter" ?
Hello, I'm a rookie and i work on my first 2d game. I want my player character to be able to kick in a rock, and the rock to damage enemy once it's kicked, but i can't do it. Here's my code linked to my rock. Do you have any idea ?
Thanks !
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag ("PlayerAttackCollider")) {
gameObject.GetComponent<Rigidbody2D> ().isKinematic = false;
gameObject.GetComponent<Rigidbody2D>().position = new Vector2(gameObject.GetComponent<Rigidbody2D>().transform.position.x + 1, gameObject.GetComponent<Rigidbody2D>().position.y);
if (col.CompareTag ("Enemy"))
{
SendMessageUpwards("Damage", damage);
Destroy(gameObject);
}
}
}
I am guessing the kick is executed, but the damage is not?
Answer by karadag · Feb 08, 2017 at 01:46 PM
Try this:
private bool isPlayerAttack = false;
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("PlayerAttackCollider"))
{
isPlayerAttack = true;
gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
gameObject.GetComponent<Rigidbody2D>().position = new Vector2(gameObject.GetComponent<Rigidbody2D>().transform.position.x + 1, gameObject.GetComponent<Rigidbody2D>().position.y);
}
if (col.CompareTag("Enemy") && isPlayerAttack)
{
SendMessageUpwards("Damage", damage);
Destroy(gameObject);
}
}
Answer by Fikce · Feb 10, 2017 at 12:04 PM
Thanks !
I found a way to fix my problem with the following code meanwhile ! And i have added the detection of my player position to kick the rock to the left and to the right.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets._2D;
public class poubelle : MonoBehaviour {
public int damage = 1;
public float velocity = 10f;
public float curVelocity;
void Start() {
gameObject.GetComponent<Rigidbody2D> ().mass = 5;
gameObject.GetComponent<Rigidbody2D> ().isKinematic = false;
curVelocity = 0f;
}
void Update()
{
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(curVelocity, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("AttackCollider"))
{
gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
if (GameObject.FindGameObjectWithTag("Player").transform.position.x < gameObject.transform.position.x)
{
curVelocity = velocity;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(curVelocity, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
if (GameObject.FindGameObjectWithTag("Player").transform.position.x > gameObject.transform.position.x)
{
curVelocity = velocity*-1;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(curVelocity, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
gameObject.tag = "AttackCollider";
gameObject.transform.GetComponent<Animation>().Play("poubelleblink");
Destroy(gameObject, 1);
}
}
}
Your answer
Follow this Question
Related Questions
Trigger for every tilemap square. 1 Answer
OnTriggerEnter2D Detect Trigger Forever 1 Answer
OnTriggerEnter2D problem 1 Answer
When instantiating 3 clones are created rather then 1 2 Answers
Falling object respawner 1 Answer