Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ImagedOps · Apr 04, 2013 at 12:06 PM · bulletprojectilenotworking

Can't get bullet to spawn and move

Doing a certificate course in Game Development and the code that we've been given hasn't been working properly for me, mainly the projectile firing script for the main player and the enemies, this here is for the enemies yet, the main player's script for firing is relatively the same, The main problem I'm have here is that the enemy doesn't actually fire until it's in contact with me and when it is it spawns continous copies of projectile that are invisible yet in the hierachy. The main player I can't even get the projectile to appear in the hierachy at all\ Could I get some help please guys :D!
using UnityEngine;
using System.Collections;

 public class Enemy : MonoBehaviour {
     
     public GameObject ExplosionType;
     public float RespawnTimer = 10.0f;
     public int Health = 100;
     public float StopDistance = 5.0f;
     public float MovementSpeed = 5.0f;
     public float ProjectileForwardForce = 10;
     public float ProjectileUpForce = 10;
     public GameObject objectToSpawn;
     
     float CurrentTimer;
     GameObject ParticleExplosion;
     bool isAlive = true;
     // Use this for initialization
     void Start () {
         CurrentTimer = RespawnTimer;
     
     }
     
     // Update is called once per frame
     void Update () {
         if ( isAlive == false)
         {
             CurrentTimer -= Time.deltaTime;
         
         if (CurrentTimer < 0)
         {
             Destroy( ParticleExplosion );
             
             Destroy ( gameObject );
             }
         }
         else
         {
             if(Health <= 0) 
             {
                 Explode(); 
             }
             GameObject tank = GameObject.FindGameObjectWithTag("Player");
             if (tank != null)
             {
                 // work out new facing direction for tank
                 Quaternion rotation = Quaternion.LookRotation(tank.transform.position - transform.position); 
                 // slowly rotate to new facing
                 transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
                 
                 //get distance between enemy and player for when to stop
                 float distance = Vector3.Distance(tank.transform.position, transform.position);
                 
                 if (distance > StopDistance)
                 {
                     transform.Translate (transform.forward * MovementSpeed * Time.deltaTime);
                 }
                 else 
                 {
                     Vector3 offsetForward = -transform.up * 1;
                 Vector3 offsetUp = transform.forward * 0.5f;
                 
                 Vector3 finalPosition = transform.position + offsetForward + offsetUp;
                 
                 GameObject projectile = Instantiate(objectToSpawn, finalPosition, transform.rotation) as GameObject;
                 
                 projectile.rigidbody.AddRelativeForce(projectile.transform.forward * -ProjectileForwardForce, ForceMode.Impulse);
                 projectile.rigidbody.AddRelativeForce(projectile.transform.up * -ProjectileUpForce, ForceMode.Impulse);
                 }
             }
         }
     }
     
     public void Hit(int Damage)
     {
         Health -= Damage;
     }
     /// Explode this instance.
     public void Explode ()
     {
         ParticleExplosion = Instantiate(ExplosionType, transform.position, transform.rotation) as GameObject;
         isAlive = false;
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 04, 2013 at 01:10 PM

A few suggestions. You appear to be firing backwards (I have to guess at your geometry). You are using '-ProjectileFordwardForce' and '-ProjectileUpForce' instead of positive values. In addition, if the mass is set to 1.0, you are applying far too little force (try 500 or 1000). There may be a reason, but I've never seen an independent 'up' force. Usually the gun is aimed, and the projectile is fired with a simple forward force to arch the projectile towards the target. Since I don't see any gun elevation, maybe you need this.

Also calculating 'offsetForward + offsetUp' is unusual (and you appear to be doing backwards though it should not matter given the way you use the values). Usually there is an empty game object placed in front of the gun and made a child of the gun. This empty game object is used for position and often rotation of the projectile, and provides a way in scene view to verify the projectile is shooting from the right place (and not hitting any internal colliders).

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

11 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

Related Questions

GuiButton Going To Next Level 0 Answers

Script not working 0 Answers

Menu script not showing up like normal 1 Answer

Jump Script, no luck getting it to work.. 2 Answers

Script working on some objects but not others 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