Question by
TechKitty_ · Nov 29, 2021 at 05:31 PM ·
instantiate prefab
Instantiated arrow rotates with bow,Arrow rotates with the bow.
Once the arrow is instantiated, it rotates with the bow. The arrow is a prefab. The arrow must stay the child of the bow.
public GameObject projectile;
public float force;
public Transform shot;
void Update()
{
if (GetComponentInParent<PlayerAttack>().hp > 0)
{
Vector2 bowPos = transform.position;
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 bowDirection = mousePos - bowPos;
transform.right = bowDirection;
}
if (Input.GetButtonDown("Fire1"))
{
Fire();
}
}
void Fire()
{
if (GetComponentInParent<PlayerAttack>().hp > 0)
{
GameObject newProjectile = Instantiate(projectile, shot.position, shot.rotation);
newProjectile.GetComponent<Rigidbody2D>().velocity = transform.right * force;
newProjectile.transform.parent = gameObject.transform;
}
}
Help is very much appreciated.
Comment
Sorry first time using this system, the post posted twice?
Your answer