Getting transform of an object to which script wasnt assigned to
Following online (Brakeys) tutorials, I am trying to make a bullet exit a gun for a top down shooter. The "Bullet" is a child of the "Weapon" which in turn is a child of the "Player". The script assigned to the "Weapon" is the following:
public class Shooting : MonoBehaviour {
public float FireRate = 0f;
public float Damage = 10f;
public LayerMask NotToHit;
float TimeToFire = 0f;
Transform Bullet;
void Update () {
if (FireRate == 0f){
if (Input.GetKeyDown(KeyCode.Mouse0)){
Shoot();
Debug.Log("Working2");
}
}
}
void Shoot(){
Vector2 MousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).z);
Vector2 BulletPosition = new Vector2 (Bullet.position.x, Bullet.position.z);
RaycastHit2D hit = Physics2D.Raycast (BulletPosition, MousePosition - BulletPosition, 100, NotToHit);
Debug.DrawLine (BulletPosition, MousePosition);
}
}
The problem, from what I understand, arises from the "Transform Bullet" and "Vector2 BulletPosition = new Vector2 (Bullet.position.x, Bullet.position.z);". There isn't an actual main error (only minor ones), the game runs, but the path of the bullet won't get drawn. Shouldn't the "Transform Bullet" read the tranform of the "Bullet", which is the child of the "Weapon", even if this script is only attached to the "Weapon" itself?
Hope it's clear, thx in advance!
Your answer
Follow this Question
Related Questions
Set parent of an object when they collide? 2 Answers
How to make health and damage script function correctly? 3 Answers
Health and Damage Script Not Working 2 Answers
Huge Health and Damage Script Problem 0 Answers
Move parent without moving the child 1 Answer