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

How can i give FPSController a new parent in c#

Discussion in 'Scripting' started by Demonichero, Dec 17, 2015.

  1. Demonichero

    Demonichero

    Joined:
    Nov 28, 2015
    Posts:
    6
    Ok to give you an idea when my game starts my FPSController is inside a model of a helicopter.
    The helicopter is animated.
    My FPSController is a Child of the Helicopter Model
    (This is so you can stand and move in the helicopter as its moving else if FPSController is not a child you jutter as it tries to chuck you through the collider on the helicopter).
    So good so far how ever when you get off the helicopter i have it animated to fly off and crash lol.
    Because my FPSController is still a child i start flying across the map too.
    So is there a way that once i get off the helicopter i can change the parent of my FPSController to an empty object i have in the hirarchy called Player so that i'm not further affected by the animations ?
    Please help.

    Alternatively is there a way i can stand in the helicopter as it moves without FPSController being a child of the helicopter?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    On the root transform of your FPS controller, just do a .SetParent(null) and that will disconnect you.
     
  3. Demonichero

    Demonichero

    Joined:
    Nov 28, 2015
    Posts:
    6
    Thank you Well i have this and it works ...But it catapults me off the map lol

    using UnityEngine;
    using System.Collections;

    public class ParentChangeScript : MonoBehaviour {

    public GameObject player;

    public void OnTriggerEnter(Collider other)
    {
    if (other.CompareTag("Player"))
    {
    player.transform.parent = null;
    }
    }

    }

    The console is saying to use "setParent" method. Ive been trying that but i cant trigger it off even though the tip says


    //Invoked when a button is clicked.
    public void Example(Transform newParent)
    {
    //Sets "newParent" as the new parent of the player GameObject.
    player.transform.SetParent(newParent);

    //Same as above, except this makes the player keep its local orientation rather than its global orientation.
    player.transform.SetParent(newParent, false);
    }
    }

    cant for love or money figure this out even when searching on line they just say use the above but don't say how to trigger it. I cant use the on collider trigger as above because i cant link to the script with

    public void OnTriggerEnter(Collider other)
    {
    if (other.CompareTag("Player"))
    {
    GameObject.Find("FPSController").GetComponent<ParentChangeScript>().Example();

    }
    }

    this just gives me an error. lol

    please help more lol