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 oliver-jones · Nov 20, 2013 at 03:25 PM · raycastpositionclamp

Raycast - Clamp Math Issue

Hello,

I have a ball, in which the camera can rotate around. I have a cube that moves on the x axis of the ball depending where the centre of the screen is. Using a raycast from the camera, to an invisible plane where the ball is, I am able to move the cube along the X axis of the raycast, using a Clamp so the cube never leaves the surface of the ball.

The cube is used to show the user where force is going to be applied to the ball (like an angle shot in a game of pool/billiards). So its a visual aid.

alt text

The issue I'm having is that if I rotate my camera around the ball, the cube position is off (but the Raycast is still fine), so it must be a transform + maths issue I'm having. By 'off', I mean that if the ball is in the centre of the screen, the cube does not place itself central.

 var start = Camera.main.transform;
 var direction = start.TransformDirection(Vector3.forward);
 var centreHit : RaycastHit;
 
 if (Physics.Raycast (start.position, direction, centreHit, Mathf.Infinity, playerRay.value)) {
     
     Debug.DrawLine (start.position, centreHit.point, Color.yellow, 0.5);
 
     axisLock.x = Mathf.Clamp(centreHit.point.x - ball.position.x, -0.3, 0.3) + ball.position.x;
 
     forceHitVisual.position.x = axisLock.x;
 }



Further explanation

Below is a top down diagram showing my issue. Something I didn't say before is that my ball always rotates itself to face the camera. The camera is off shot, but all the raycasts you see are coming from it (The white lines are not raycasts, but the camera frame).

alt text

--- UPDATE ---

After some debugging - I came across that when I move the camera around on 0 degree rotatation (photo 1), the Z axis of the clamp ray is always the same (8.5), and the X axis changes depending where the centre is. But if I rotate the camera around the ball 90 degrees (same as in 3 and 4), both my X and Z axis start changing. How would I get it so that only my X axis affects the clamp ray, and Z stays as my depth?

pool.png (15.3 kB)
ex.jpg (260.6 kB)
Comment
Add comment · Show 9
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 meat5000 ♦ · Nov 20, 2013 at 04:28 PM 0
Share

Post a screener so we can see what your drawline does?

avatar image oliver-jones · Nov 20, 2013 at 05:22 PM 0
Share

Okay - please see my updated question. Thanks

avatar image meat5000 ♦ · Nov 20, 2013 at 05:31 PM 0
Share

The cube isn't a child of the Ball? This would probably make it easier to keep it relative.

As it works in 90 degree increments I'm guessing there is a problem somewhere with regards to Vector.dot or cross implementations. Or perhaps some angle function which is giving >360 problems. Do you rotate with eulerangles or quaternions?

avatar image oliver-jones · Nov 20, 2013 at 05:37 PM 0
Share

It doesn't use 90 degree increments, the camera rotation is based on the mouse input, so the angle could be anything. Which is why I need to this work, regardless of the angle. The cube isn't a child of the ball, the cube is just placed where the red raycast is hitting (point).

avatar image meat5000 ♦ · Nov 20, 2013 at 05:38 PM 0
Share

Sorry, I mean, according to the pics it appears to be ok when it hits each 90 degree increment

Show more comments

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

18 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

Related Questions

Setting the position of linerenderer 1 Answer

Just can't get this Player to move on the x axis? 1 Answer

Teleporting player to touch position and ray problem 3 Answers

How to shoot raycast with slight random direction? 1 Answer

Having an object point towards mouse position in 3d world 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