I have a script that just essentially rotates the x and the y axis of a camera.
void Update () {
float xRot = Input.GetAxis("Mouse X");
float yRot = Input.GetAxis("Mouse Y");
Vector3 rotate = new Vector3(xRot, yRot);
transform.Rotate(rotate);
}
It appears as the input from the Mouse X is the actually the Mouse Y and vice versa. I know I can fix the problem just by flipping the xRot and the yRot in the assignment of the Vecort3 variable. So I was wondering if there is a better way to get the x and y axis of the mouse or if I just accidentally modified my project some how. Also would appreciate it to know how to fix the problem.
This is not a problem, just how things are.
Left and right are the horizontal axis. Your first thought would be assign them to the x value. Problem is that the rotation rotates around the given axis and most likely left/right should be rotating the object towards the left or the right.
But to do so, you can think of a pole (like a pole dancer) and you'd be rotating around it. If you are real close to it, it'd be like rotating left-right. But this axis is going up, and it is the up vector.
It means that to rotate left-right, you rotate around the up vector but use the left-right buttons which are the horizontal axis.
Obviously, if you want to rotate so that you can look down-up, you will rotate around the right (x) axis and use the vertical movement.
To conclude, there is no more simple or more logic solution than inverting the input. You could change the input setting so that horizontal is vertical and vice-versa but I would not recommend it.
var pos = Input.mousePosition;
var x = pos.x;
var y = pos.y;
The bottom-left of the screen or window is at (0, 0). The top-right of the screen or window is at (Screen.width, Screen.height).
Related
I have a camera, it is attached to the capsule, and rotates independently of it.
But I need the capsule to rotate along the Y axis following the camera. How to get the Y-axis rotation values of the camera? Tried through transform.rotation.y. But it gives an error.
Transform.rotation returns a Quaternion (see also Wikipedia - Quaternion) - it has not 3 but 4 components x, y, z and w!
=> Unless you know exactly what you are doing - which is almost never the case ^^ - you do never want to touch any of its components directly.
Unfortunately also the Transform.eulerAngles are not really reliable for your use case.
So what I would do is rely on Vector3 instead.
// take camera's forward vector
private forward = yourCamera.transform.forward;
// erase the Y axis => vector is now only on the XZ plane rotating around Y
forward.y = 0;
// rotate the capsule so its forward vector aligns with "forward"
yourCapsule.transform.rotation = Quaternion.LookRotation(forward);
// could also do
//yourCapsule.transform.forward = forward;
see also Quaternion.LookRotation
Quaternion FromToRote = Quaternion.FromToRotation(Vector3.right, new Vector3(roadEnd.x, roadEnd.y, roadEnd.z) - road.position);
road.transform.rotation = FromToRote;
I have this piece of code handling my rotation. It rotates a GameObject to the position of my mouse, but the x axis rotates to -90 when moving towards the right.
https://gyazo.com/91fa0caee3f62b353b33e6a175e93e55
I tried to fix the x axis to 0, but that caused all the axes to default to 0. I'm not at all good with quaternions so I have no clue how to approach this.
I got a question about modifying a rotated transform's position. For example,
I have rotated the object shown in the picture around x axis. Then I want to move it along itsown y axis(The yellow arrow). For achieving this, I use the code like
transform.localPosition = new Vector3(transform.position.x, newY, transform.position.z);
where newY means the new y value I want to give to the object's transform.
However, the result turns out like
Apparently the object goes up around the world y axis.
So How can I let it go with its own y axis (The yellow arrow) ?
Add transform.up * distance_to_move to the object's position to move it along its own rotated Y-axis.
The reason is that the object shown in the picture doesn't have parent object. In this case, transform.localPosition is the same as transform.position.
You can calculate its own y axis's direction in the world axis, then move it. For example, You can use Transform.TransformDirection to transform direction from local space to world space.
Vector3 RelativeDirection = gameObject.transform.TransformDirection(0, 1, 0);
The RelativeDirection might is the direction you want.
I have a ship and I want the Ship to follow the mouse, it does that fine and dandy. When forward and backwards are pressed it goes towards and away from the mouse perfectly, but I can not figure out how to make the left and right buttons make the ship circle around the mouse in a clockwise/counterclockwise direction.
I have tried to take the ships location, and the mouse's location, creating a slope, and then getting the perpendicular to that slope, but that doesn't work either.
How can I achieve this? I do not think it needs code, more of an equation, but if there is code, please tell me.
You need the parametric form for the equation of a circle. Since you want it centered about the mouse's current location, you need an offset translation. Try something like:
float radius = 10f;
float shipX;
float shipY;
float angle = current_angle; // update this to animate
shipX = mouseX + ( radius * Math.Sin(angle));
shipY = mouseY + ( radius * Math.Cos(angle));
Actually, i'm programming a little spaceshooter game (2.5D, Topdown View). The player can move along the XZ Axies and rotate the spaceship via right ministick (gamepad) or look to the cursor position (keyboard + mouse).
So, the movement and rotation (Y-Axies, Yaw) are seperated.
The whole thing works fine and looks good - but now i want to do the following:
If the spaceship moves sideways, it should rotate around the X / Pitch axies / lean left and right a bit, dependent on the sideways speed.
So, i have to compute the sideways speed from the following, given input:
Velocity Vector (Movement on X and Z Axies, Y is always '0')
Direction Vector (Rotation on Y Axies, X and Z are always '0')
And with the amount of sideways speed, i could rotate my spaceship around the X axies and multiply the resulting quaternion by the rotation around the y axies.
Anyone who has a solution for this?
Solution: Just "rotate" the velocity vector by the heading of the spaceship and use the "roll/z" axis as the sideways rotation about the X axis (the axis, where your ships nose points towards):
Quaternion Rotation = Quaternion.Euler(0, mHeading.y, 0);
Vector3 RealVeloctiy = Quaternion.Euler(0, -mHeading.y, 0) * Velocity;
float Angle = RealVeloctiy.z * 2.5f;
Rotation = Rotation * Quaternion.Euler(Angle, 0, 0);
A couple of possibilities:
Compare the velocity vector with local left direction of the spaceship (make sure they're in the same coordinate space first). You could use the Vector3.Angle function, or a dot product to do the comparison. Scale the result appropriately, and apply a local rotation around the forward axis.
Take user input directly. If the user is strafing in a direction, apply a roll value. You could use a float between -1 and +1, along with a rate of change. If they're strafing left, move the value towards -1 at that rate, or if they're strafing left, to +1. If neither key is being pressed, move the value back towards 0. (You might like to play around with the Lerp, SmoothStep and SmoothDamp functions too). Scale the value to apply an appropriate rotation about the relevant axis.