Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Changing the rotation of a 2d projectile sprite somehow changes the direction of the projectile.

Discussion in 'Scripting' started by JeaNiX, Sep 8, 2021.

  1. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    Hello,
    while working on my personal project I've found some strange behavior. I try my best to describe everything. Couldn't find an answer to such a problem, also tried many ways to achieve the same result, but it all worked and ended with this bug.
    I have a "Projectile" object in my scene, that is spawned when the player clicks the mouse.
    1. I have an "InputController" Script that just returns me the current mouse position like this:

    Code (CSharp):
    1.     private Vector3 _mousePosition;
    2.  
    3.     public Vector3 MousePosition
    4.     {
    5.         get
    6.         {
    7.             GetMousePos();
    8.             return _mousePosition;
    9.         }
    10.     }
    11.     private Vector3 GetMousePos()
    12.     {
    13.         _mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    14.         _mousePosition.z = 0;
    15.  
    16.         return _mousePosition;
    17.     }
    2. I have a "BaseProjectile" class which holds information for all my created projectiles. Each projectile is created in the "Hero" script, which is for us not important, and the starting position of the projectile is a Vector3 from a "Scriptable Object" attached to that specific hero.
    3. After that I have a specific projectile script which is used for the current Hero (in my case Wiz).

    Code (CSharp):
    1. public class WizBaseProjectile : BaseProjectile
    2. {
    3.  
    4.     void Start()
    5.     {
    6.         SetUpBaseData();
    7.         GetShootingDir();
    8.     }
    9.  
    10.  
    11.     void Update()
    12.     {
    13.      
    14.     }
    15.     private void FixedUpdate()
    16.     {
    17.         Fly();
    18.     }
    19.  
    20.     protected override void Fly()
    21.     {
    22.         base.Fly();
    23.     }
    4. In the BaseProjectile:
    SetUpBaseDate is just there to initialize all the data from the Scriptable Object and it is called for all projectiles at start, just so it imports the settings.

    Code (CSharp):
    1.     private protected Vector3 GetShootingDir()
    2.     {
    3.         baseShootingDirection = (inputControllerScript.MousePosition - _projectilePosition).normalized;
    4.         return baseShootingDirection;
    5.     }
    As you can see, I get the shooting direction for the projectile after it has spawned, and as a result the projectile goes flying.

    Code (CSharp):
    1.     protected virtual void Fly()
    2.     {
    3.         gameObject.transform.Translate(baseShootingDirection * Speed * Time.deltaTime);
    4.     }
    And this works just fine as it is. But.
    Now the problem begins if I want to change my Projectiles Z rotation. In my case it is a fireball sprite with attached animation. I want it to rotate to the mouse position, so it looks like it goes flying to the mouse. Now it just flies horizontal.

    After research I came up with this solution. I created a function in the base class, and I call it in my Projectile Script.
    Code (CSharp):
    1.     private protected float GetShootingAngle()
    2.     {
    3.         return Mathf.Atan2(baseShootingDirection.y, baseShootingDirection.x) * Mathf.Rad2Deg;
    4.     }
    And it looks like this:
    Code (CSharp):
    1. public class WizBaseProjectile : BaseProjectile
    2. {
    3.  
    4.     void Start()
    5.     {
    6.         SetUpBaseData();
    7.         GetShootingDir();
    8.         transform.rotation = Quaternion.AngleAxis(GetShootingAngle(), Vector3.forward);
    9. }
    And the problem is, it really changes the Z axis of the rotation, but now the projectile isn't hitting the mouse, it just flies random a little away from the mouse. It's not -90 degree or smth. Also tried many ways to fix this, call this earlier or later, or make a new reading of the position. Tried to not use .normalized and also tried to do everything in the projectile script not in the base script. Normally I would do all int the base script and change the rotation there, and just call the function if I need it in the projectile script, so I don't repeat myself. But I tried different ways. It's something I can't figure out alone. It just changes the direction of the projectile, not just the rotation.
    Maybe someone could have an idea why is this happening, how can that be connected.
    Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You should read the documentation for Transform.Translate().

    It explains precisely why, and it also supports an overload to define
    Space
    if you like.
     
    JeaNiX likes this.
  3. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    There is one big lesson you taught me with your answer. Before asking and searching for answers on Google, read the documentation! I was sure that it was not connected at all, but just by reading the documentation, I immediately understood what the problem was, and it immediately worked. I am very grateful to you for this lesson, and in the future I will try to use the documentation more often! This was more than an answer to my question. I wish you a great time and thank you! ^_^
     
    gjaccieczo and Kurt-Dekker like this.
  4. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    That's nothing! Sometimes you find the solution to your issue right when you finish typing your post out and realize what the exact problem is :D.