Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EscTheCtrl · Jun 08, 2013 at 05:24 PM · javapickupammo

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);
 
 
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Negagames · Jun 08, 2013 at 06:30 PM 0
Share

This is no simple topic! Try bringing your problem to the forums.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges