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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Resolved Changing Weapon Position Via Script

Discussion in 'Scripting' started by iamthebubb, Sep 25, 2022.

  1. iamthebubb

    iamthebubb

    Joined:
    Jul 21, 2016
    Posts:
    58
    Hey So I'm having an issue with this. I'm trying to change my gun position when the player runs.
    I made a little script to start doing this but not sure how to implement it so the changes to the position I want and moves with the parent "player" object? Currently if i try and change the gun position to something else, when I start moving the gun ends up in a weird position behind the player. I move the gun while the game is running, get the positions, then try and add them to the script

    Code (CSharp):
    1. public class GunPosition : MonoBehaviour
    2. {
    3.     //weapon object
    4.     public GameObject gameObj;
    5.  
    6.     //Attaching playermovement script to use it's varables
    7.     public playermovement Player;
    8.  
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         //weapon start position
    14.         gameObj.transform.position = new Vector3(0.1349f, 0.1358f, 0.3093f);
    15.         transform.localPosition = new Vector3(0.1349f, 0.1358f, 0.3093f);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Player.moveDirection != Vector3.zero) //when player is moving, change gun position
    22.         {
    23.             //Example
    24.             gameObj.transform.position = new Vector3(1.0f, 1.0f, 1.0f);
    25.             transform.localPosition = new Vector3(0.1349f, 0.1358f, 0.3093f);
    26.         }
    27.  
    28.     }
    29. }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,380
    I can't even understand what the problem is... can you post some screenshots? Preferably what it looks like before and after, and maybe a pointer to where you want the gun to actually be.

    From what it sounds like, your gun is probably parented to something you don't want it to. You should just switch that to match where you want your gun's new placement to be.
     
  3. iamthebubb

    iamthebubb

    Joined:
    Jul 21, 2016
    Posts:
    58
    Hey yeah the gun is parented to the player
     
  4. iamthebubb

    iamthebubb

    Joined:
    Jul 21, 2016
    Posts:
    58
    Solved the issue using Rig Layers