Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiated object randomly changing direction

Discussion in 'Scripting' started by Berthil, Mar 18, 2020.

  1. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    22
    Hello!

    Im having a problem with an instantiated object which randomly changes its direction. Its from a brick breaker game. A power up makes the player release shots upward to hit the bricks.

    Sometimes the shot goes downwards. The shots are instantiated objects which goes upward using the following code (projectile speed is 0.4):


    Code (CSharp):
    1. void Update()
    2.     {
    3.         transform.Translate(0, projectileSpeed, 0);
    4.     }
    The objects are instantiated using the following code (timeBtweenShots is 0.5):

    Code (CSharp):
    1. Enumerator ShotsCoroutine()
    2.     {
    3.         yield return new WaitForSeconds(timeBtweenShots);
    4.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);
    5.         yield return new WaitForSeconds(timeBtweenShots);
    6.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);
    7.         yield return new WaitForSeconds(timeBtweenShots);
    8.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);
    9.         yield return new WaitForSeconds(timeBtweenShots);
    10.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);
    11.         yield return new WaitForSeconds(timeBtweenShots);
    12.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);
    13.         yield return new WaitForSeconds(timeBtweenShots);
    14.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);
    15.         yield return new WaitForSeconds(timeBtweenShots);
    16.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);
    17.         yield return new WaitForSeconds(timeBtweenShots);
    18.         Instantiate(bulletsPrefab, transform.position, bulletsPrefab.transform.rotation);

    So the player release 8 shots. Sometimes, one or 2 of them goes downwards.

    brick braker.png

    Would apreciate any help.

    Thanks!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    When you spawn the bullets, you only tell it where to spawn what in which orientation. If the bullets go backwards, that should be caused by the movement of the bullet itself. So i'd assume projectileSpeed sometimes is negative. Use Debug.Log() to print projectileSpeed and see if it sometimes changes from positive to negative. Are you influencing projectileSpeed at all or should it be constant?

    Also, please use a loop if you have so much repetitive code.

    Edit: Or the rotation in the prefab changes. Are you editing the prefab at runtime? Cant you just use Quaternion.identity?
    With the last parameter of Instantiate() you are telling your bullet the orientation it should spawn in. Unless bulletsPrefab.transform.rotation is already Quaternion.identity, i dont think you want to rotate it there (since the prefab itself contained a rotation then, and would again be rotated by the same amount on spawn). If anything you want the bullet to face forwards based on the spaceship.
     
    Last edited: Mar 18, 2020
    Joe-Censored likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Do the bullets have (non-kinematic) physics on them? They could be colliding with something on spawn and getting bounced.

    If not that, pause when they shoot downward and select the object, and check their rotation - see if they got rotated around 180 degrees somehow.
     
    Joe-Censored likes this.
  4. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    22
    Thanks for your reply!!

    I checked the rigidbody Velocity and the shots going downwards have negative value.


    I'm assuming it is constant. The only value I use is the "projectileSpeed".

    Yes, thanks for the advice. I just did it. Its my first game and im learning a lot.

    I noticed thwt when I unchecked the freeze rotation boxes in the rigidbody it rotates in multiple directions. I don't know where this information come from. I will read about Quaternion.

    Yes I noticed that if I check “is Trigger” it never goes downward. Looks like collision is changing the rigidbody Y velocity to a negative value.

    I will try to use collision layers to avoid its collision and try to remove its rotation (maybe checking the quaternion).

    Thanks again for your reply!!
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Just to avoid some unnecessary confusion:
    Quaternions are four dimensional complex numbers, mainly used in two fields. They are used to describe the rotation of a point in 3d space in computer science, as well as in quantum mechanics. The latter should give you a hint that this may not be an easy to comprehend topic. Looking into it is, of course, fine. If you want to try and understand them, the youtuber 3Blue1Brown did a couple nice videos on them. However, be aware that you do not have to understand Quaternions in order to use them. You can create Quaternions from Euler Angles or vice versa, multiply Quaternions to apply rotations and so on. Take a look at what's possible here:
    https://docs.unity3d.com/ScriptReference/Quaternion.html

    Be aware that the 4 (!) values saved in a Quaternion do not in 'any' way represent the rotations on the x-, y- or z-axis as, for example, seen in the inspector (which are the euler values). The x-, y-, z- and w-value of a Quaternion instead each represent a value between 0 and 1. Most common math rules also do not apply for Quaternion values.
    Unity internally uses Quaternions for pretty much everything. The two main reasons for this are performance and gimbal lock prevention. The one exception to this rule is the inspector, where it actually saves euler values in order to let you adjust them more easily.

    Since Transform.rotation is also saved as a Quaternion, a lot of beginners eventually run into the problem where they read (or try to set) these values and are confused as for why it does not work as expected. If you ever are in this situation, you most likely intend to convert either Quaternion values to Euler, or Euler to Quaternion.

    Hope this saves you a bit of trouble in the future :p
     
  6. Berthil

    Berthil

    Joined:
    Aug 26, 2013
    Posts:
    22
    Hi Yoreki.

    Thans for the advice, and for the link. I have read about quaternion in Unity some time ago and I remember it was hard to use. In that time i was doing a robotic arm simulator and used it for a sequence of 3 joints. I made it work and never used quaternion again.

    I will check 3Blue1Brown video. It's always good to learn stuff.