Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mehve · Jul 06, 2012 at 09:42 AM · 2dinstantiatequaternionrotateaiming

Cannot instantiate rotation on Prefab

Evening, all.

I am creating a 2D platformer game in which the player shoots in the direction of where the mouse is pointing at. I am trying to set the rotation of an instantiated bullet prefab in the direction of where the mouse is pointing. The bullet will correctly travel at _deltaX in the X direction, and _deltaY in the Y direction, but will NOT be instantiated in the direction that I specify.

This ShootController.cs class is attached to the Player. The Player does have their RigidBody rotations frozen though.

Any advice would be greatly appreciated!

     public class ShootController : MonoBehaviour { 
     
         public GameObject bullet;
      GameObject playerObject;
     
      //Attributes for finding the Angle of Attack
      private float _deltaX;
      private float _deltaY;
      private float _radians;
      private float _degrees;
 
  
  void Start ()
  {
  // Assigns the player position values
  playerPosX = Screen.width  * 0.5f;
  playerPosY = Screen.height * 0.5f - 50.0f;  // This is to properly align the origin
  }
  void Update ()
  {
  // When the PLAYER clicks and Shoots
  if (Input.GetMouseButtonDown(0))
  {
  _deltaX = Input.mousePosition.x - playerPosX;
  _deltaY = Input.mousePosition.y - playerPosY;
  ShootNormalShot();
  }
  }
  void ShootNormalShot(){ 
  _radians = Mathf.Atan(_deltaY/_deltaX);
  _degrees = _radians * Mathf.Rad2Deg;
  
  Vector3 angle = new Vector3(0f,0f, _degrees);
  GameObject projectile = (GameObject)GameObject.Instantiate(bullet, playerObject.transform.position, Quaternion.Euler(angle));
  projectile.GetComponent().Initialize(_deltaX, _deltaY, _normalDamage, _MaxSpeed); 
  }
 } 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xKroniK13x · Jul 06, 2012 at 04:39 PM

Why not something like:

 GameObject projectile = (GameObject)GameObject.Instantiate(bullet, playerObject.transform.position, Quaternion.Euler(_deltaX,_deltaY,degrees));
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 mehve · Jul 06, 2012 at 06:19 PM 0
Share

$$anonymous$$mm. I only want rotation along the Z axis. _deltaX and _deltaY are used to calculate the difference in position from the mouse and the player. That is then converted into degrees which are used to rotate the object.

I tried a variation to your suggestion but it had no effect:

GameObject projectile = (GameObject)GameObject.Instantiate(normalBolt, playerObject.transform.position, Quaternion.Euler(0f,0f,_degrees));

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Quarternion Rotation 1 Answer

How do you instantiate a prefab with a rotation based on the side of a cube you click on? 1 Answer

Instantiate GameObject on GUI Button click and rotate the object. 1 Answer

I'm trying to make it so when I left click, my character's sword slash sprite appears towards the direction of the mouse 0 Answers

How to instantiate on custom rotation? 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