Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by notloop · Jan 23, 2019 at 07:04 AM · rotationvector3transform.positionoffsettranslation

Set transform.position relative to rotation

Forgive my title, but I tried to make it somewhat generic to help other people who may fall in front of the same problem.


The problem:

I'm creating a pool sim where I can currently rotate the cue around the cue ball and want to be able to move the cue stick sideways between the edges of the cue ball. The issue I'm having is calculating the cue's position relative to the cue ball when the cue is rotated. I've managed to get the cue to move between the two edges of the cue ball, but once I rotate the cue around the cue ball (using the left and right keys) and then set the spin the cue is positioned incorrectly.


Note: The side spin is defined as a Vector2 where -1 (is all the way to the left, 0 is aligned to the centre of the cue ball and 1 is the right side of the cue ball).


 private void Update()
 {
     float horizontal = Input.GetAxis("Horizontal");
 
     transform.RotateAround(m_cueBallTrans.position, Vector3.up, horizontal * 50f * Time.deltaTime);
 }

 // x = 1.0 right side
 // x = -1.0 left side
 public void SetSpin(Vector2 spin)
 {
     // Calculate the max distance the cue can move in each direction 
     // (essentially half the cue ball either side)
     float cueMaxSideDist = m_cueBallTrans.localScale.x * 0.5f;

     // Get the amout we want to move to the edge of the cue ball
     float cueSideAmount = cueMaxSideDist * Mathf.Clamp(spin.x, -1f, 1f);

     // Get the right dir and multiple by cue side amount and then add 0.8 (magic number) in 
     // the backwards direction of the cue transform
     transform.position = (transform.right.normalized * cueSideAmount) + 
                          (-transform.forward.normalized * 0.8f);
 }


I've added comments to the code to help with what I'm trying to achieve. I'm trying to use the transform right direction to offset the cue from the centre of the white ball to either side of the cue ball and then add the backwards transform direction of the cue to place it just behind the cue ball (see below). Additionally, I've avoided using transform.Translate(Vector3, Space.local) as I want to offset the cue with the values of -1 to 1.


alt text

screenshot-2019-01-22-at-215758.png (50.1 kB)
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 notloop · Jan 23, 2019 at 01:07 PM

     public void SetSpin(Vector2 spin)
     {
         // x = 1.0 right side
         // x = -1.0 left side
         // y = 1 top spin
         // y = -1 back spin
 
         float cueXSpinAmount = m_cueBallDistFromOriginToSide * Mathf.Clamp(spin.x, -1f, 1f);
         float cueYSpinAmount = Mathf.Clamp(spin.y, -1f, 1f);
 
         // Position cue behind the cue ball
         transform.position = m_cueBallTrans.position + (-transform.forward.normalized * 0.8f);
 
         // Reset y height in cue
         transform.position = new Vector3(transform.position.x, 0f, transform.position.z);
 
         // Translate the cue from the centre of the cue ball to the specified X spin amount
         transform.Translate(cueXSpinAmount, 0f, 0f);
 
         // Reset the X rotation of the cue
         transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y , transform.rotation.eulerAngles.z);
 
         // Rotate the cue in the x axis with the specified Y spin amount
         transform.Rotate(new Vector3(-cueYSpinAmount, 0f, 0f));
     }


I managed to get it working by resetting the cue each time I set the spin to the centre of the ball and then translate and rotate accordingly. I'm going to try and refactor to mitigate the need to reset the cue each time and not use transform.Translate and transform.Rotate and instead set the transform.position and transform.rotation directly.

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

214 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

Related Questions

Helicopter Move Local Position,Local Position 1 Answer

transform object to a spawnpoint position and rotation 0 Answers

Get angle between direction and plane 1 Answer

rotate an object with HTC Vive controller 0 Answers

Are I stupid? Vector3 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