- Home /
How to hit a specific object
Hi.
I want the tag to work as the tag.
When I shoot the - only falls the one which i touched.
But the tag objects fall together. When i touch balloon's karzina, i want the gravityScale to be changed.
Thanks in advance.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Arrow_1 : MonoBehaviour {
public float speed;
public float lifeTime;
public float distance;
public int damage;
public LayerMask whatIsSolid;
GameObject[] karzina;
void Start () {
Invoke("DestroyArrow_1", lifeTime);
}
void Update () {
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Destroy(gameObject);
}
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if (hitInfo.collider != null) {
if (hitInfo.collider.CompareTag("Balloon")) {
hitInfo.collider.GetComponent<Balloon>().TakeDamage(damage);
}
Destroy(gameObject);
karzina=GameObject.FindGameObjectsWithTag("karzina");
foreach (GameObject r in karzina){
r.GetComponent<Rigidbody2D>().gravityScale = 2f;
}
}
transform.Translate(Vector2.up * speed * Time.deltaTime * 1.5f);
}
}
Answer by xxmariofer · Apr 01, 2019 at 06:54 PM
Change your update method to this one
void Update () {
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Destroy(gameObject);
}
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if (hitInfo.collider != null) {
if (hitInfo.collider.CompareTag("Balloon")) {
hitInfo.collider.GetComponent<Balloon>().TakeDamage(damage);
hitInfo.collider.gameObject.transform.GetChild(0).GetComponent<Rigidbody2D>().gravityScale = 2f;
}
Destroy(gameObject);
}
transform.Translate(Vector2.up * speed * Time.deltaTime * 1.5f);
}
yes, the kazira is the first child of the balloon? any errors? yers only update
sorry, karzina is a parent, balloon is a child
Answer by xxmariofer · Apr 01, 2019 at 07:07 PM
void Update () {
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Destroy(gameObject);
}
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if (hitInfo.collider != null) {
if (hitInfo.collider.CompareTag("Balloon")) {
hitInfo.collider.GetComponent<Balloon>().TakeDamage(damage);
hitInfo.collider.gameObject.transform.parent.GetComponent<Rigidbody2D>().gravityScale = 2f;
}
Destroy(gameObject);
}
transform.Translate(Vector2.up * speed * Time.deltaTime * 1.5f);
}
mark the answer as correct for future readers please
Your answer
Follow this Question
Related Questions
How Expensive is Find function? 3 Answers
What's the most efficient way of finding child objects at runtime? 1 Answer
Find object from another scene 1 Answer
Best method to manage multiple tags 2 Answers