Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cohav0310 · May 18, 2018 at 08:32 PM · instantiate2d gameshootingbullettopdown

How do I make a 2d bullet object move towards the players original position when it is spawned?

When the player enters a circle collier trigger, the turret object, which is a child of the enemy. Will rotate towards the player and shoot a bullet every 2 seconds. Right now, the bullets just spawn. I would like some help getting the bullets to move towards the player. NOTE: I don't want the bullet to follow the player, just to fly towards the position the player was at when it had spawned. Here is my code:

Enemy Shooting Script: public class EnemyShooting : MonoBehaviour {

 public GameObject enemyBullet;
 public Transform player;
 public float shootTimer;
 public GameObject enemyTurret;
 public bool playerInRange;
 public float rotateSpeed;
 public GameObject enemyBarrel;


 // Use this for initialization
 void Start () {
     playerInRange = false;
     shootTimer = 2;
 }
 
 // Update is called once per frame
 void Update () {
     
     if(playerInRange == true)
     {
         Vector3 vectorToTarget = player.position - transform.position;
         float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 90;
         Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotateSpeed);
         shootTimer -= Time.deltaTime;

         if(shootTimer <= 0)
         {
             EnemyShoot();
             shootTimer = 2;
         }
     }

 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         playerInRange = true;
     }  
 }

 private void OnTriggerExit2D(Collider2D collision)
 {
     if (collision.gameObject.CompareTag("Player"))
     {
         playerInRange = false;
     }
 }


 private void EnemyShoot()
 {
     GameObject bulletShoot = (GameObject)Instantiate(enemyBullet, transform.position, Quaternion.identity);
 }

}

Enemy Bullet Script: public class EnemyBullet : MonoBehaviour {

 public int damage;
 public float speed;
 public Transform player;

 // Use this for initialization
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     transform.position = transform.up;
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     GameObject GO;
     if (collision.gameObject.CompareTag("Player"))
     {
         GO = collision.gameObject;
         GO.GetComponent<PlayerMovement>().playerHealthCount -= damage;
         Destroy(gameObject);
     }

     if (collision.gameObject.CompareTag("NorthWall"))
     {
         Destroy(gameObject);
     }
     if (collision.gameObject.CompareTag("SouthWall"))
     {
         Destroy(gameObject);
     }
     if (collision.gameObject.CompareTag("EastWall"))
     {
         Destroy(gameObject);
     }
     if (collision.gameObject.CompareTag("WestWall"))
     {
         Destroy(gameObject);
     }

 }

}

Thank you for your time.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by MT369MT · May 24, 2018 at 07:15 PM

Hi, in EnemyShoot() and after instantiating the Bullet add this:

 bulletShoot.GetComponent<Rigidbody2D>().velocity = (player.transform.position - transform.position).normalized * bulletSpeed;

Be sure to add a rigidbody2d to your bullet and to set its gravity scale to 0. Create also a new float called for example bulletSpeed.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tobychappell · May 24, 2018 at 07:24 PM 0
Share

To build on top of your answer, i recon it would be aesthetically pleasing to make the bullet face the same direction as the velocity, that's if the bullet's image/sprite looks like it could benefit from that (i.e not a ball).

avatar image Cohav0310 · May 24, 2018 at 10:54 PM 0
Share

Thank you so much! also thanks for the heads up @Tobychappell It's a ball now, but it will be changed eventually.

avatar image
1

Answer by JusSumGuy · May 24, 2018 at 10:09 PM

This is the code for the bullet to just go to the players position at the time the bullet is instantiated.

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Ship2Rocket : MonoBehaviour {
 
     [SerializeField] GameObject explosionPrefab;
     [SerializeField] float speed;
     float finalSpeed;
     GameObject player;
     Vector3 currentPlayerPos;
     
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         if(player)
             currentPlayerPos = player.transform.position;
         finalSpeed = speed * Time.deltaTime;
     }
 
     void Update()
     {
         transform.LookAt(currentPlayerPos);
         transform.position = Vector3.MoveTowards(transform.position, currentPlayerPos, finalSpeed);
         if (transform.position == currentPlayerPos)
         {
             Instantiate(explosionPrefab, transform.position, Quaternion.identity);
             Destroy(gameObject);
         }
     }
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Damage problem 1 Answer

Invoke is not working properly. 0 Answers

Very basic Instantiate Question 2 Answers

Instantiate after setting values? 0 Answers

Shooting a bullet object toward the mouse position in a 2D. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges