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

*DELETE*

Discussion in 'Scripting' started by aviator2005, Jul 8, 2013.

  1. aviator2005

    aviator2005

    Joined:
    May 20, 2013
    Posts:
    9
    *DELETE*
     
    Last edited: Oct 1, 2015
  2. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    Well I guess there are different approaches for your problem but I'd go like this:

    1. Add an EmptyObject as child to your FPS-Controller. This represents the position where your weapon will be placed later.
    2. If your press eg E, make the weapon a child of the FPS-Controller and then position it at the emptyObject you attached before.

    Hope that helps you.
     
  3. xniinja

    xniinja

    Joined:
    Mar 12, 2011
    Posts:
    230
    If you are a complete noob and need more help than what yodel!dodel offered, this should help.
    This is mainly for people that stumble upon this thread through Google.


    For the first part of yodel!dodel's comment he suggests that you should add an empty game object as a child to your First Person Controller. This is pretty much any old game object that doesn't have any components attached to it. It's pretty much just a representation for a position. You could just put a position into your code using a Vector3 variable, but this is more visual.

    Once you have the empty game object added you need to know how to add children to your game object. To do this you need to go like this:

    Code (csharp):
    1.  Weapon.transform.parent = FPController.transform;
    Once you do this you need to tell the gun to rotate the right way and be at the right spot. This is where your game object comes in. Make sure you are working in local position to your First Person Controller this will simply make things easier for you, especially for rotation. To do this go like:

    Code (csharp):
    1.  
    2.  
    3. Weapon.transform.localPosition = emptyGameObject.transform.localPosition;
    4. Weapon.localEulerAngles = emptyGameObject.localEulerAngles; //This SHOULD work, I usually suck at rotations though, so speak up if it doesn't
    5.  
    6.  
    And that's pretty much all you need to know.

    Also, tell me if I made any mistakes. I would prefer to not look like a bumbling idiot.
     
    Last edited: Jul 8, 2013
  4. aviator2005

    aviator2005

    Joined:
    May 20, 2013
    Posts:
    9
    Thank you everyone for your help so far! @xniinja - I did what you suggested, but it seems like I am missing some kind of fundamental concept of unity ... It works, but if i walk towards the edge of a level, the cube falls off the map. Likewise, if I walk around it moves with terrain rather than being "pinned" at the empty game object position. Ideas? My code below:
    Code (csharp):
    1.  
    2. void OnTriggerStay(Collider other) {
    3.         if (other.gameObject.name == "First Person Controller"  Input.GetKeyDown ("e")) {
    4.             gameObject.transform.parent = other.transform;
    5.             Transform WeaponMount = other.transform.FindChild ("WeaponMount");
    6.            
    7.             gameObject.transform.localPosition = WeaponMount.localPosition;
    8.             gameObject.transform.localEulerAngles = WeaponMount.localEulerAngles;
    9.             print ("I want to pick up the weapon.");   
    10.         }
    11.     }
     
  5. xniinja

    xniinja

    Joined:
    Mar 12, 2011
    Posts:
    230
    Could you explain what you mean by "moves with terrain." does that mean it goes up and down when you go over hills? Also, what do you mean by the cube falling off the map. Does that mean the gun or the game object you are attaching to it, or something else?
     
  6. aviator2005

    aviator2005

    Joined:
    May 20, 2013
    Posts:
    9
    Here is a short video showing you what I mean:
     
  7. xniinja

    xniinja

    Joined:
    Mar 12, 2011
    Posts:
    230
    Oh, make sure you don't have the rigidbody component on the cube that you are using as the gun. Then it should work like a charm.