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 /
  • Help Room /
avatar image
0
Question by Proponent · Jan 30, 2020 at 02:33 AM · raycastvector3quaternionmouseposition

Clamp vector values to a minimum circular radius around player?

I have been messing around with this for a couple days now and feel like there is probably a simple solution that is just not occurring to me.

Top-down shooter; I am using a main camera raycast to point a turret at the mouse cursor. However, I am trying to determine how best to limit the rotation of the turret along X and Z so that it will not point too far down when the mouse position is close to the player.

Image

Ideally, when the mouse cursor is within the minimum radius, I would like the turret to continue rotating around the Y-axis to "face" the mouse cursor, but for rotation along X and Z to stop at a minimum angle until the cursor is moved outside the minimum radius.

Am I correct in assuming that the turret direction vector needs to have its minimum values clamped to X and Z coordinates on a circle with radius R, with an origin of player X and player Z? If so, I am at a loss how to implement that logic.

If anyone has any idea how to go about this I would like to hear it.

 public class CrosshairRaycast : MonoBehaviour
 {
     public GameObject Player;
     
     [SerializeField]
     private Transform turret;
     public float turretTurnSpeed = 5f;
 
     Quaternion turretQuat = Quaternion.identity;
 
     void Start()
     {
         
     }
 
     void FixedUpdate()
     {
         //New ray from the camera origin to the crosshair position
         Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         //New raycast hit
         RaycastHit hitInfo;
 
         //If the raycast hits anything, store hit info "hitinfo" from cursor and run logic. Otherwise do nothing
         if (Physics.Raycast(rayOrigin, out hitInfo))
         {
 
 
             //If the raycast is hitting a collider, otherwise do nothing
             if (hitInfo.collider != null)
             {
                 //New vector "directionTarget" to determine the proper turret direction. Destination vector "hitinfo.point" minus source.
                 Vector3 directionTarget = hitInfo.point - turret.position;
 
                 turretQuat = Quaternion.LookRotation(directionTarget, Vector3.up);
                 
                 turret.rotation = Quaternion.Slerp(turret.rotation, turretQuat, (Time.deltaTime * turretTurnSpeed));
 
                 Debug.DrawRay(turret.position, directionTarget, Color.red);
             }
         }
     }
 }


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 kylecat6330 · Feb 05, 2020 at 07:19 AM

I'm not sure I completely understand the question, but it sounds like you are just asking where/how to clamp the x and z rotations of the turret, so it doesn't look too far down.

In which case you would first need to figure out what angles you want it at. Place some Debug.Logs to give you the directionTraget's x and z values.

  Debug.Log("X: " + directionTarget.x);
  Debug.Log("Z: " + directionTarget.z);

Once you know what you want your minimums and maximums to be you can clamp them and put them into directionTarget.

  Vector3 directionTarget = hitInfo.point - turret.position;

  directionTarget.x = Mathf.Clamp(directionTarget.x, xMin, xMax);
  directionTarget.z = Mathf.Clamp(directionTarget.z, zMin, zMax);

I hope that helps.

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

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

assigning several positions and call array of raycasts 0 Answers

My Ray is going the wrong way 1 Answer

Raycast under crosshair 0 Answers

Quaternion lerp slowing down movement? 1 Answer

Grabbing the Relative eulerAngles.y of a Rotation 1 Answer


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