How can I modify a rotated transfom's position - c#

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.

Related

Getting rotation values ​of another object in Unity

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

Correctly pointing a GameObects rotation to the mouse position?

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.

Unity Mouse getaxis issue

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).

Spaceship: Rolling Sideways

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.

xna make an object move around ITS axis(not around point 0,0,0)

How can I make an object rotate around its axis? i.e. having the moon rotating BOTH around point 0, 0, 0 and its own axis? So far I have only been able to do the point 0, 0, 0 point by using the gametime component and creating a rotation matrix.
Transpose the object's center to (0,0,0), do the rotation, and transpose back.
Let's say we have the following:
class 2DMoon
{
Texture2D texture;
Vector2 axis;
Vector2 origin;
}
The origin point could be (0,0), but let's say it's something more complex--something like (29,43). Now let's say the texture's width is 50 and the height is 90.
To get the axis for the texture for it to rotate around, assuming you want the center, you would do the following (assuming the origin (ie. current position) and texture are loaded):
axis.X = (.5 * texture.Width);
axis.Y = (.5 * texture.Height);
As you know, that would take make the axis a vector of (25,45).
As BlueRaja states above, you could then make a method that looks like this:
Rotate()
{
origin.X -= axis.X;
origin.Y -= axis.Y;
// rotation goes here
origin.X += axis.X;
origin.Y += axis.y;
}
This should work for any sort of standard texture. (And of course, you don't HAVE to have the Vector2 I made up called "axis"--it's just for easy reference.
Now, take the same logic and apply it for the 3D.
A word of advice: if you are trying to work through logic in 3D, look at the logic in 2D first. 9 times out of 10, you'll find the answer you're looking for!
(If I made any mistake during the transposition in my Rotate() method, please let me know--I'm sort of tired where I'm at, and I'm not testing it, but the rotation should work like that, no?)

Categories

Resources