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. Dismiss Notice

Shooter game bullet doesn't translate by currect direction with currect Unit Vector component set

Discussion in 'Scripting' started by 769270865, Dec 14, 2016.

  1. 769270865

    769270865

    Joined:
    Feb 10, 2016
    Posts:
    23
    Code (CSharp):
    1. public class NonSpellFairyControl : MonoBehaviour {
    2.  
    3.     public float Speed;
    4.     public GameObject FairyBullet;
    5.     Vector2 topRightConner = new Vector2(10.2f, 4.9f);
    6.     Vector2 bottomLeftConner = new Vector2(-10, -4.9f);
    7.     float[] BulletPath = new float[] { 22.5f, 45.0f, 67.5f, 90f, 112.5f, 135f, 157.5f, 180f, 202.5f, 225f, 247.5f, 270f, 292.5f, 315f, 337.5f, 360f};
    8.     public static float BulletDirection;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         GameObject boss = GameObject.Find("Alice");
    13.         if (gameObject.transform.position.x < boss.transform.position.x)
    14.         {
    15.             Speed = -Speed;
    16.         }
    17.         InvokeRepeating("ShootBullet", 0.1f, 1f);
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.         gameObject.transform.Translate(Speed * Time.deltaTime, 0, 0);
    24.        
    25.     }
    26.     void ShootBullet()
    27.     {
    28.      
    29.         GameObject Bullet1 = Instantiate(FairyBullet);
    30.        
    31.         BulletDirection = BulletPath[0];
    32.         Bullet1.transform.Rotate(0, 0, DeterMineBulletRotation(BulletPath[0]));
    33.        
    34.      
    35.     }
    36.     float DeterMineBulletRotation(float TravelDirectionDegree)
    37.     {
    38.         // When Rotate respect to Z asxis 0 degrees is corsponding to 90 degrees in normal sense, and 0 degrees in UnitCircle is -90 in this case
    39.         float rotationDegres;
    40.         rotationDegres = -90f + TravelDirectionDegree;
    41.         return rotationDegres;
    42.  
    43.     }
    44. }
    So I set the static field BulletDirection to 22.5 in this case in the spawner, the bullet does get the 22.5 from this script and it does show 22.5 everytime in Debug.log but instead translate in 22.5degree postive it trnslate 22.5 degrees negative.
    Here is the code in the bullet script
    Code (CSharp):
    1. public class NonSpellFairyBulletControl : MonoBehaviour {
    2.  
    3.     public float Speed;
    4.     float BulletPath;
    5.     Vector3 UnitVector;
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.         BulletPath = NonSpellFairyControl.BulletDirection;
    10.         Debug.Log(BulletPath);
    11.         BulletPath = Mathf.Deg2Rad * BulletPath;
    12.         UnitVector = new Vector3(Mathf.Cos( BulletPath), Mathf.Sin(BulletPath) , 0);
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         gameObject.transform.Translate(Speed * Time.deltaTime * UnitVector.x, Speed * Time.deltaTime * UnitVector.y , 0);
    19.                                      
    20.     }
    21. }

    QQ截图20161214032221.jpg
    This is the scene while the game play, why the bullet doesn't translate on 22.5degree postive(Green Arrow) path?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    When you call NonSpellFairyControl, you are basically calling the base script, if you want to do this, either do:
    GameObject.FindGameObjectWithTag("Fairy").GetComponent<NonSpellFairyControl>().BulletDirection;
    or if you want a faster method, set the value of the Bulletpath in the script that instantiates these bullets
     
  3. vinay-2477

    vinay-2477

    Joined:
    Mar 25, 2016
    Posts:
    7
    Lerp the bullet in forward direction like Vector3.Lerp(fromPos,toPos,Time.time*vector3.forward*speed);
     
  4. 769270865

    769270865

    Joined:
    Feb 10, 2016
    Posts:
    23
    Hi, I think there is something more to it, when I comment out the rotation part of the script, line 32 in NonSpellFairyControl
    the bullet does translate in postive 22.5 degrees, so the rotation part seems also interfere with the translation. The speed is set to positive in the inspector. Maybe this is not the right way to set an object's rotation to a certain degrees respect to an axis?
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It's probably because Translate uses local space by default. Try this instead:

    Code (csharp):
    1.  
    2. gameObject.transform.position += UnitVector * Speed * Time.deltaTime;
    3.  
     
  6. 769270865

    769270865

    Joined:
    Feb 10, 2016
    Posts:
    23
    Thank you that did it, so when it translate respect to local space, the axis is arealy rotated, translate would translate add on to the rotation it arealy has is that what happened?