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 DiscoFever · Nov 04, 2015 at 03:35 PM · 2d gameangleeulerangles

2D Projectile angle problem with angle

alt text

I'm having a 'quite' troubling issue with my 2D topdown shooting game.

targetPosition is the target position (obviously)

The projectile is a simple sprite; but when firing which fires in the x-y plane but the sprite is 90° rotated on the Y axis. For now Bullet is a RigidBody (3D) not 2D

What am I doing wrong here ?

UPDATE: code

         /// <summary>
         /// Does the actual attack. 
         /// </summary>
         /// <param name="targetPosition">The position to attack.</param>
         public void Attack(Vector3 targetPosition)
         {
             GameObject.Instantiate(bullet, transform.position, Quaternion.LookRotation(targetPosition - transform.position));
             lastAttackTime = Time.time;
         }


Bullet Script

 Using UnityEngine;
 
   
     public class Bullet : MonoBehaviour
     {
         // The speed of the bullet
         public float speed;
         // The amount of damage the bullet does
         public float damageAmount = 5;
         // Destroy itself after this amount of time
         public float selfDestructTime = 5;
 
         private Rigidbody m_Rigidbody;
         private Transform m_Transform;
 
         /// <summary>
         /// Cache the component references and initialize the default values.
         /// </summary>
         private void Awake()
         {
             m_Rigidbody = GetComponent<Rigidbody>();
             m_Transform = transform;
             Invoke("SelfDestruct", selfDestructTime);
         }
 
         /// <summary>
         /// Move in the forward direction.
         /// </summary>
         void Update()
         {
             m_Rigidbody.MovePosition( m_Rigidbody.position + speed * m_Transform.forward * Time.deltaTime);
         }
 
         /// <summary>
         /// Perform any damage to the collided object and destroy itself.
         /// </summary>
         /// <param name="collision"></param>
         private void OnCollisionEnter(Collision collision)
         {
             IDamageable damageable;
             if ((damageable = collision.gameObject.GetComponent(typeof(IDamageable)) as IDamageable) != null) {
                 damageable.Damage(damageAmount);
                 Destroy(gameObject);
             }
         }
 
         /// <summary>
         /// Destroy itself.
         /// </summary>
         private void SelfDestruct()
         {
             Destroy(gameObject);
         }
     }
  


capture-decran-2015-11-04-a-184132.png (109.8 kB)
Comment
Add comment · Show 10
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 CosmoM · Nov 04, 2015 at 03:55 PM 0
Share

Is the bullet sprite by default aligned with the x-axis, or with the y-axis? Your code is assu$$anonymous$$g it's with the x-axis, as you calculate the angle relative to it.

avatar image DiscoFever CosmoM · Nov 04, 2015 at 04:00 PM 0
Share

What you mean 'aligned' ? The sprite object is with rotation (0, 0, 0)

avatar image CosmoM · Nov 04, 2015 at 04:04 PM 0
Share

What I mean is, if you instantiate the object without any rotation (or just drag the prefab bullet into the scene), which direction is it pointing towards? It should like you fired it along the x-axis; if not, that's the reason for your 90 degree offset.

avatar image DiscoFever CosmoM · Nov 04, 2015 at 04:10 PM 0
Share

Ohh ok i see; yes it's aligned with the X-axis. But i think the main problem is with the update function which don't work in 2D; if i'm not mistaken. The bullet is being instantiated but it doesn't move toward the target.

avatar image CosmoM DiscoFever · Nov 04, 2015 at 04:19 PM 0
Share

As far as I know it should work. Is your game in 2D mode, or 3D mode but in the xy-plane only? I'm assu$$anonymous$$g m_Rigidbody etc are 2D objects…?

Show more comments
avatar image Nexgea · Nov 04, 2015 at 04:44 PM 0
Share

$$anonymous$$aybe you could rotate the source image of your sprite by 90 degrees. To counter it.

avatar image DiscoFever Nexgea · Nov 04, 2015 at 04:53 PM 0
Share

I did; the problem is not with the sprite it's with the rotation that is wrong.

avatar image CosmoM · Nov 04, 2015 at 05:41 PM 0
Share

Could you post a screenshot showing how the bullet looks when fired compared to how it should look? $$anonymous$$aybe I'm misunderstanding the rotation.

avatar image DiscoFever CosmoM · Nov 04, 2015 at 05:43 PM 0
Share

Just did. The original code was for 3D and i'm trying to flip 2D

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by CosmoM · Nov 04, 2015 at 06:53 PM

I'm not sure why the object apparently instantiates in the wrong plane, but you can fix it by changing the y-angle in your call to Instantiate to 90f (or -90f).

Comment
Add comment · Show 4 · 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 DiscoFever · Nov 04, 2015 at 07:56 PM 0
Share

Can you provide an example; i don't understand your 'y-angle' thing.

avatar image CosmoM DiscoFever · Nov 04, 2015 at 08:01 PM 0
Share

Change:

GameObject.Instantiate(bullet, transform.position, Quaternion.LookRotation(targetPosition - transform.position));

to:

GameObject.Instantiate(bullet, transform.position, Quaternion.Euler(0f,90f,rotz));

where rotz is as you defined it before you edited your post and removed it. I believe you had:

Vector3 diff=targetPosition-transform.position; float rotz=$$anonymous$$athf.Atan2(diff.y,diff.x)*$$anonymous$$athf.Rad2Deg;

avatar image DiscoFever CosmoM · Nov 04, 2015 at 09:00 PM 0
Share

Ok that definitely improved the sprite rotation; but now it's the Update function that is wrong somehow transform.forward is Vector.Zero ...

I've tried to calculate Direction from the transform.rotation.z :

 float angle = m_Transform.rotation.z;
 Debug.Log("angle: " + angle);
 direction =  new Vector2((float)$$anonymous$$athf.Cos(angle), -(float)$$anonymous$$athf.Sin(angle));
 m_Rigidbody.$$anonymous$$ovePosition( (Vector2) m_Rigidbody.position + speed * (Vector2) direction * Time.deltaTime);

But again messed up :(

avatar image DiscoFever DiscoFever · Nov 04, 2015 at 09:02 PM 0
Share

Ok never $$anonymous$$d it was all about transform.right in 2D ... :) Thanks !

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

Scrolling texture UV background based on player rotation 1 Answer

Aligning/mapping angles of Input.GetAxis(with .localEulerAngles 0 Answers

Limit local rotation 6 Answers

Limit rotation angle on Z axis while slerp 1 Answer

Euler angles have a dead zone near 90? 2 Answers


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