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 CristiBala24 · Jan 06, 2019 at 08:45 AM · shootingclampy-axisthird-person-camerathird-person-shooter

Y rotation of the Camera doesn't clamp

Hi there! So I am making a third person shooter and i have a small issue with my aiming controls. When I press the right click mouse button it goes into aiming as wanted, but for some reason the rotation on the Y axis of the camera doesnt clamp and i can rotate the camera 360 degrees which I don't want.

Here is the script:

 if (Input.GetKey(KeyCode.Mouse1))
         {
             if (isShooting == false)
             {
                 cameraTransform.rotation = Quaternion.Euler(0, lookAt.rotation.eulerAngles.y, 0);
             }
             isShooting = true;
             cameraTransform.position = shootPoint.position;
             mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
             cameraTransform.Rotate(mouseX * Vector3.up, Space.World);
             lookAt.Rotate(mouseX * Vector3.up);
             mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * -1f;
             mouseY = Mathf.Clamp(mouseY, -25f, 30f);
             cameraTransform.Rotate(mouseY * Vector3.right, Space.Self);
         }
         if (Input.GetKeyUp(KeyCode.Mouse1))
         {
             isShooting = false;
         }

NOTE: This is not the full script!

Comment
Add comment · Show 2
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 Ady_M · Jan 06, 2019 at 10:45 AM 0
Share

Your post is not is not displaying it as code. Insert a couple of line breaks, click on the code icon in the toolbar (101010) then paste your code.

avatar image CristiBala24 Ady_M · Jan 06, 2019 at 10:53 AM 0
Share

I just changed it. Hope you can help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ady_M · Jan 06, 2019 at 11:35 AM

First of all, you need to use clamp on the code that's working with Rotate (Vector.up) since that's Yaw, not the other one.

 

Secondly, your code is currently clamping how fast you can rotate, not how far.

 

On MouseButtonDown(1) (meaning only once) you need to save your current lookAt in a separate property of the script, for example lookAtOnAimStart, so you can later limit how far you can yaw.

 

I just noticed that you're rotating the camera directly without applying the value of lookAt to it. What are you using the lookAt variable for?

Comment
Add comment · Show 2 · 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 CristiBala24 · Jan 06, 2019 at 11:48 AM 0
Share

The lookat variable is for the player and as I said I am using it in the other part of the code which is irrelevant for my issue right now. I will try the thing you said and then I will give you updates. Thanks for the help :)!

avatar image Ady_M CristiBala24 · Jan 06, 2019 at 11:53 AM 0
Share

I see. Because I like the idea of managing your own lookAt so that you're not working only with the camera's transform.

 

You're welcome :) I can update my answer when you've done that. Happy coding.

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

99 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

Related Questions

How can i Limit the movement of character (x, y axis) on camera view 0 Answers

Won't Shoot if Y Rotation Value Is Negative 2 Answers

How can I destroy an enemy with two shots 1 Answer

Nullreference exception after build! 1 Answer

Problem with raycasting in direction of mouse 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