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 Using GameObject's Rotation

Discussion in 'Editor & General Support' started by Heroic0, Nov 6, 2017.

  1. Heroic0

    Heroic0

    Joined:
    Nov 6, 2017
    Posts:
    2
    To be more clear, here is how my Player gameobject is set up. I'm using the FirstPersonController.

    upload_2017-11-5_23-9-40.png

    My FPSController has a script that allows you to shoot from the gun, which takes in a BulletEmitter gameobject. I instantiate the bullet prefab at the bullet emitter's position and rotation using:

    Instantiate(bullet, bulletEmitter.transform.position, bulletEmitter.transform.rotation) as GameObject;

    However, the bullet is not firing at the bullet emitter's rotation, but it is using the emitter's position.

    How can I get it to fire at the emitter's rotation, so that the bullet fires in the direction that the gun is pointing?


    (Note: The script that controls the firing is on the FPSController. At first, I had it on the GunHeavy, and it worked fine. But I am turning this into a multiplayer game, and I can't add
    Code (CSharp):
    1.   if(!isLocalPlayer){
    2.  
    3.         return;
    4.  
    5.     }
    Without adding another NetworkIdentity to the GunHeavy, but one is already on the FPSController, so the only way it's letting me is to put the gun firing script on the FPSController. (That was confusing, whoops)

    Gun Shooting Script


    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.Networking;
    5.  
    6.     public class WeaponController : NetworkBehaviour {
    7.  
    8.         public GameObject bullet;
    9.         public GameObject bulletEmitter;
    10.         public float bulletSpeed;
    11.  
    12.         // Update is called once per frame
    13.         void Update () {
    14.  
    15.             if (!isLocalPlayer)
    16.             {
    17.                 return;
    18.             }
    19.  
    20.             if (Input.GetMouseButtonDown(0))
    21.             {
    22.                 // Create a bullet
    23.                 GameObject shotBullet = Instantiate(bullet, bulletEmitter.transform.position, bulletEmitter.transform.rotation) as GameObject;
    24.  
    25.                 float randomColor = Random.Range(1,6);
    26.  
    27.                 if(randomColor == 1)
    28.                 {
    29.                     shotBullet.GetComponent<Renderer>().material.color = Color.red;
    30.                 } else
    31.                 if(randomColor == 2)
    32.                 {
    33.                     shotBullet.GetComponent<Renderer>().material.color = Color.blue;
    34.                 } else
    35.                 if(randomColor == 3)
    36.                 {
    37.                     shotBullet.GetComponent<Renderer>().material.color = Color.green;
    38.                 } else
    39.                 if(randomColor == 4)
    40.                 {
    41.                     shotBullet.GetComponent<Renderer>().material.color = Color.magenta;
    42.                 } else
    43.                 if(randomColor == 5)
    44.                 {
    45.                     shotBullet.GetComponent<Renderer>().material.color = Color.yellow;
    46.                 }
    47.  
    48.                 shotBullet.GetComponent<Rigidbody>().AddForce(transform.forward * bulletSpeed * 100);
    49.  
    50.                 Destroy(shotBullet, 10f);
    51.             }
    52.  
    53.         }
    54.     }
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    I would think your problem lies in this line:
    Code (CSharp):
    1. shotBullet.GetComponent<Rigidbody>().AddForce(transform.forward * bulletSpeed * 100);
    You are using the direction of the transform that the script is on and not the direction of the bulletEmitter.
    Changing it like this should help:
    Code (CSharp):
    1.  shotBullet.GetComponent<Rigidbody>().AddForce(bulletEmitter.transform.forward * bulletSpeed * 100);
     
  3. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,902
    try bulletEmitter.transform.localRotation
     
  4. Heroic0

    Heroic0

    Joined:
    Nov 6, 2017
    Posts:
    2
    Didn't even think about that, thanks! Works great.