Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JetStreamSham · Feb 21, 2019 at 02:38 PM · cameraaimingguns

Aimimg guns like in reciever

How do I aim my gun like in the game reciever. In receiver the gun when aimed moves independently of the camera inside a small deadzone when it reaches past that deadzone the camera rotates in the direction the gun is being moved. https://www.youtube.com/watch?v=GCThInmzjXw

I was able to get the gun to move within a deadzone but when it leaves the deadzone the gun does properly rotate with the camera. Instead the distance between the camera and gun is changed and the gun becomes unusable.

My code:

 public class GunMouseLook : MonoBehaviour {
 
     public Transform gun,gunRest,gunAim;
     public float aimSpeed;
     public float XGunAimSensistivity = 3f;
     public float YGunAimSensistivity = 3F;
 
     public SimpleSmoothMouseLook ssml;
 
     [Range(0,1)]
     public float xMaxDist;
     [Range(0,1)]
     public float yMaxDist;
     [Range(0,1)]
     public float zMaxDist;
     [Range(0,1)]
     public float zMinDist;
     [Range(0,1)]
     public float zInterval;
     public float maxDistanceFromCenter;
     public float maxDist;
     bool reachedTarget;
 
     // Update is called once per frame
     public void LateUpdate()
     {
         if(Input.GetMouseButton(1)){
             GunLook();
         }
         else{
             NormalLook();
         }
     }
 
 
     public void NormalLook(){
         Cursor.lockState = CursorLockMode.Locked;
         gun.position = Vector3.Lerp(gun.position,gunRest.position,aimSpeed*Time.deltaTime);
         gun.rotation = Quaternion.Slerp(gun.rotation,gunRest.rotation,aimSpeed*Time.deltaTime);
         reachedTarget = false;
         ssml.enabled=true;
     }
 
     public void GunLook(){
 
 
         
         if(!reachedTarget){
             if((Vector3.Distance(gun.position,gunAim.position)<0.1f)){
                 reachedTarget=true;          
             } else{
                 gun.position = Vector3.Lerp(gun.position,gunAim.position,aimSpeed*Time.deltaTime);
                 gun.rotation = Quaternion.Slerp(gun.rotation,gunAim.rotation,aimSpeed*Time.deltaTime);
             }
             return;
         }
         
         Vector3 screenPointGun = Camera.main.WorldToScreenPoint(gun.position);
         float distanceFromCenter =  Vector3.Distance(screenPointGun,new Vector3(1/2,1/2,0));
         Debug.Log(distanceFromCenter);
         if(distanceFromCenter > maxDistanceFromCenter)
             ssml.enabled = true;
         else{
             float z = Mathf.Clamp(gun.position.z + zInterval * Input.mouseScrollDelta.y, zMinDist, zMaxDist);
             Vector2 mp = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * 0.1f;
             gun.position = (gun.position + new Vector3(mp.x, mp.y, 0));
             gun.rotation = transform.rotation;
         }
     }
     
 
 }

example gif

Comment
Add comment · Show 6
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 xxmariofer · Feb 21, 2019 at 03:02 PM 0
Share

share your code for us to help you.

avatar image JetStreamSham xxmariofer · Feb 21, 2019 at 03:09 PM 0
Share

oh yea thats a good idea brb

avatar image xxmariofer JetStreamSham · Feb 21, 2019 at 03:31 PM 0
Share

you are adding to the gunposition the inputaxis value, when the distanceFromCenter is lower than the maxDistanceFromCenter, shouldnt it be the other way? you should be adding the value while your distanceFromCenter is higher or i am missing something?

Show more comments
avatar image zereda-games · Feb 21, 2019 at 03:09 PM 0
Share

Got any examples of your issue, images, code. more details

avatar image zereda-games zereda-games · Feb 21, 2019 at 03:25 PM 0
Share

$$anonymous$$e and my Camera manipulation is not so hot.. Bump.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cornelis-de-Jager · Feb 24, 2019 at 11:08 PM

The function below checks for a deadZone angle and rotates the transform if the angle is past:

 public float deadZone = 30;
         
     void GunLookDeadZoneRotation() {
         // Get the angle
         float angleDiff = Vector3.Angle (transform.forward, gunAim.forward);
         
         float rotY = angleDiff.y;
         
         if (angleDiff < deadZone)
             return;
         
         transform.rotation = transform.localRotation * Quaternion.Euler(-rotY, 0f, 0f);
     }
Comment
Add comment · 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

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

160 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 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

Bombs and Sniper Camera 1 Answer

Shell Drop and Aiming 1 Answer

How to make camera position relative to a specific target. 1 Answer

How do I get 2D Soldat-style camera movement/aiming? 0 Answers

Align the center of Camera towards a position in the world 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