Getting rotation values ​of another object in Unity - c#

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

Related

Quaternion.FromToRotation Issue with Hit.normal

I am spawning these 2 tyers on either side of the cube using Raycast hit.
tyer.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
But as you can see both the tyers have a different spin directions which is Positive Y axis in the Hinge Joint.
I want them to spin in same direction when attached in opposite sides.
If i use tyer.rotation = Quaternion.LookRotation(hit.normal); it works fine but on a different tyer where Z axis is pointing outwards.
How can I make it work with this tyer where Y axis is point outwards ?

How can I calculate the speed of a rotation unity 3d c#

so the thing is: i have a sensor that makes a rigidbody rotate, the problem is that i can't calculate the speed in which it was rotated (If the sensor was moved fast the object would also move fast but when it collides with another gameobject it won't apply the speed/force which with it has been rotated)
Is there any way I can calculate the speed/force in which it has recently rotated so that i can apply that speed/force to another object when it collides?
note: im not entirely sure if what i need is speed or force
float rot = float.Parse (sp.ReadLine());
transform.eulerAngles = new Vector3 (0, 90, rot);
note: because of how the object was designed it has to be rotated by default in 90 degrees. Basically what im doing is receiving information from a Gyroscope and puting it in the object rotation.
The sensor gives values from -90 to 90 in the form of Float, when it is rotated to the left it gives values from -90 to 0, when rotated to the right it gives values from 0 to 90. this values should be used to graphically rotate the object and also aply the speed in which it was rotated when it collides whith another gameobject (if it was moved fast from -90 to -20 for example it should apply more force than if it was moved slowly from -90 to -50 to -20)
When trying to cause believable physical interaction between objects, you never want to manipulate the transform directly. Instead, you should be making use of the various methods available to physically influence an attached Rigidbody.
To apply rotation in this case, you're going to want to use Rigidbody.AddTorque(). Here's an idea of how you might use it:
// Probably get this value in Start() and save it for later
Rigidbody rb = GetComponent<Rigidbody>();
float rot = float.Parse (sp.ReadLine());
// transform.forward is the unit vector corresponding to the local z-axis of the object
Vector3 rotationAxis = transform.forward * rot;
rb.AddTorque(rotationAxis);
Hope this helps! Let me know if you have any questions.

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

How can I modify a rotated transfom's position

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.

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.

Categories

Resources