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 Icezman · Jan 10, 2018 at 10:45 AM · instantiatevector3transform.positiontransform.rotation

The instantiated prefab doesn't fire correctly from the player

Hello!

I've been breaking my head on fixing this considering I'm pretty sure it's a very easy fix. (complete beginner here). I have the FireMissile method instantiating my missile prefab but the issue is that the missile always starts 2f above the transform.position. I'm thinking it has to do with world or local but don't have any idea how to change that.(I tried changing it to transform.localPosition and transform.localRotation but no dice).

Even when it's horizontal it shoots from 2f above the middle of the gameObject (so in the middle section, which looks weird).

Any help would be greatly appreciated!

     private void FireMissile()
     { 
         if (Input.GetKeyDown(KeyCode.F))
         {
             Vector3 firePoint = new Vector3(0f, 2f, 0f);
             Instantiate(rocketProjectile, transform.position + firePoint, transform.rotation);
        }
     }

alt text

failed-missile-launch.jpg (35.2 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
Best Answer

Answer by Icezman · Jan 10, 2018 at 12:32 PM

For anyone in the future looking:

(thanks to @Legend_Bacon putting me on the right track!)

I fixed it by making a new (empty) GameObject in my scene (which is a child of my player prefab) and creating a public GameObject firePoint in the player script. I put the empty firePoint object through the inspector on the firepoint reference slot.

I then changed my transform.position and transform.rotation to firePoint.transform.position and firePoint.transform.rotation.

     private void FireMissile()
     { 
         if (Input.GetKeyDown(KeyCode.F))
         {
             Instantiate(rocketProjectile, firePoint.transform.position, firePoint.transform.rotation);
        }
     }

As I'm a beginner, I have no idea if this is a viable way to do this but it worked for me!

alt text


missile-succes.jpg (109.7 kB)
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
1

Answer by Legend_Bacon · Jan 10, 2018 at 11:31 AM

Hello there,

Sorry if I'm misunderstanding this, but if your problem is that the missile get instantiated 2f above the position of the gun, that's because your code is telling you to do it.

You're not instantiating the missile at the gun's position, you're instantiating it at the gun's position + firepoint, which is defined just above as having a Y of 2f. Change that 2 to 0, or remove "+ firePoint", and the missile will then get instantiated at the exact position of the gun.

Also just in case, you might want to check the pivot of the missile. If it isn't centered, then that offset will affect the position it gets instantiated at. You can do that by clicking on a missile and changing between "center" and "pivot" in the top left corner.

Hope that helps!

Best,

~LegendBacon

Comment
Add comment · Show 1 · 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 Icezman · Jan 10, 2018 at 12:26 PM 0
Share

Thanks for the feedback! Thanks to your post I saw it completely different and I have fixed it!

I know the problem was the origin of my 'fire point' which was on my player itself. I added an empty gameobject on my player and instantiated it from that firePoint.transform.position, firePoint.transform.position.

I have no idea if that is a viable way or not or if there's a better way but it worked :D!

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

transform.position on Instaniated object does not match inspector 0 Answers

How to carry over variable (transform.position) and use it as Vector3 for instanitating a prefab 2 Answers

Updating transform.position correctly to instantiated GameObjects 0 Answers

Instantiate object in front of player? 2 Answers

I'm trying to Instantiate the collided object. 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