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 BearDogGaming · Oct 19, 2015 at 11:36 PM · instantiatespawnyaxis

Instantiated Object Spawning in different position

I am trying to spawn a fireball whenever the 'F' key is pressed, but every time I press the F key, the fireball spawns at a different place on the Y-axis. Any thoughts? Player Attack Script

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAttack : MonoBehaviour {
     public float attackTimer;
     public float coolDown;
     public float damage;
     public Rigidbody projectile;
     public float speed = 0.1f;
     Rigidbody fireball;
     public Transform fireBallSpawn;
 
     void Start () {
         attackTimer = 1;
         coolDown = 0.5f;
         damage = 5.0f;
     }
     
     void Update () {
         if (attackTimer > 0) {
             attackTimer -= Time.deltaTime;
         }
         if (attackTimer < 0) {
             attackTimer = 0;
         }
         if (Input.GetKeyUp(KeyCode.F))
         {
             if (attackTimer == 0) {
                 Attack();
                 attackTimer = coolDown;
             }
         }
     }
 
     private void Attack() {
         fireball = Instantiate(projectile, fireBallSpawn.position, fireBallSpawn.rotation) as Rigidbody;
         fireball.velocity += transform.forward * 5;
         Destroy(fireball.gameObject, 2.0f);
     }
 }

alt text

alt text

At the bottom of the screen, the console is printing the y-axis for the spawner.

screenshot-2.png (369.1 kB)
screenshot-1.png (437.6 kB)
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

Answer by Eno-Khaon · Oct 20, 2015 at 12:00 AM

Nothing here appears to clearly suggest that the fireballs would be created in different locations each time, so my main two questions are:

1 - Does the fireBallSpawn position ever change in a way that's reflected by what you're experiencing

2 - Are the fireballs' colliders inside anything else when they're created and, if so, is that shoving them out quickly to another position immediately after spawning?

If it's neither of those, then I don't believe it would be feasible to provide a more accurate answer with the current information available.

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 BearDogGaming · Oct 20, 2015 at 12:58 AM 0
Share

It does seem to start reflecting a certain point on the y-axis after a couple shots. Any suggestions on how to fix it.

avatar image BearDogGaming · Oct 20, 2015 at 03:47 AM 0
Share

So I put a debug.log and I logged the position for the spawner on the x, y, and z axes and for some reason, the y value keeps changing in between about -1 and 1. I have no idea why this is happening. Any thoughts?

avatar image Eno-Khaon BearDogGaming · Oct 20, 2015 at 04:11 AM 0
Share

I'm afraid I have to grasp at straws a little bit. There are too many possibilities that I would have no idea about without more information (like pictures of hierarchy). So for now, I'll pester you for more information and offer food for thought through them.

Assu$$anonymous$$g you're launching the fireballs from your character or the like, what is the "fireBallSpawn" attached to as a child, if anything?

If the "fireBallSpawn" is a child of another object, does its localposition ever change?

For that matter, have your tests included looking around much, or is this facing a single direction?

How many objects, if there's a hierarchy, have colliders and/or rigidbodies?

Are these colliders interpenetrating? If so, is that shoving things apart which shouldn't be?

Are there any other scripts in your scene which might be influencing the motion of the spawner? For example, do any other scripts look for the spawner at all?

avatar image
0

Answer by TheLazyTurtle9 · Oct 20, 2015 at 11:58 AM

I'm not completely sure as to what you mean, but you could try:

     fireball = Instantiate(projectile, fireBallSpawn.position, fireBallSpawn.rotation) as Rigidbody;
     fireball.AddForce (fireBallSpawn.forward * 5);

             Destroy(fireball.gameObject, 2.0f);

the major thing that I changed was now it's working off the fireBallSpawn position instead of the transfom that may or may not be changing. the 5 in the AddForce part can be changed to velocity as well, but for Instantiate I prefer to use AddForce

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiated Object spawning at wrong position 3 Answers

Spawning objects on trigger does not work as expected 2 Answers

Having Trouble with Instantiating an object on an axis 2 Answers

Spawn: Instantiate GameObject 1 Answer

Y value wont work? 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