- Home /
Question by
ALEGOMan · Apr 07, 2016 at 06:59 PM ·
transformchildlocalscale
How do I access a child of my player through another script?
I'm trying to make an enemy shoot at the player no matter their positions, but it only seems to be shooting on the right side, I think it's something to do with my sprite being a separate child. How would I access just the child and not the whole player?
using UnityEngine; using System.Collections;
public class ShootAtPlayer : MonoBehaviour {
public float playerRange;
public GameObject enemyProjectile;
public Player player;
public Transform launchPoint;
public float waitBetweenShots;
private float shotCounter;
void Start()
{
shotCounter = waitBetweenShots;
}
void Update()
{
player = FindObjectOfType<Player>();
player.transform.FindChild("Graphics");
if (player != null) //only active when player is alive
{
Debug.DrawLine(new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z));
shotCounter -= Time.deltaTime;
if (transform.localScale.x < 0 && player.transform.position.x > transform.position.x + playerRange && shotCounter < 0 )
{
Instantiate(enemyProjectile, launchPoint.position, launchPoint.rotation);
shotCounter = waitBetweenShots;
}
if (transform.localScale.x > 0 && player.transform.position.x < transform.position.x - playerRange && shotCounter < 0)
{
Instantiate(enemyProjectile, launchPoint.position, launchPoint.rotation);
shotCounter = waitBetweenShots;
}
return;
}
}
Comment