Move Weapon with the player when picked up

Hey guys,
I wrote a script which move my weapon to the point I’ved set in the scene.The problem is when i pick it up I don’t want that it just move to the point I want that it moves to the point and stay with the player (sorry for bad english I hope you understand what I mean)
I’ve add the point where it moves to, to the camera in the scene.

Here’s my code :

var gegenstand :Transform;

var stopTakeWeapon = false;

var Spawn :Transform;

function takeWeapon ()
{

  Debug.Log("takeweapon gestartet");

  

  if(Vector3.Distance(transform.position,gegenstand.position)<=5){
   
    Debug.Log("unter 5 meter");
 
    if(Input.GetButtonDown ("e") ){

      Debug.Log("e gedrückt");

     
      gegenstand.transform.position = Spawn.position;
      //gegenstand.transform.position = Vector3(0,20,50); 
      
      stopTakeWeapon = true;
   
      }
    }
}

function Update ()
{

  if(stopTakeWeapon == false)
  {
    takeWeapon ();
  }
  
}

Thanks for help :slight_smile:

I dont understand what you are trying to say when you explain that you want the weapon to move. Do you want weapon sway? you want an animation for the weapon being picked up?

it works with other stuff, for me. And by that I mean, calling an external function from outside OnGUI..

using UnityEngine; using System; using System.Collections; public class Example : MonoBehaviour { void Update() { NotInGUI(); } //Will not work. void NotInGUI() { GUILayout.Label("Outside of GUI Function"); } //Will work. void OnGUI() { GUILayout.Label("Inside GUI Function"); } } [26158-notingui.png|26158]

putting it back inside OnGUI did not do anything.

and btw, originally, I was calling the ServerCreate function from within the OnGUI function. Not Update and public class Example : MonoBehaviour { void NotInGUI() { GUILayout.Label("Outside of GUI Function"); } void OnGUI() { NotInGUI(); } } will work.

2 Answers

2

You have to make the gun a child of the Player Camera or just the player-object like this:

    var gegenstand :Transform;
     
    var stopTakeWeapon = false;
     
    var Spawn :Transform;

    var PlayerCamera : GameObject; //Drag the PlayerCamera here
     
    function takeWeapon ()
    {
     
    Debug.Log("takeweapon gestartet");
     
     
     
    if(Vector3.Distance(transform.position,gegenstand.position)<=5){
     
    Debug.Log("unter 5 meter");
     
    if(Input.GetButtonDown ("e") ){
     
    Debug.Log("e gedrückt");
     
     
    gegenstand.transform.position = Spawn.position;
    gegenstand.transform.parent = PlayerCamera.transform; //Sets the gegenstand parent as the PlayerCamera-Gameobject
     
    stopTakeWeapon = true;
     
    }
    }
    }
    ....

(Aha schön mal einen anderen Deutschen hier zu sehen :slight_smile: )

If you have a question about it or if it’s not working in your case let me know.

Btw. if your problem is solved please accept an answer or close the question.

I was thinking about this, that’s why I add the Spawnpoint to the PlayerCamera. I thought when I add it to the camera the Weapon would be add as a child of the camera too…
But your answer isn’t working too…

Freut mich auch jemanden mal anzutreffen der deutsch kann :wink:

Thanks, but I already could solve this problem, the code you wrote down is working now :)

Ok good. Now you should either accept an answer or close the question to mark it as solved. Good luck with your project.