- Home /
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.
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.
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.
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.
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!
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
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
![](https://koobas.hobune.stream/wayback/20220613023336im_/https://answers.unity.com/themes/thub/images/avi.jpg)