Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 alyasa50230 · May 24, 2019 at 10:39 AM · bulletenemy aimovment

enemy shooting bullet

Good morning. I want to make top down space game. I made a player and an enemy but the problem is that I can't make the bullets continue going forward, I only can make it stop at the point which the player was when it was summoned! need help please.

here is the bullet script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Test : 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 · Show 1
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 akaBase · May 24, 2019 at 10:53 AM 0
Share

$$anonymous$$ove everything in your Start() method after player = GameObject.FindGameObjectWithTag("Player"); into the Update() You are setting values once on script start and they will change during gameplay

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ben_The_boxer · May 24, 2019 at 12:43 PM

Try mine if you like, its up and running though i am sure its far from perfect but it does have a timer to explode which you might like on your space shooter. I am also making one myself so feel free to ask me any other questions that might pop up

 using UnityEngine;
 using System.Collections;
 
 public class Missile : MonoBehaviour
 {
 
     public Transform target;
 
     public float speed = 70f;
 
     public int damage = 50;
 
     public float explosionRadius = 0f;
     public GameObject exploEffect;
     public float proximity;//<-----------I tried to implement this as a means of detonation without colliders, no luck yet
 
     public float missileTime;
 
     // Update is called once per frame
     void Start()
     {
         //if (target == null))
         target = GameObject.FindWithTag("Player").transform;
     }
 
     void Update()
     {
 
         if (target == null)
         {
             Destroy(gameObject);
             return;
         }
 
         Vector3 dir = target.position - transform.position;
         float distanceThisFrame = speed * Time.deltaTime;
 
         if (dir.magnitude <= distanceThisFrame + proximity)
         {
             HitTarget();
             return;
         }
         if (missileTime <= 0f)
         {
             HitTarget();
         }
         missileTime -= Time.deltaTime;
 
 
         transform.Translate(dir.normalized * distanceThisFrame, Space.World);
         transform.LookAt(target);
 
     }
     private void OnCollisionEnter(Collision collision)
     {
         HitTarget();
     }
 
 
 
     void HitTarget()
     {
         GameObject effectIns = (GameObject)Instantiate(exploEffect, transform.position, transform.rotation);
         Destroy(effectIns, 5f);
 
         if (explosionRadius > 0f)
         {
             Explode();
         }
 
         Destroy(gameObject);
     }
 
     void Explode()
     {
         Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
         foreach (Collider collider in colliders)
         {
             if (collider.tag == "Player")
             {
                 collider.gameObject.GetComponent<PlayerHealth>().DamagePlayer(damage);
                 //Damage(collider.transform);
 
             }
             Destroy(gameObject);
         }
     }
 
     void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(transform.position, explosionRadius);
     }
 }
 


if you would prefer to stick with yours id recommend moving some bits from the "start" to "Update" and look into using "player.transform.position" as there is no need to manually extract the position when using that.

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

107 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

Related Questions

How to make an object ricochet in 2d 2 Answers

Trying to make an enemy fire at the player 1 Answer

Top down shooter, bullet movement issues 3 Answers

Help! Can't destroy my bullet object because is a rigidbody 1 Answer

Best way to make a gun? 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