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

Change Object's Rotation Based on Rotation of a Prefab when Instantiated?

Discussion in 'Prefabs' started by Tyrant7, May 7, 2020.

  1. Tyrant7

    Tyrant7

    Joined:
    Aug 15, 2019
    Posts:
    34
    Recently I've been trying to develop a shotgun mechanic for my top-down shooter game. I have a list of transforms for where to spawn each bullet relative to the gun. And my code goes as follows:
    Code (CSharp):
    1.         foreach (Transform t in _bulletSpawnPoints)
    2.         {
    3.             GameObject g = Instantiate(_currentGun.gunData.bullet, _itemPivot.transform.GetChild(0), false);
    4.  
    5.             g.transform.localPosition = _bulletSpawnPoints[i].localPosition;
    6.             g.transform.localRotation = Quaternion.Euler(_bulletSpawnPoints[i].localRotation.eulerAngles);
    7.  
    8.             if (_currentGun.gunData.bulletSpeed != 0)
    9.             {
    10.                 Bullet bullet = g.GetComponent<Bullet>();
    11.                 bullet.speed = _currentGun.gunData.bulletSpeed;
    12.             }
    13.  
    14.             var r = Random.Range(-_currentGun.gunData.spread / 2, _currentGun.gunData.spread / 2);
    15.             g.transform.localRotation = Quaternion.Euler(0, 0, g.transform.rotation.z + r);
    16.  
    17.             i++;
    18.         }
    For some reason, on line 6 it isn't assigning the correct rotation, and is instead assigning a rotation that is much smaller than the desired one. For context my shotgun has 3 bulletSpawnPoints, each with the respective rotations of 20º, 0º, and -20º. But when the bullets spawn, they have the correct position, however the rotations are always around 0.1 degrees apart, instead of 20 degrees like they should be. I'm not really sure what's causing this, but I don't understand Quaternions well enough to be able to figure it out. I've tried every solution that I can find to this, and similar issues, so if anybody has the solution to my problem I would be very happy if you could share it.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,877
    I don't see anything obviously wrong here. You'll need to debug what's happening. That can be using a debugger to stop execution at break points and inspect values, or using Debug.Log to print out values at various places and ensure they are what you expect them to be. Then you can use this to narrow down where exactly the problem stems from.
     
    Tyrant7 likes this.
  3. Tyrant7

    Tyrant7

    Joined:
    Aug 15, 2019
    Posts:
    34
    Yeah, the thing is as far as I an tell the problem is arriving on one line, so I'm not really sure what could be happening, or how to properly debug it. I've tried looking at the values, but they all seem to be correct, is there something that I'm missing as to how transferring rotation from child objects works?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    You're setting the parent of the spawned bullet to be _itemPivot.transform.GetChild(0), but you're reading off the local rotation of _bulletSpawnPoints

    local rotation is relative to the parent, so that only makes sense if _itemPivot.transform.GetChild(0) is the parent of _bulletSpawnPoints, or if the absolute rotation of those two objects are the same.

    I generally find it easier to just work with the forwards vector and let the engine deal with figuring out the rotations. So if the spawn points are pointing in the correct direction, I'd just replace this line:

    Code (csharp):
    1. g.transform.localRotation = Quaternion.Euler(_bulletSpawnPoints[i].localRotation.eulerAngles);
    with this:

    Code (csharp):
    1. g.transform.forwards =_bulletSpawnPoints[i].forwards
     
  5. Tyrant7

    Tyrant7

    Joined:
    Aug 15, 2019
    Posts:
    34
    Thanks for the help! I tried that and it didn't seem to work, but then I realized something: when assigning the spread on the bullet on lines 14 and 15 I was setting the rotation of the bullet to 0 on the x and y axis's, I fixed it now and it seems to be working fine. Thanks for the help though!