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

Instantiate rotation not working

Discussion in 'Scripting' started by McGluckerson, May 19, 2021.

  1. McGluckerson

    McGluckerson

    Joined:
    Apr 10, 2020
    Posts:
    7
    Code (CSharp):
    1. Rigidbody p = Instantiate(AssaultRifleProjectile, bulletSpawn.position, spread);
    2.         p.velocity = transform.forward * AssaultRifleProjectileSpeed;
    so I have it when I press left click it Instantiates a bullet. I figured out a way to add randomized spread but when I put it into the Instantiate rotation it doesn't do any thing. I have tried doing it with set numbers and nothing still happens. it always spawns facing completely forward no matter what value I put in for the rotation. thx
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Replace your AssaultRifleProjectile with a prefab made out of a cube (and an RB)... does that work?

    If so maybe you have an animation on the real projectile and it's resetting the rotation.

    Or something else is doing it, such as a script on the projectile.
     
  3. McGluckerson

    McGluckerson

    Joined:
    Apr 10, 2020
    Posts:
    7
    i do have a script on the projectile that deletes it when it comes into contact with another object. ill try the cube thing.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    How are you calculating the spread? You did not share that code which is most likely to be causing the problem.
     
  5. McGluckerson

    McGluckerson

    Joined:
    Apr 10, 2020
    Posts:
    7
    Code (CSharp):
    1. nextShotSpread = Random.Range(-AssaultRifleSpread, AssaultRifleSpread);
    2.         spread = (bulletSpawn.transform.rotation.x, (bulletSpawn.transform.rotation.y + nextShotSpread), bulletSpawn.tranform.rotation.z);
    here's my scuffed code.
    it also didn't work when I entered a fixed quaternion
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Code (CSharp):
    1. spread = (bulletSpawn.transform.rotation.x, (bulletSpawn.transform.rotation.y + nextShotSpread), bulletSpawn.tranform.rotation.z);
    Is this as-written? That's not valid C# code and should be throwing an error*.

    You are misunderstanding the nature of quaternions. Quaternions are not the same thing as Euler angles, in which x, y, and z correspond to a rotation about the x, y, and z axis. It is not suggested to directly manipulate quaternions. Instead, create your rotation by using a utility method like Quaternion.Euler.

    *Unless you're on C#7 and defining a tuple, I suppose.