• 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
Question by Latedi · Sep 09, 2012 at 09:36 PM · rotationinstantiateprojectile

Instantiate projectile rotation

So yeah t$$anonymous$$s seems to be a common problem and I haven't gotten any wiser after a few hours of trying t$$anonymous$$ngs out. The fireballs fly in all the wrong directions and I have no idea why :)

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Create fire ball.
 /// 
 /// T$$anonymous$$s script is attached to the player and instantiates a fireball when "Attack" is pressed.
 /// </summary>
 
 public class CreateFireBall : MonoBehaviour {
     
     public GameObject fireBall;
     Transform playerTransform;
     Vector3 firePosition;
     Quaternion playerRotation;
 
     // Use t$$anonymous$$s for initialization
     void Start ()
     {
         //if(networkView.isMine == true)
         {
             playerTransform = transform;
         }
         //else
         {
             //enabled = false;
         }
     }
     
     // Update is called once per frame
     void Update ()
     {
         //mouse 0
         if(Input.GetButtonDown("Attack"))
         {
             //In front of the player?
             firePosition = playerTransform.TransformPoint(0,0,0.2f);
             //what the f---?
             playerRotation = Quaternion.Euler(playerTransform.eulerAngles.x + 90f, playerTransform.eulerAngles.y, playerTransform.eulerAngles.z);
             
             //call the function to instantiate the fireball
             fire (firePosition, playerRotation);
             //networkView.RPC("fire", RPCMode.All, firePosition, playerRotation);
         }
     }
     
     //[RPC]
     void fire(Vector3 position, Quaternion rotation)
     {
         Instantiate(fireBall, position, rotation);
     }
 }
Comment

People who like this

0 Show 1
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 Latedi · Sep 11, 2012 at 11:41 PM 0
Share

Ok I did some more debugging. The projectile flies perfectly straight when I aim at 0, 0 (x and y). However as soon as I aim somewhere else the rotation gets random. A script prints the rotation of the player, the camera and the fireball the moment it is shot and they're exactly what they should be. X rotation of the player/camera, Y rotation of the camera and Z rotation = 0. If i pause the game asap the rotation is exactly what it should be but the Z is off by like 1.23457e-08 so it shouldn't even matter. The fireball is set to fly at transform.Translate(transform.forward speed Time.deltaTime); every update(). The prefab of the fireball and the player both have their rotations set at 0, 0, 0.

So yeah.... Could anyone help me? :| I'll even upload my project if that's necessary.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Latedi · Sep 11, 2012 at 11:43 PM

Oh my god I fixed it. I added Space.World to the projectile's script like t$$anonymous$$s transform.Translate(transform.forward speed Time.deltaTime, Space.World); I've been debugging t$$anonymous$$s for like 12 hours I'm amazed I didn't kill anyone yet :o Is it always like t$$anonymous$$s when running into problems? Even after you have some/a lot of experience in Unity?

Comment
asanyc

People who like this

1 Show 2 · 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 DesiQ · Sep 12, 2012 at 03:29 AM 0
Share

As you get more experience in Unity and get to know its systems, debugging will become easier and faster (for known problems ;). Much the same as programming with any other library.

avatar image Latedi · Sep 12, 2012 at 10:15 AM 0
Share

I sure hope so :d

avatar image

Answer by DesiQ · Sep 09, 2012 at 09:43 PM

It's bizarre that you're messing with the player's rotation like that. What view is the game being played from? If first person, just use camera.transform.forward. If top-down, use the player's forward vector.

Comment

People who like this

0 Show 7 · 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 Latedi · Sep 09, 2012 at 09:51 PM 0
Share

Hmm it's third person. Just using the transform.rotation doesn't cut it.

Edit: and yeah, the playerRotation = ... there is just what I was currently trying.

avatar image DesiQ · Sep 09, 2012 at 09:53 PM 0
Share

If it's third-person, then surely you're still using the camera to aim, and camera.transform.forward will cut it?

avatar image Latedi · Sep 09, 2012 at 09:57 PM 0
Share

Well if I use the camera transform, wouldn't that make my fireballs fly in the wrong direction anyway? The camera is slightly angled downwards.

Edit: Uuhm it looks like about as random as previously. The camera is attached to the player and is in a fixed position behind the player's back.

avatar image kristercollin · Sep 09, 2012 at 10:17 PM 0
Share

Along the same vein as DesiQ, why can't you use Quaternion.identity to have the rotation default to the gameobject's in world rotation (which will give you the unadulterated value for where it's facing)?

Edit: Disregard, I can't believe I said that. I'm going to get some sleep, now.

avatar image Latedi · Sep 09, 2012 at 10:21 PM 0
Share

Well it's a projectile and I want it to fly the same way as the player is aiming. If i use Quaternion.Identity all projectiles will fly in the same direction regardless of where the player is aiming.

Show more comments

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

10 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

Related Questions

Gun Projectile Shooting In Wrong Direction (Javascript) 1 Answer

Fire Projectile based on Model rotation 1 Answer

Objects Instantiating at wrong position 3 Answers

Rotation troubles 2 Answers

Instantiated object using parent's transform as world axes 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