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

Bullet orientation issues

Discussion in 'Scripting' started by GladisAd, Mar 12, 2015.

  1. GladisAd

    GladisAd

    Joined:
    Oct 5, 2014
    Posts:
    28
    I've been using one of the very useful Unity tutorials regarding shooting bullets on Youtube. However, the tutorial uses a sphere as a bullet, so the orientation isn't a problem for the specific tutorial. I'm using an actual 3D prefab shell, and no matter what i do with the orientation (in the Unity editor) it simply will not change it's orientation from vertical to horizontal, apart from do some very strange things. Could someone please explain, in this code, what is wrong please?

    I have looked at similar responses on the forum, but i have no coding skills, so am unsure how to change the code to make it work. This is a JavaScript file.

    Shoot code
    Code (csharp):
    1.  #pragma strict
    2.   var prefabBullet:Rigidbody;
    3.   var shootForce:float;
    4.   var shootPosition:Transform;
    5.  
    6.   function Update ()
    7.   {
    8.      if(Input.GetMouseButtonDown(0))
    9.      {
    10.      var instanceBullet = Instantiate(prefabBullet, transform.position, shootPosition.rotation);
    11.      instanceBullet.rigidbody.AddForce(shootPosition.forward * shootForce);
    12.    
    13.      }}
    Many thanks.
     
  2. GladisAd

    GladisAd

    Joined:
    Oct 5, 2014
    Posts:
    28
    I've managed to sort out the bullet orientation by using a shell_location gameobject , moving it away from the object it is firing from and manually rotating the shell to it's desired horizontal orientation. However, the bullet now appears when i press the mouse button, to fire the bullet, and doesn't seem to have any force, so the bullet just appears and doesn't move any distance.
     
  3. GladisAd

    GladisAd

    Joined:
    Oct 5, 2014
    Posts:
    28
    I've changed the shootPosition.forward to shootPosition.right which seems to work just fine, but now the bullet actually fires sideways and directly to the right, no longer in front. If i change it back to .forward the bullet just spawns in place?
     
  4. GladisAd

    GladisAd

    Joined:
    Oct 5, 2014
    Posts:
    28
    Any help appreciated :).