Home>
I want to make the bullet disappear when the enemy character hits the enemy character and reduce the HP of the enemy character that hits it. In addition, since bullets are generated from the character, we want to avoid physical contact.
Error messageEven if a shot is in contact with an enemy, it will pass through without disappearing. Also, HP has not decreased.
Applicable source codeA bullet script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript: MonoBehaviour
{
public float speed = 10f;
// Start is called before the first frame update
void Start ()
{
}
// Update is called once per frame
void Update ()
{
transform.position + = transform.right * speed * Time.deltaTime;
}
void OnTrigerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "enemy")
{
Destroy (gameObject);
}
}
}
Enemy HP script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPmanager: MonoBehaviour
{
[Header ("HP")] public float HP;
// Start is called before the first frame update
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnTrigerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "bullet")
{
if (HP>0)
{
HP-= 1;
}
else
{
Destroy (gameObject);
}
}
}
}
Bullet isTrigger is checked, enemy isTrigger is not checked, Rigidbody is attached to both, and Simulated is checked.
I tried various things with reference to http://kimama-up.net/unity-trigger/, but the results did not change.
-
Answer # 1
Related articles
- c # - b:about takahashi-kun and character string compression
- special character operation of python is difficult
- c # - move the character at a constant speed
- c # - movement of player character in tps does not work
- c # - about joystick operation
- c # - [unity 2d] i want to fire a bullet in the direction of the enemy
- c # how to get if the line following a line containing a particular character contains a particular character
- how to read character string from standard input one by one in c # and cast to string type and put in array
- c # - about character recognition engine in wpf
- c # - animation to change the direction of the character (bool)
- c # - i want to programmatically put the character variable set in another script into text (script)
- unity - the enemy that hit the bullet does not disappear
- c # - can't build when using vroid character in unity please help me!
- c # - i want to separate the character string of string (regular expression)
- c # - i want to connect common characters from the character strings of each element of list
- c # - i want to change the value read as a character string from a csv file to double type
- character tracking by camera using unity and c #
- c # - regular expression first character last character deletion or other extraction method
- left and right character placement within a line in a c # word document
- c # - move left and right, the character turns around (timing is random)
Related questions
- c # - player production in unity (shooting game)
- c # - please tell me how to use the additional components of unity's volunteer production
- c # - conversion from enum type to enum type
- c # - missingreferenceexception occurs even though there is a reference
- c # - i want to update the value
- c # - i want the camera to follow the player character in a tps game, but if i make multiple players in multiplayer, the camera
- c # - [unity] even though "has exit time" of animator is specified, it will transition to another state earlier than t
- c # - == becomes false even though the strings are equal
- c # - how to use start () in unity
- c # - in a script? error cs1056: unexpected character'?' is displayed even though there is no
The name of the method defined in your code is
OnTrigerEnter2D
. Isn't it a spelling error?How about setting it to OnTriggerEnter2D?