Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 lsugzdinis · Dec 19, 2020 at 05:12 PM · 3dgunaim

How to move gun from middle of screen to right of screen?

So i'm making an FPS where time slows down when you aim. There's an aim counter that automatically un-aims the gun when you've been aiming too long. For this, I'm moving the gun from the center of the screen (when you aim) to the right of the screen (when you're not aiming). I don't want to move the gun to a specific position, because it should follow the camera. I've been using transform.Translate, but it's not working. i'm fairly new to unity, so any help would be appreciated.

this is the code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerShoot : MonoBehaviour { public GameObject bulletPrefab; public Transform gunTip;

 public float speed = 8f;
 public bool inRange = false;

 public Camera camera;
 public GameObject gun;

 public float maxAim = 3f;
 public float currentAim;
 public float aimWait;
 public bool gunAiming;
 public bool gunRight;
 public bool gunMoved;

 // Start is called before the first frame update
 void Start()
 {
     currentAim = maxAim;
     gunAiming = false;

     aimWait = 1f;

     gunMoved = false;
 }

 // Update is called once per frame
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Mouse0))
     {
         GameObject bulletObject = Instantiate(bulletPrefab);
         bulletObject.transform.position = gunTip.transform.position;
         bulletObject.transform.forward = gunTip.transform.forward * speed;
     }

     if(currentAim <= 0)
     {
         gunRight = true;
         gunMoved = true;
     }
     else
     {
         gunRight = false;
         gunMoved = false;
     }

     if(Input.GetKeyDown(KeyCode.Mouse1))
     {
         gun.transform.Translate(Vector3.right);
         gunAiming = true;
     }

     if (Input.GetKeyUp(KeyCode.Mouse1))
     {
         gun.transform.Translate(Vector3.left);
         gunAiming = false;
     }


     // StartCoroutine(Aim());
     Aim();

     if (Input.GetKey(KeyCode.Mouse1))
     {
         //  Aim();
         gunAiming = true;
     }
     else
     {
         //  NoAim();
         gunAiming = false;
     }
 }

 void Aim()
 {
     if (gunAiming)
     {
         //    gun.transform.Translate(Vector3.right);
         if(!gunRight)
         {
         //    gun.transform.Translate(Vector3.left);
             Time.timeScale = 0.1f;
             camera.fieldOfView = 45;
             currentAim -= Time.deltaTime * 10;
         }
         if(gunRight)
         {
             if(gunMoved)
             {
                 gun.transform.Translate(Vector3.left);
                 gunMoved = false;
             }
             
             if(!gunMoved)
             {
                 Time.timeScale = 1f;
                 camera.fieldOfView = 60;
             }

             

           //  yield return new WaitForSecondsRealtime(0.02f);

        //     gunRight = false;
         }
     }

     if (!gunAiming)
     {
         Time.timeScale = 1f;
         camera.fieldOfView = 60;

         if (currentAim < maxAim)
         {
             currentAim += Time.deltaTime / 2;
         }
     }
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pokedlg3 · Dec 19, 2020 at 05:27 PM

Just turn the weapon into a child object of the camera. In the hierarchy, take your weapon and drag it to your camera, so that you will turn the weapon into a child object of the camera and it will move according to a camera.

Comment
Add comment · Show 4 · Share
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 lsugzdinis · Dec 19, 2020 at 06:56 PM 0
Share

I already did that, I want to move it from the middle of the camera to the right of the camera when I stop ai$$anonymous$$g.

avatar image Pokedlg3 lsugzdinis · Dec 19, 2020 at 08:05 PM 0
Share

Well, I analyzed your script and try to add this:

  if (gunAi$$anonymous$$g == false)
  {
       gun.transform.position = Vector3.Lerp(gun.transform.position, Vector3.right,                     Time.deltaTime);
  }
avatar image lsugzdinis Pokedlg3 · Dec 19, 2020 at 09:02 PM 0
Share

now the gun moves away from the player

Show more comments

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

141 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

when float is less then zero, do action once 1 Answer

Weapon random movement 0 Answers

FPS - Attach a gun to hands and move arms when aiming up and down 1 Answer

Aiming Gun at Cursor 2 Answers

I Want To Shoot Bullets Correctly. 2 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