• 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 /
  • Help Room /
avatar image
Question by SilvanK · Aug 03, 2017 at 08:10 PM · rotationinstantiatenot workingangleangles

Instantiating Projectiles not working at certain directions??

Hey, I'm new to Unity and C#, and I am creating a simple game for my application. Sorry if some things sound confusing, Im trying my best to explain my problem. I have 2 players moving in a certain direction. I then coded a script attached to a Bullet Emitter (null object) that follows the player and rotates in the direction the players is walking. From this emitter, the player can fire bullets when he presses "2". The emitter then instantiates a bullet prefab and fires it in the direction of the blue axis of the emitter (ergo the direction the player is walking to, because the emitters script). This works fine in every direction, but when I combine directions (like pos. horizontal and neg. vertical), things go crazy. The bullets spawn in most directions, but in some, bullet copies aren't even instantiated. More detailed: firing bullets in following directions works: walking in + / - horizontal , + / - vertical, + horizontal + / + vertical , -horizontal - vertical. +horizontal -vertical doesnt work

Here are the scripts that might be relevant:

Bullet Emitter

 GameObject Temporary_Bullet_Handler;
 Temporary_Bullet_Handler = Instantiate(Bullet, Emitter.transform.position, Emitter.transform.rotation) as GameObject;
 Rigidbody Temporary_RigidBody;
 Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
 Temporary_RigidBody.AddForce(transform.forward * BulletSpeed);
 Destroy(Temporary_Bullet_Handler, 3.0F);

Bullet Emitter Repositioner

 public GameObject obj;
     public Vector3 offset;
     public int player;
     private Vector3 xOffset = new Vector3 (0, 0, 0);
     private Vector3 zOffset = new Vector3 (0, 0, 0);
 
 
     void Start () {
         offset = obj.transform.position - this.transform.position;
     }
     
     // Update is called once per frame
     void Update () {
 
         float horizontal = Input.GetAxis("Horizontal" + player);
         float vertical = Input.GetAxis("Vertical" + player);
 
         if (horizontal < 0) {
             xOffset = new Vector3(-2.5F, 0, 0);
         }
         else if (horizontal > 0) {
             xOffset = new Vector3(2.5F, 0, 0);
         }
         else if (horizontal == 0) {
             xOffset = new Vector3(0, 0, 0);
         }
 
         if (vertical < 0) {
             zOffset = new Vector3(0, 0, -2.5F);
         }
         else if (vertical > 0) {
             zOffset = new Vector3(0, 0, 2.5F);
         }
         else if (vertical == 0) {
             zOffset = new Vector3(0, 0, 0);
         }
 
         if (horizontal == 0 && vertical == 0) {
             zOffset = new Vector3(0, 0, 2.5F);
         }
 
         this.transform.position = obj.transform.position - offset + xOffset + zOffset;
         this.transform.forward = obj.GetComponent<Rigidbody> ().velocity;
     }

Hope you can help me!

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by SilvanK · Aug 04, 2017 at 11:11 PM

I FOUND THE PROBLEM! It is not the code or any bug with unity, it is the mousepad! Just google "mousepad ghsoting" and you will see what I mean. Basically, it means that some combinations of keys aren't registered due to technical reasons. The more keys, the more likely the combination is to not work. In this case, down + right + numpad2 couldnt be registered (but other arrows + numpad2 did, strangely). Hope there is any fix to the hardware, but that's not of this forum's buisness, I guess...

Comment

People who like this

0 Show 0 · 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

Answer by Darksorcerer57 · Aug 03, 2017 at 10:49 PM

For the x and z offsets, if you're trying to move it along the x and z axis respectively, then simply adding those values to the Vector3 won't work, you have to add a Vector3 with those values in their respective places to its position or use the translate function on its transform, again with those values in their respective places.

Comment

People who like this

0 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 SilvanK · Aug 04, 2017 at 10:24 AM 0
Share

Hey, thank you for your answer. I still didnt quite get it, Im afraid ^^ Just by the way, moving the emitter to the right spot and rotating it correctly works perfectly, I tested it in play mode and watched the emitter in scene mode. There was nothing wrong with the offsets, rather with the bullet instantiation. Moving in pos. Horizontal and neg. Vertical hinders bullets from spawning, somehow. Every other direction works. I also found out that this is only the case when I am holding the right and down arrow, when I press 2 shortly after I release these buttons (but the character still moves in that direction), bullets do instantiate. Strange problem I know, but maybe someone knows the answer ^^

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

146 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 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

Copy Roll From one Gameobject to another Without other rotations effecting it 0 Answers

Instantiate is not working in unity 5.6.4f1 0 Answers

Instantiated object does not inherit rotation. 1 Answer

Set Instanatiated Object's Rotation Towards Player 1 Answer

change angle towards direction 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