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 sprogobalionas · Jan 15, 2021 at 02:43 PM · rotationmovementcamera movementroll a ball

How do I make it roll same speed, no matter what heights my orbit camera is looking at?

Any help will be supported


So, I have completed roll a ball tutorial, the game I created seemed a little too simple for me so I have added alot of additional things. One of those is mouse-controlled orbit camera, I coded the ball to roll same direction, as camera is looking. The problem is, that, if I look up or down, the ball rolls slower, because it's supposed to roll in ALL direcions (X, Y, Z).
How do I make it roll same speed, no matter what heights camera is looking at?
Here is my code:

     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         movement = Camera.main.transform.TransformDirection(moveHorizontal, 0.0f, moveVertical);
         Vector3 forcedmovement = new Vector3(movement.x * speed * Time.deltaTime, 0, movement.z * speed * Time.deltaTime);
         forcedmovement.z = Mathf.Clamp(forcedmovement.z, -100, 100);
         forcedmovement.x = Mathf.Clamp(forcedmovement.x, -100, 100);
         GetComponent<Rigidbody>().AddForce(forcedmovement.x - GetComponent<Rigidbody>().velocity.x/1, 0 , forcedmovement.z - GetComponent<Rigidbody>().velocity.z/1);
         Debug.Log("speed: " + GetComponent<Rigidbody>().velocity.x);
     }

How you can see, I have already worked on it, since the ball was even flying up, if I looked up, and also made the ball lose inertia over time.

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
3

Answer by jackmw94 · Jan 15, 2021 at 05:24 PM

It seems like the only part of the camera rotation you care about is the "yaw" or how it's rotated around the up axis. If you are rolling it according to the camera's full rotation you're either pushing it into the ground or fighting against gravity.

To achieve this, where you transform you movement using the camera transform, instead take the y element of the camera transform's euler angles then rotate movement around the up axis using this value.

Comment
Add comment · Show 5 · 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 sprogobalionas · Jan 18, 2021 at 05:46 AM 0
Share

I understand what i want, and imagine it, but I dont't really understand how do i make it, here you can see:

 movement = Camera.main.transform.TransformDirection(moveHorizontal, 0.0f, moveVertical);

In this line of code when converting camera's direction, I did it opposite, the Y rotation is changed to 0, X for W, S keys and Y for A, D keys. Could you show me, where exactly I'm doing mistake? Also as I told, I did much from tutorial, it should reveal, that I'm rookie on unity, I don't really understand what is EulerAngles, Quaternions and etc.

avatar image jackmw94 sprogobalionas · Jan 20, 2021 at 12:03 PM 1
Share

https://ibb.co/1v4xGFR
The gizmos (green/blue/red arrowy thing) in this image show the camera's rotation. When you transform the movement direction into the camera transform's "space" then the horizontal movement will be in the direction of the red arrow and what you've named vertical movement will be in the direction of the blue arrow.

This will work fine for your horizontal movement but your vertical / forward movement will push into the ground and thus the resultant forward movement of your ball will be less than you expect. The actual movement you get is only the global forward component of your movement vector.

https://ibb.co/WvhsV4R
Forgive the bad editing but this image (should) show that when you push into the ground, you're only ending up with a portion of that vector acting as your forward movement. This effect will increase as you increase the pitch at which you look at the object.

Untested code warning! but I expect the way of rotating your movement will look something like this:

 public void GetRotatedInput(float moveHorizontal, float moveVertical)
 {
     var cameraTransform = Camera.main.transform;
     var cameraYaw = cameraTransform.eulerAngles.y;
     var unrotated$$anonymous$$ovement = new Vector3(moveHorizontal, 0f, moveVertical);
     var rotated$$anonymous$$ovement = Quaternion.AngleAxis(cameraYaw, Vector3.up);
     return rotated$$anonymous$$ovement;
 }



Debugging vectors is really hard. With most code you can look at the data and figure out what's going wrong but looking at -1.525f, 51.21f, -12.015 and knowing what that means isn't something us humans do all that well. It's because of this I strongly advise that you use the monobehaviour function called "OnDrawGizmos" to show the directions and positions that your vectors are referring to when you try figure out why they're all going wrong. In this function you can use unity's Gizmos library to do calls such as Gizmos.DrawCube(position, Vector3.one * 0.15f); to show a 15cm cube in scene view at the vector position, or Gizmos.DrawLine(transform.position, transform.position + movementVector); to draw a line from your transform's position in the direction of a movement vector.

I hope this makes sense, although if it doesn't it's not you and it is hard!

avatar image jackmw94 jackmw94 · Jan 20, 2021 at 12:12 PM 1
Share

The images aren't showing for me so here are copy/pastable links

First image: https://ibb.co/1v4xGFR
Second image: https://ibb.co/WvhsV4R

Show more comments
avatar image sprogobalionas sprogobalionas · Feb 22, 2021 at 07:28 AM 0
Share

I tried something like this but i didn't understand the part with "...$$anonymous...". Could you tell, how exactly I should change my code?

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

198 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

Related Questions

Rotate the camera around a gameObject with a dynamic up value. 0 Answers

transform.rotation cancels movement. 1 Answer

Top down Shooter upper body look at mouse pos 1 Answer

Player model rotates forward with camera 0 Answers

click to move workig script!! but pls help with 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