• 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 ZeroSennin · Mar 16, 2014 at 12:49 AM · projectilefiring

Projectile firing at wrong height

I've been trying to get an enemy to fire a projectile from a spawn point, and while it's firing in the right direction and at the right speed, the height is way off and I don't know what's causing the problem.

Here's a picture of what's going wrong as well as some code. Note that GameObject SpawnPoint has its value assigned via the Inspector panel.

alt text

 using UnityEngine;
 using System.Collections;
 
 public class RedNinjaCat : MonoBehaviour 
 {
     //create a timer that determines how often an arrow is fired
     int arrowTimer = 0;
     int health = 1;
     
     public Transform ShurikenDrop;
     public Transform KunaiDrop;
     public Transform MouseDrop;
     public Transform CatnipDrop;
     public Transform FoodDrop;
     public Transform FishDrop;
     
     public Transform ArrowShoot;
     public GameObject SpawnPoint;
 
     // Use this for initialization
     void Start () 
     {
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         arrowTimer++; //increase the timer
 
         if (arrowTimer == 120) // Every 2 seconds (120 frames)
         {
             arrowTimer = 0; //reset the timer
             Transform instanceBullet = (Transform)Instantiate(ArrowShoot, 
                 SpawnPoint.transform.position, SpawnPoint.transform.rotation); //Create the arrow
         }
         
         if (health <= 0) {
             enemyDie ();
         }
     }
why.png (23.3 kB)
Comment
Add comment · Show 2
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 tobyfee · Mar 16, 2014 at 12:59 AM 0
Share

Best way to figure this out: start the game, wait for an arrow to fire, then pause the game, click on the 'arrow' and then the 'spawnpoint' gameobjects and check their positions. I'll bet you'll find that the spawnpoint's position isn't where you expect it to be. Sidenote: to prevent small errors from causing your arrowTimer to go haywire, change it to if (arrowTimer >= 120)

avatar image ZeroSennin · Mar 16, 2014 at 01:39 AM 0
Share

As it turns out, messing with the scale of the arrow (it was imported from my partner's Unity and he wasn't working on the same size scale as me) fixed part of it, but it's still firing weirdly. Using your advice to check and see what might be happening.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by scipiothegreat · Mar 16, 2014 at 01:23 AM

That should put the arrow exactly where the spawn point is. I think you should check and make sure the spawnpoint is only moving where you want it to move to.

Also you should consider using Time.deltaTime to make the arrow timer frame rate independent. Right now, if the FPS drops to, say, 30 FPS, the timer will take 4 seconds, not 2.

Comment
Add comment · Show 3 · 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 ZeroSennin · Mar 16, 2014 at 01:42 AM 0
Share

What would the deltatime expression look like? I know what it does functionally speaking but I'm not sure how to get it to translate into the interval of time that I need it to be.

As for the spawnpoint, it's been fixed in the same spot the whole time so I don't know why it's not working. I figured out a quick cheat by moving the arrow's position within the Prefab that's storing it in addition to tweaking the scale, but it's not perfect.

avatar image scipiothegreat · Mar 16, 2014 at 02:03 AM 0
Share

If you change arrowTimer to a float you can use

 void Update(){
     arrowTimer += Time.deltaTime;
 
     if (arrowTimer >= 2f){
         //do something
     }
 }

deltaTime is measured in seconds and is affected by Time.timeScale.

As for the arrow, everything looks fine to me. Are you sure there isn't a component on the arrow GameObject that tries to change the position? Is the arrow's scale set to (1, 1, 1)?

avatar image ZeroSennin · Mar 16, 2014 at 02:44 AM 0
Share

It was imported from my partner's unity file and he was working at a different scale than I was (long story). The prefab package I got from him generated objects at a stupidly huge size so I had to scale it down.

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

22 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

Related Questions

Firing only a single projectile at a time, then adding a second. 2 Answers

How to avoid speed change in bullets while moving? 1 Answer

How to make a projectile fire at a moving player. 3 Answers

Projectile Hit Detection with Bullet Drop 1 Answer

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 2 Answers


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