- Home /
Need help with picking up items by typing "E"
So I'm still working on my game and I've come across another problem I can't solve on my own, and can't seem to find a tutorial on YouTube or anywhere else that can help me with it.
I'm wanting to make a script that will allow the player to pickup something when they are looking at it by pressing "E" on the keyboard Similar to the way you can pickup stuff in The Elder Scrolls Games. I need a script that will be able to pickup ammo and add it to my Current Ammo, when a player looks at a "clip" and presses "E" I would also (if Possible) like to make it so you can drag the item around by Holding down "E" (Again similar to games like Skyrim) Can anybody help with this?
Here is my Script that takes care of the guns and ammo. (if needed)
var PlayerTransform : Transform;
var PlayerScript : Player;
var RotationSpeed : float;
var PlayerCamera : GameObject;
var IsAiming : boolean = false;
var GunAnimationHolder : GameObject;
//Targets
@HideInInspector
public var TargetXrotation : float;
@HideInInspector
public var TargetYrotation : float;
@HideInInspector
public var TargetXrotationV : float;
@HideInInspector
public var TargetYrotationV : float;
// Gun Specs
var MaxClipSize : float = 10;
public static var AmmoInCurrentClip : float = 10;
public static var ExtraAmmo : float = 0;
var MaxCarringAmmo : float = 30;
// Bullets and shiz
var Bullet : GameObject;
var BulletSpawn : GameObject;
var BulletSound : GameObject;
var FireRate : float;
var FireTimer : float;
//Recoil
var Recoil : float = 3;
var RecoilAimingIn : float = 3;
var RecoilAimingOut : float = 6;
//Reloads
var ReloadAnimation : GameObject;
var ReloadSound : GameObject;
var Reloading : boolean = false;
var ReloadName : String;
function Update()
{
AimingInController();
}
function AimingInController()
{
IsAiming = GunAnimationHolder.GetComponent(Player).AimingIn;
}
function LateUpdate ()
{
if (AmmoInCurrentClip > MaxClipSize)
AmmoInCurrentClip = MaxClipSize;
if (ExtraAmmo > MaxCarringAmmo)
ExtraAmmo = MaxCarringAmmo;
if (FireTimer < -5)
FireTimer = -5;
if (MaxClipSize < 0)
MaxClipSize = 0;
if (AmmoInCurrentClip < 0)
AmmoInCurrentClip = 0;
if (!Reloading && AmmoInCurrentClip < MaxClipSize && ExtraAmmo > 0 && Input.GetButtonDown("Reload")&& IsAiming == false)
{
Reloading = true;
ReloadSound.Play();
ReloadAnimation.animation.Play("Reload");
}
if (!Reloading && AmmoInCurrentClip == 0 && ExtraAmmo > 0 && Input.GetButtonDown("Fire1") && IsAiming == false)
{
Reloading = true;
ReloadSound.Play();
ReloadAnimation.animation.Play("Reload");
}
if (Reloading && !ReloadAnimation.animation.IsPlaying(ReloadName))
{
if (ExtraAmmo >= MaxClipSize - AmmoInCurrentClip)
{
ExtraAmmo -= MaxClipSize - AmmoInCurrentClip;
AmmoInCurrentClip = MaxClipSize;
}
if (ExtraAmmo < MaxClipSize - AmmoInCurrentClip)
{
AmmoInCurrentClip += ExtraAmmo;
ExtraAmmo = 0;
}
Reloading = false;
}
var MyBulletSound : GameObject;
if (Input.GetButton("Fire1") && AmmoInCurrentClip > 0 && !Reloading)
{
if (FireTimer <= 0)
{
AmmoInCurrentClip -= 1;
TargetXrotation += (Random.value - 0.5) *Mathf.Lerp (Recoil, RecoilAimingOut,0);
TargetYrotation += (Random.value - 0.5) *Mathf.Lerp (Recoil, RecoilAimingOut,0);
if (Bullet)
{
Instantiate(Bullet,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
}
if (BulletSound)
{
MyBulletSound = Instantiate(BulletSound,BulletSpawn.transform.position,BulletSpawn.transform.rotation);
}
FireTimer = 1;
}
}
if (Input.GetButton("Fire2") && !Reloading )
{
Recoil = RecoilAimingIn;
}
if (Input.GetButton("Fire2") == false || Reloading )
{
Recoil = RecoilAimingOut;
}
FireTimer -= Time.deltaTime * FireRate;
transform.position = PlayerCamera.transform.position + (Quaternion.Euler(0,TargetYrotation,0)* Quaternion.Euler(TargetXrotation,TargetYrotation,0) * Vector3(0,0,0));
TargetXrotation = Mathf.SmoothDamp(TargetXrotation, -PlayerCamera.GetComponent(MouseLook).rotationY,TargetXrotationV,RotationSpeed);
TargetYrotation = Mathf.SmoothDamp(TargetYrotation, PlayerCamera.GetComponent(MouseLook).RotationX,TargetYrotationV,RotationSpeed);
transform.rotation = Quaternion.Euler(TargetXrotation,TargetYrotation,0);
}
This is no simple topic! Try bringing your problem to the forums.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Ladder script not working? 0 Answers
Java script for for basic walking and jumping? 0 Answers
I need help instantiateing a bullet 0 Answers
Unity Editor Function Animation 0 Answers