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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems with parenting of objects

Discussion in 'Editor & General Support' started by Richard9699, May 4, 2017.

  1. Richard9699

    Richard9699

    Joined:
    Nov 10, 2015
    Posts:
    11
    Hi. I have a model of a gun,which is by the way paranted to my caracter´s camera(it´s a first person character). I was trying to make a instantiation of bullets from the gun,but the rotation of the bullets path gets weird when I look too much for up or down,I mean,they don´t follow the rotation of the gun. I also tried to create a empty object and place it on the tip of the gun so that it would make the paper of instantiating the bullets,but the same problem involving their rotation happened. Here are some images that may help me explain the situation(oh,and sorry for my english)

    http://pasteboard.co/22hzq2m0d.png
    http://pasteboard.co/22jgYpkWV.png
     
    Last edited: May 6, 2017
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    how do you instantiate the bullets there? (can you show code?)
     
    Richard9699 likes this.
  3. Richard9699

    Richard9699

    Joined:
    Nov 10, 2015
    Posts:
    11
    I can't show the entire code right now,but it's basically this(this is assigned to the weapon's object)

    var Bullet :GameObject;
    If(Input.GetMouseButton(0))
    {
    Instantiate(Bullet,transform.position,transform.rotation)
    }
     
  4. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
    after creating the bullet, try bullet.transform.forward = gun.transform.forward

    don't parent the bullet to the gun after its fired... move it seperately... jsut use gun muzzle position to get position and orientation.
    bullet
    move it like this....
    Vector3 moveVector = gun.transform.forward.normalized;
    bullet.transform.position = bullet.transform.position + moveVEctor * bulletSpeed * Time.deltaTime

    This should work as long as your bullet and gun point forward along the Z / forward axis in their default positions.... if the bullet shoots 90 degrees to the side or up.... then you'll need to use transform.right or transform.up instead (or a - in front)
     
  5. Richard9699

    Richard9699

    Joined:
    Nov 10, 2015
    Posts:
    11
    fine,but... How would I acess the bullet transform data and the gun transform data to put it in the same script?
     
  6. JonPQ

    JonPQ

    Joined:
    Aug 10, 2016
    Posts:
    120
    in editor window... browse your hierarchy to the gun... see if there is a nice transform on there that corresponds to the muzzle position, or a bullet object. Personally, I would add my own node to the the gun as a child at the position of the muzzle.... Then you can use script to spawn a bullet prefab at that muzzle transform position whenever you like... and muzzle flash or smoke effects.
    Add a script to the gun called... gun.cs might work. Expose a variable.... public Transform muzzleTransform. then drag your new muzzle node object onto that variable. Now its pretty easy in script to reference that muzzle transforms' orientation or position. Or to spawn new bullet or object at its position...