Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question child gameObject keep its own transform!!

Discussion in 'Scripting' started by hrohibil, Oct 18, 2023.

  1. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    280
    Hello

    For the life of me. I simply cant figure this one out. I tried so many differnts scripst, i even asked chatGTP!!

    I have a child gameobject which acts as gun firePoint transform. This is child of the gun barrel, which is child of the gun which is child of the hand etc etc...

    No matter what i cant get the bullet to shoot from the transform point.

    The code below is the only thing that actually worked , but issue here is that the empty is not a child of the gun to begin with. And my game object will be spawned in runtime so this approch wont work

    Code (CSharp):
    1. public Transform targetObject; // Reference to the target object.
    2.  
    3.     void Update()
    4.     {
    5.         if (targetObject != null)
    6.         {
    7.             // Set the empty GameObject's position and rotation to match the target object.
    8.             transform.position = targetObject.position;
    9.             transform.rotation = targetObject.rotation;
    10.         }
    11.     }

    My latest code is this

    Code (CSharp):
    1.  private Vector3 localPositionOffset;
    2.     private Quaternion localRotationOffset;
    3.     private Vector3 localScale;
    4.  
    5.     void Awake()
    6.     {
    7.         // Store the initial local position, local rotation, and local scale.
    8.         localPositionOffset = transform.localPosition;
    9.         localRotationOffset = transform.localRotation;
    10.         localScale = transform.localScale;
    11.     }
    12.  
    13.     void LateUpdate()
    14.     {
    15.         // Apply the stored local position, local rotation, and local scale.
    16.         transform.localPosition = localPositionOffset;
    17.         transform.localRotation = localRotationOffset;
    18.         transform.localScale = localScale;
    19.     }
    This does not work either, it shoots way of.



    https://imgur.com/a/tyltnbY
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,581
    Where is the code that instantiates the bullet? That's probably the best place to assign it's initial orientation. There's even an overload for Instantiate for that purpose.

    Also, don't trust ChatGPT, it will occasionally give you delusional idiot answers. Even if it gives you a working a solution, it might be a weird round-about solution that has a much simpler alternative.
     
    hrohibil likes this.
  3. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    280

    Code (CSharp):
    1.   // Instantiate a bullet at the fire point position.
    2.         GameObject bullet = Instantiate(bulletPrefab, firePoint1.position, firePoint1.rotation);
    3.      
    4.         // Access the bullet's Rigidbody and apply velocity.
    5.         Rigidbody bulletRigidbody = bullet.GetComponent<Rigidbody>();
    6.         bulletRigidbody.velocity = firePoint1.forward * bulletSpeed;
    7.         Debug.Log("ROBO SHOOT");
     
  4. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    280
    Got it Does Can you see what is missing here?
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,581
    This looks like it should work fine if you just delete that code in your first post.
     
  6. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    280
    I removed all this code:

    Code (CSharp):
    1.  private Vector3 localPositionOffset;
    2.     private Quaternion localRotationOffset;
    3.     private Vector3 localScale;
    4.     void Awake()
    5.     {
    6.         // Store the initial local position, local rotation, and local scale.
    7.         localPositionOffset = transform.localPosition;
    8.         localRotationOffset = transform.localRotation;
    9.         localScale = transform.localScale;
    10.     }
    11.     void LateUpdate()
    12.     {
    13.         // Apply the stored local position, local rotation, and local scale.
    14.         transform.localPosition = localPositionOffset;
    15.         transform.localRotation = localRotationOffset;
    16.         transform.localScale = localScale;
    17.     }
    Still shooting off
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,581
    Are you sure that you have the correct transform assigned to firePoint1? You could use something like
    Debug.Log(firePoint1.name);
    to see the name of the object.
     
  8. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    280
    Yes i have indeed.
    Its very frustrating. Because if my firePoint in not a child of anything, meaning just in the hierchy and i use a public transform to fetch it, then it works perfectly :-(

    Due to this enemy will be spawning in runtime i cant make that above situation to fit
     
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,581
    whether it's in the hierarchy or not shouldn't make any difference since firePoint1.position is in world space. :confused:

    You should still be able to assign it using a public member, even though it's in the hierarchy and your enemy prefab will retain that connection since they are both part of the same hierarchy.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,298
    I don't know what all those crazy every-frame Update() shenanigans are above, but you shouldn't need any of that!

    On spawn:

    - use the Transform of the gun firePoint (don't call that targetObject, call it something useful, like Muzzle, or GunMuzzle... help yourself out as much as possible!)

    - copy the position and rotation out and use it to spawn the bullet

    - use the .forward property on the gun / fire point to know which way the bullet should travel

    After that, you no longer need the firepoint.

    MAKE SURE you are not simply seeing a physics artifact of the shot's collider "bumping" into the hand / gun / player when fired. That's very common and handled by proper use of Layers / Layermasks and the physics collision matrix.

    The difference between Layers vs LayerMasks:

    https://forum.unity.com/threads/raycast-layermask-parameter.944194/#post-6161542

    "There are 10 types of people in this world: those who understand binary, and those who don't."