• 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 $$anonymous$$ · Feb 22, 2019 at 01:32 AM · projectile

why dont my enemy bullets kill the playership?,why dont my enemy bullets kill the player?

  using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class Projectiles : MonoBehaviour
     {
         public float range = 50.0f;
         public float enemyGunImpulse = 20.0f;
         // The target marker.
         [SerializeField] Transform playerShip;
         [SerializeField] Transform target;
         [SerializeField] Transform parent;
         // Angular speed in radians per sec.
         [SerializeField] float speed;
         bool onRange = false;
         public Rigidbody projectile;
         private void Start()
         {
             float rand = Random.Range(1.0f, 2.0f);
             InvokeRepeating("Shoot", 2, rand);
             AddSphereCollider();     
         }
     
         private void AddSphereCollider()
         {
             Collider sphereCollider = gameObject.AddComponent<SphereCollider>();
             sphereCollider.isTrigger = false;
         }
     
         void Shoot()
             {
     
                 if (onRange)
                 {
     
                     Rigidbody enemyGun = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
                     enemyGun.AddForce(transform.forward * enemyGunImpulse, ForceMode.Impulse);
                 }
             }
         void Update()
         {
             Vector3 targetDir = target.position - transform.position;
     
             // The step size is equal to speed times frame time.
             float step = speed * Time.deltaTime;
     
             Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
             Debug.DrawRay(transform.position, newDir, Color.red);
     
             // Move our position a step closer to the target.
             transform.rotation = Quaternion.LookRotation(newDir);
        
             onRange = Vector3.Distance(transform.position, playerShip.position) < range;
     
             if (onRange)
                 transform.LookAt(playerShip);
         }
     }
     
 
 

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
0
Best Answer

Answer by SlowCircuit · Feb 22, 2019 at 03:26 AM

You're destroying the bullet immediately after creating it.

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
avatar image
0

Answer by $$anonymous$$ · Feb 22, 2019 at 03:59 AM

Hi, I want to Thank you for responding. I was able to get the enemy gun to kill the player finally but the death fx explosion for the player doesn't happen? Any feedback is appreciated :)

I removed this line of code: Destroy(bullet.gameObject, 2);

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 zereda-games · Feb 22, 2019 at 06:13 AM 0
Share

also have you set up your layer matrix?

avatar image $$anonymous$$ zereda-games · Feb 22, 2019 at 08:32 PM 0
Share

Hi, I updated my code. At one point I had scripts separated but then when I did the player ship would be cloned in spawn time and I was getting many errors but the enemy bullets worked. I just combined the enemy management.cs with the projectiles.cs and no longer get errors but the enemy bullets do not kill the player ship.

Any feedback Ino longer get enemy bullets to kill player ship is welcomed :/

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

100 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

Related Questions

Setting IgnoreCollision on Network spawned objects. (Unet) 2 Answers

shoot forward and comeback 0 Answers

Getting launch angle for projectile given height, distance and speed in 3D 2 Answers

The bullet only moves in one direction and does not rotate with object. Code: 1 Answer

Arching and Rotating arrow. 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