The question is answered.
C# FPS Pick up and Carry script help?
Hello, I am new to coding and would like some help with how I can edit this code. I was doing a tutorial and I wanted to edit it. I want the player to be able to press E to pick up the object and then also put it down by pressing E again.
Right now you can press E to pick it up and then right click on mouse to drop it.
void Update()
{
//check distance of gameObject and player
float dist = Vector3.Distance(gameObject.transform.position, player.position);
if (dist <= 4.5f)
{
//can be picked up
hasPlayer = true;
}
else
{
hasPlayer = false;
}
if (hasPlayer && Input.GetButtonDown("Use"))
{
GetComponent<Rigidbody>().isKinematic = true;
transform.parent = p_Camera;
beingCarried = true;
}
if (beingCarried)
{
if (touched)
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
beingCarried = false;
touched = false;
}
if (Input.GetMouseButtonDown(0))
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
beingCarried = false;
GetComponent<Rigidbody>().AddForce(p_Camera.forward * throwForce);
}
//right mouse button
else if (Input.GetMouseButtonDown(1))
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
beingCarried = false;
}
}
}
void OnTriggerEnter()
{
if (beingCarried)
{
touched = true;
}
}
}
I'm guessing I have to make "else if (Input.GetMouseButtonDown(1))" to "Input.GetButtonDown("Use")" but I'm not sure what to do for " (Input.GetMouseButtonDown(0))." Any help would be appreciated.
Answer by Nomenokes · Feb 11, 2017 at 06:58 PM
Right now mouse button 0 throws it, and mouse button 1 drops it. What you should do is have mouse button 1 or "e" drop it. In code, this would be:
else if(Input.GetMouseButtonDown(1) || Input.GetButtonDown("e"))
When using that code I got an error to create Input for $$anonymous$$ I then created an input for E and for positive button put e but it didn't work. I tried putting Input.Get$$anonymous$$eyDown("e")) but that didnt work either. Any suggestions? When I create Input for E it doesnt give any errors just doesn't work.
Hmm...
Try running this in FixedUpdate ins$$anonymous$$d of Update.
If that doesn't work try using Input.GetButton ins$$anonymous$$d of Input.GetButtonDown
Follow this Question
Related Questions
transform.Rotate having no effect 0 Answers
how to make bullet go straight to middle of the screen 3 Answers
Cant figure out how to make a pickup that swaps one game object for another. 0 Answers
Gun shoot problem? 1 Answer
Recoil system 0 Answers