- Home /
Change Pickup key
Hey everybody :) I have a little problem that i cant solve... Im not really in to C# but i got a pickup script that picks up the object with mouseclick. How can i change it so you pick it up with the key E?.This might be a noobie question but im not in to C# can someone help me please:)
using System.Collections; public class Pickup : MonoBehaviour { public enum Item { Flashlight, Battery } public float Distance = 5.0f; public Item item; public AudioClip flashlightSound; public AudioClip batterySound; void OnMouseDown() { var playerPos = GameObject.FindGameObjectWithTag("Player").transform.position; if(Vector3.Distance(playerPos, transform.position) <= Distance) { if(item == Item.Flashlight) { Flashlight.HeadLightMount.SetActiveRecursively(true); Flashlight.HeadLightMount.active = true; AudioSource.PlayClipAtPoint(flashlightSound, transform.position, 1); HUD.HasFlashlight = true; } else HUD.BatteryCount++; AudioSource.PlayClipAtPoint(batterySound, transform.position, 1); Destroy(gameObject); } } }
Answer by sullivanshad · Nov 30, 2013 at 06:30 PM
I believe if you change void OnMouseDown() to void update() and change your code on line 11 to this
if(input.GetKeyDown(KeyCode.E) && Vector3.Distance(playerPos, transform.position) <= Distance)
}
it should allow you to pick up the flashlight with E. Hope this helps you.
Your answer
Follow this Question
Related Questions
Click to destroy object 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
C# moveOnMouseClick Rotation 2 Answers
MouseButtonDown on button (Far away) NEED: Range for button 2 Answers
Rotating a Sphere with the mouse? 1 Answer