Stop child instatiate get effect by parent

Hello guys,

Currently I’m trying to make a FPS game with a faux gravity. I set my faux gravity for my player and the ground with a body and attractor and when I started doing shooting I noticed that the bullets were all flying towards sky. I couldn’t understand why it is so first but I guess it is because of the transform.position changes in parents.

What can I do in order to stop the child to get effected by parent? Here are the codes

Gravity Body

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Rigidbody))]
public class GravityBody : MonoBehaviour {
   
    GravityAttractor planet;
    Rigidbody rigidbody;
   
    void Awake () {
        planet = GameObject.FindGameObjectWithTag("Ground").GetComponent<GravityAttractor>();
        rigidbody = GetComponent<Rigidbody> ();

        // Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
        rigidbody.useGravity = false;
        rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
    }
   
    void FixedUpdate () {
        // Allow this body to be influenced by planet's gravity
        planet.Attract(rigidbody);
    }
}

Gravity Attractor

using UnityEngine;
using System.Collections;

public class GravityAttractor : MonoBehaviour {
   
    public float gravity = -100f;
   
   
    public void Attract(Rigidbody body) {
        Vector3 gravityUp = (body.position - transform.position).normalized;
        Vector3 localUp = body.transform.up;
       
        // Apply downwards gravity to body
        body.AddForce(gravityUp * gravity);
        // Allign bodies up axis with the centre of planet
        body.rotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
    } 
}

Fire

using UnityEngine;
using System.Collections;

public class Fire : MonoBehaviour {

    public Rigidbody projectile;
   
    public float speed = 1;
    public Transform weapon;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetButtonDown("Fire1"))
        {
            Rigidbody instantiatedProjectile = Instantiate(projectile,
                                                           transform.position,
                                                           weapon.rotation)
                as Rigidbody;

            Debug.Log(weapon.position);
            instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
            //instantiatedProjectile.AddForce(instantiatedProjectile.transform.forward * speed);

        }
    }
}

Does your projectile have the gravitybody script attached to it in the prefab?

No, Prefab has only destroy event script and fire script belogns to weapon. Since Weapon is childe of player I think it’s position is getting affected by the parent

You are instantiting with world position, so it should start in the right place.

If probably is colliding with your player collider and causing it to fly off. If the start location is inside another collider you will want to have it ignore that one.

What you say might be possible. How can I ignore it?

You can do through code or editor. Probably the easiest is the editor. You’ll need to have them on different layers, you can layers in the layers pull down on the right. Make the something like Player and Projectiles. Then make sure the objects are set to those layers in the inspector.

Then under edit > project settings > physics, you will find a matrix of layers. Find projectile, then deselect its box for player. You may want to deselect projectile as well so shots won’t hit other shots.

Bear in mind that they will now ignore anything on the player layer. So if you have enemies, they can’t be on player layer. You use this method to exclude elements in your game as well.

1 Like

You are the man. Incredible mate. Thanks to you I managed to handle this weird situation which I’ve been working for last 4 days. Thank you :slight_smile:

Glad I could help. I remember when I first banged my head against that one. :wink:

I set up projectiles once with a script that has them set to triggers when they are created. Then in OnTriggerExit they become actual colliders.

Now I just make sure the spawn point is outside of the gun model.