I am trying to make a space game, but I don't know how to rotate my spacecraft to a specific point in vector3 whith addtorque.
For example, to kill the velocity, my current script to calculate the trajectory is this
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class CalculateTrajectory : MonoBehaviour
{
Vector3 Direction;
// Start is called before the first frame update
void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
Rigidbody rb = GetComponent<Rigidbody>();
Vector3 trajectory = rb.velocity; // Velocity of gameObject (Vector3)
Vector3 NextPosition = transform.position + trajectory;
Vector3 Direction = (NextPosition - transform.position) * 1000;
Debug.DrawRay(transform.position, Direction, Color.green, 0.2f);
}
}
(sorry for my bad English I'm a 14 year old student from Germany)
You need to find the axis of rotation to get from the current heading (eg, transform.forward to the desired heading (rb.velocity if I correctly understand your goal). This can be found using the cross product:
// Note: will be scaled by the sine of the angle between the two vectors
var axis = Vector3.Cross (transform.forward, rb.velocity.normalized);
This can then be scaled by the desired torque which is then applied to the rigid body via AddTorque to give a proportional feedback. That is, this acts much like a spring. However, this will get weaker for angles > 90 degrees (see below)
var torque = axis * torqueFactor;
You might want to factor in the rigid body's angular velocity to include some damping. This can be done by subtracting the rigid body's angular velocity vector (scaled by some factor).
torque -= rb.angularVelocity * dampingFactor;
Note that finding good values for torqueFactor and dampingFactor can take some tweaking, and depends on the rigid body's moment of inertia (combination of mass and shape).
The problem with angles > 90 degrees:
When the angle is between 90 degrees and 180, the torque will drop off again as the angle approaches 180 degrees. This can be handled by computing the sine of the half angle, which can be done directly from the vectors:
// returns the sin(half angle between a and b).
// Loses the sign (thus direction) of the angle.
float HalfSin(Vector3 a, Vector3 b)
{
a = a.normalized;
b = b.normalized;
// (a - b).magnitude gives 2*sin(angle/2)
// using + will give 2*cos(angle/2)
return (a - b).magnitude * 0.5f;
}
This can then be used to scale the normalized result of the cross product of the two angles to get both direction and magnitude of the half-angle, and will be 0 at 0 degrees and 1 at 180 degrees (or would be if not for issues with normalizing the 0-vectors resulting from the cross product of parallel vectors).
Using the above ideas, this is my replacement for unity's FromToRotation (because it falls apart for very small angles due the normalization problem):
Quaternion fromtorot(Vector3 a, Vector3 b)
{
float ma = a.magnitude;
float mb = b.magnitude;
Vector3 mb_a = mb * a;
Vector3 ma_b = ma * b;
float den = 2 * ma * mb;
float mba_mab = (mb_a + ma_b).magnitude;
float c = mba_mab / den; // cosine of half angle
// find the rotation axis scaled by the sine of the half angle (s)
// using |a x b| = sin(angle) |a| |b| = 2 c s |a| |b|
// where c and s are the cosine and sine of the half hangle
// and mba_mab is 2 c |a| |b|
// c is not 0 until 180 degrees (vectors are anti-parallel)
Vector3 v = Vector3.Cross (a, b) / mba_mab;
return new Quaternion(v.x, v.y, v.z, c);
}
It behaves very well near 0 degrees, but does fall apart near 180 degrees, but that is to be expected: the axis of rotation is not defined. An undefined rotation axis is not a problem at 0 degrees, but is a very big problem at 180 degrees (try turning a book by 180 degrees about the X, Y and Z axes, or anything in between: very different results).
Related
I want to rotate a 2D game object on the z axis. That game object should only face the Player when he is in between 2 areas. The Z-axis should be clamped between -45 to 45 and 135 to 225.
Example Image of the rotations
(The green are is where the function: LookAtTarget() should be applied to)
Right now I'm using this code to bypass the problem, but it doesn't feel very satisfying:
public virtual void LookAtTarget()
{
float rotationZ = direction.transform.rotation.z;
direction.transform.right = target.position - transform.position;
if (rotationZ > -0.25F && rotationZ < 0.25F || rotationZ > 0.975F)
{
transform.right = target.position - transform.position;
}
Debug.Log(rotationZ);
}
I'm using this type of code because when I'm talking to eulerAngles I can't clamp the value between -45 and 45 and because -45 its like 315 degree for unity, so I can't go into the negative numbers. Also I'm declearing the rotation from another game object that should tell me when the Player is in the specific range.
I hope i could explain my problem. TY
Transform.rotation is the rotation of the transform expressed as a Quaternion. To get the rotation around the z-axis, use direction.transform.eulerAngles.z. Always use eulerAngles unless you understand Quaternions.
I cannot understand what your code is doing, but here is an idea to clamp rotation. This code is untested. The key is Mathf.DeltaAngle(a, b) which returns the shortest angle (range of [-180, 180]) from a to b, even if the angles wrap around.
float maxAngle = 45f;//insert your max/min angles
float minAngle = -45f;
float midAngle = (maxAngle + minAngle) / 2f;//convert max/min angles to a center angle and how far from the center angle
float angleRange = maxAngle - midAngle;
Vector2 targetRelativePos = target.position - transform.position;
float targetAngle = Mathf.Atan2(targetRelativePos.y, targetRelativePos.z) * Mathf.Rad2Deg;//make sure everything is in degrees
float targetAngleDifference = Mathf.DeltaAngle(midAngle, targetAngle);
if(Mathf.Abs(targetAngleDifference) > angleRange){
//outside of min/max angles
if(targetAngleDifference > 0){
//your angle is too big, consider setting it to maxAngle
}else{
//your angle is too small, consider setting it to minAngle
}
}
I'm making a game in a custom engine using C#. (Not Unity)
I've got a large grid and the x/y coordinates of two objects. The Player object and the Destination object. Along with the player's current rotation in degrees (0-360).
I've become too reliant on existing game engines and cannot work out how to find the rotation I need to put the player at to face the target.
playerRotation;//0 to 360 degrees.
playerX double = 47.43;
playerY double = 43.36;
targetX double = 52.15;
targetY double = 38.67;
My method at the moment is to try and get the distance between the objects by:
float distanceX = Math.Abs(playerX - destinationX);
float distanceY = Math.Abs(playerY - destinationY);
Which seems to work fine. Then I need to rotate the player to face the destination and have them move towards it until distanceX/Y are <= 0.
Edit: I've been messing with Atan2 to try and get the answer.
Vector2 playerCoords = new Vector2(playerX, playerY);
Vector2 targetCoords = new Vector2(targetX, targetY);
double theta = Math.Atan2((targetCoords.yValue - playerCoords.yValue), (targetCoords.xValue - playerCoords.xValue));
theta = theta * (180 / Math.PI);//Convert theta to degrees.
double sigma = playerRotation;//Direction in degrees the player is currently facing.
double omega = sigma - theta;
OutputLog("omega: " + omega);
My output log should be showing me the degrees my player needs to be facing to be facing the target. But it's giving me the wrong results.
Player: (4782, 4172) and
Target: (4749, 4157)
Angle should be about 286~.
But Theta = -155 and omega = 229.
Vector math can be very helpful, and it's not that complicated.
First vectors would be your player position and destination's:
Vector2 playerPos = new Vector2(playerX, playerY);
Vector2 destinationPos = new Vector2(destinationX, destinationY);
Now you can just subtract both vectors, to get a vector which points from one position to the other.
Vector2 delta = destination - playerPos; // Note, it might be the other way around: playerPos - destination
That delta vector has a length, and that is the distance between both points. There is usually a Length and a LengthSquared property available on the Vector class. Be aware however that calculating the length is quite CPU intensive because it uses a square root. If you want to compare that distance to a fixed distance like 200, just use the length squared and compare it to (200 * 200) which is way faster.
You can also use that delta, to let a bullet fly from one position to the other. You just need to normalize delta, there's a method for it, and you have it scaled down to length one. You can now use that delta, multiplied with a speed on each physics cycle to change the bullets position.
Now to get the angle, you can just use:
double angle = Math.Atan2 (delta.Y, delta.X); // Note that y and x are reversed here, and it should be like that.
Note that this angle is in radians, not degrees. A circle in radians starts at -PI and ends at PI. A full circle therefore is 2 * PI. To convert radians to degrees, you can see this question
Edit
I always assume that 12 o'clock is 0 degrees, 3 o'clock is 90, 6 o'clock is 180 and 9 o'clock is 270.
But actually in the cartesian coordinate system things are a bit different. I also made this false assumption in the code below.
But it turned out that I was wrong. See this picture
Now if you look at my sourcecode, all my variables are named incorrectly. If you look at their values however you can see that they match up with the picutre. Therefore the Atan2 correction is as it's supposed to be.
// Using Vector2 from System.Numerics;
internal static double RadianToDegree(double rad)
{
double thetaDegree = rad * (180.0 / Math.PI);
// Convert negative angles into positive ones
// https://stackoverflow.com/a/25725005/7671671
double thetaDegree2 = (thetaDegree + 360) % 360;
return thetaDegree2;
}
internal void Run()
{
// Player: (4782, 4172) and Target: (4749, 4157)
Vector2 player = new Vector2(4782, 4172);
Vector2 target = new Vector2(4749, 4157);
Vector2 delta = target - player;
double theta = Math.Atan2(delta.Y, delta.X);
double thetaDegree = RadianToDegree(theta);
// Given cartesian coordinate system
// positive y is up, negative is down
// positive x is right, negative is left
// Falsely assuming up is 0
// Falsely assuming right is 90
// Falsely assuming down is 180
// Falsely assuming left is 270
Vector2 v0 = new Vector2(0, 1);
Vector2 v45 = new Vector2(0.5f, 0.5f);
Vector2 v90 = new Vector2(0.5f, 0);
Vector2 v180 = new Vector2(0, -1);
Vector2 v270 = new Vector2(-1, 0);
double theta0 = Math.Atan2(v0.Y, v0.X);
double theta45 = Math.Atan2(v45.Y, v45.X);
double theta90 = Math.Atan2(v90.Y, v90.X);
double theta180 = Math.Atan2(v180.Y, v180.X);
double theta270 = Math.Atan2(v270.Y, v270.X);
double result0 = RadianToDegree(theta0);
double result45 = RadianToDegree(theta45);
double resultv90 = RadianToDegree(theta90);
double resultv180 = RadianToDegree(theta180);
double resultv270 = RadianToDegree(theta270);
// result 0 --> 90
// result 45 --> 45
// result 90 --> 0
// result 180 --> 270
// result 270 --> 180
}
I am new in Unity3D scripting especially when it comes to specifics of Transform, Quaternions, Vector3.
Down there is a script. When I roll the cube it flips in the right direction. However when cubes' local axis doesn't match the world axis the cube's endRotation is performed on the wrong axis.
Can anybody help me to resolve this issue : "endRotation" would flip the cube on right axis regardless of the relation between LOCAL axis <=> WorldAxis.
I was trying to resolve it for a week. Of cause no success.
Further down there code and video.
if (Input.GetAxis("Horizontal") > buttonDown)
{
StartCoroutine(FlipTheCube(Vector3.right));
return
}...
.
.
.
public IEnumerator FlipTheCube(Vector3 direction)
{
startFliping = false;
float rollStartTime = 0;
float rollAngle = 90;
float halfWidth = transform.localScale.z / 2;
Vector3 pointAround = transform.position + (Vector3.down * halfWidth) + (direction * halfWidth);
Vector3 rollAxis = Vector3.Cross(Vector3.up, direction);
Quaternion rotation = transform.rotation;
Quaternion **endRotation** = rotation * Quaternion.Euler(rollAxis * rollAngle);
Vector3 endPlacement = transform.position + direction;
float oldAngle = 0;
while (rollStartTime < rollDurtnTime)
{
yield return new WaitForEndOfFrame();
rollStartTime += Time.deltaTime;
float newAngle = (rollStartTime / rollDurtnTime) * rollAngle;
float rotateThrough = newAngle - oldAngle;
oldAngle = newAngle;
transform.RotateAround(pointAround, rollAxis, rotateThrough);
}
transform.position = endPlacement;
transform.rotation = **endRotation**;
startFliping = true;
}
Here is a link to the youtube video
First part of video If cubes local axis is matching world axis the "endRotation" is flipping correctly Second part of video as soon cubes local axis is not matching world axis "endRotation" goes wrong.
Your problems lie within these lines:
Vector3 rollAxis = Vector3.Cross(Vector3.up, direction);
Quaternion rotation = transform.rotation;
Quaternion endRotation = rotation * Quaternion.Euler(rollAxis * rollAngle);
// ...
transform.RotateAround(pointAround, rollAxis, rotateThrough);
First problem is Euler vs angle/axis usage.
If you've only been rotating against Vector3.right, Vector3.forward, and their negatives you might not have even noticed this yet.
In the first part, you use rollAxis as an Euler angle representation, and in the second part as an axis.
The problem is that for a rotation, its axis is usually not the same as a Euler representation of that rotation! For example, the rotation made by rotating around (0.7, 0.0, 0.7) by 90° is completely different from an Euler rotation of (63°, 0°, 63°)!
Instead, just use rollAxis consistently as an angle/axis representation. You can get the quaternion form using Quaternion.AngleAxis.
Second problem is global vs local rotation.
This is a problem whose effects you're definitely already noticing.
In the first part, you apply rollAxis as a local rotation. This is because rollAxis is the second term of the * operator.
In the second part, RotateAround rotates around a global axis defined by rollAxis.
Keep it global or local for both. Global is simpler, and it appears that's what you're trying to do (judging from transform.position + direction), so you should have rotation as the second term of the * operator.
Altogether, the problems can be fixed by just changing the Quaternion endRotation = rotation * Quaternion.Euler(rollAxis * rollAngle); line:
Vector3 rollAxis = Vector3.Cross(Vector3.up, direction);
Quaternion rotation = transform.rotation;
Quaternion endRotation = Quaternion.AngleAxis(rollAngle, rollAxis) * rotation;
// ...
transform.RotateAround(pointAround, rollAxis, rotateThrough);
See here for more information about the quaternion * operator.
I suggest you using DoTween asset (https://assetstore.unity.com/packages/tools/animation/dotween-hotween-v2-27676):
DORotate(Vector3 to, float duration);
Using tweeners will save you a lot of time writing your own animations.
So, here is what i have:
SteamVR's hand gameobject
3D sphere.
what i want:
The sphere to move to same direction/position as the hand does, but it to move further with a multiplier. E.g. i move the VR controller and the hand moves 1 unit. I want that the sphere moves to the same direction in a same amount of time but e.g. 2 units. how do i do this?
i tried simple
sphere.transform.position = ControllerToFollow.position +2f;
but then the sphere is always offset.
position is a Vector3, which is essentially 3 floats - you can't plus a Vector3 with a float unless you overload the + operator. Otherwise what you can do is the following:
Vector3 followPos = new Vector3(ControllerToFollow.position.x + 2f,
ControllerToFollow.position.y + 2f,
ControllerToFollow.position.z + 2f);
sphere.transform.position = followPos;
If you only want it to follow on one axis, then you can do the following:
Vector3 followPos = new Vector3(ControllerToFollow.position.x + 2f, // Follow on x Axis
ControllerToFollow.position.y, // Y axis is the same
ControllerToFollow.position.z); // X Axis is the same
sphere.transform.position = followPos;
Edit: I think I understand your problem better now. Here's a better version.
if (Vector3.Distance(sphere.transform.position, ControllerToFollow.position) >= 2f)
{
// Code that makes the sphere follow the controlling
}
Just track the movement Delta of the hand and multiply it by a certain multiplier.
At the beginning of the manipulation store
private Vector3 lastControllerPosition;
...
lastControllerPosition = ControllerToFollow.position;
then in every frame compare
var delta = ControllerToFollow.position - lastHandPosition;
// Don't forget to update lastControllerPosition for the next frame
lastControllerPosition = ControllerToFollow.position;
Now in delta you have a movement of the controller since the last frame. So you can assign it to the sphere with a multiplier using Transform.Translate
sphere.transform.Translate(delta * multiplier, Space.World);
or simply using
sphere.transform.position += delta * multiplier;
I'm developing a simulation where player should be able to move around inside a 2D circle (referred to as sphere in my code). The players movement must be relative to the center of the circle.
My first step was to make sure the player always faces the center. I got the working fine. However when I tried to do the relative movement it doesn't give my quite the result I'm looking for.
When I move the player close to the center of circle and move sideways (which is relative to the player's facing vector), the player spins around the center but then slowly starts spiraling outwards. The outwards spiral is much more prominent near the center and takes about 8 orbits to reach the inner edge of the circle. Instead the player should be spinning around the center at a constant distance from the center. Why does the player spiral outwards?
Here is the code I use:
// center of the sphere
Vector3 center = sphereComponent.transform.position - player.transform.position;
// always rotate towards the center so that transform.up is
float angle = Vector3.Angle(center, Vector3.up);
float sign = (center.x < rigidbody.transform.position.x) ? 1.0f : -1.0f;
rigidbody.MoveRotation(angle * sign);
// use the input vector to calculate a vector relative to the objects right and up vectors
Vector2 relativeInputVector =
(rigidbody.transform.right * player.InputVector.x) +
(rigidbody.transform.up * player.InputVector.y);
// below is same as doing: rigidbody += relativeInputVector.normalized * 20 * Time.deltaTime;
rigidbody.MovePosition(rigidbody.position + (relativeInputVector.normalized * 20 * Time.deltaTime));
So I've tried a few things already:
I thought it was maybe a rounding issue. So I rounded the relativeInputVector's X and Y to the 2nd decimal place. Didn't help.
I normalized the relativeInputVector vector. Didn't seem to do much...
I also thought maybe I should move and then rotate instead of rotate then move. Didn't work.
Now I'm thinking the issue is somewhere in the math (probably where I define relativeInputVector) but I can't find simular use cases regarding this so that I can compare and troubleshoot.
(this is a rather saturated topic when it comes to the keywords I'm search with)
Your intuition would make sense if you were moving to the side then adjusting the direction of your forward vector simultaneously and continuously, but it's being done alternating and discretely.
Consider what happens if Time.deltaTime was absolutely enormous for one frame. You would sidestep a huge amount, maybe even going off the screen in one direction, and then you would adjust your angle to face the center of the circle. That's an exaggerated example but its exactly what's happenening on a small scale.
Here's a diagram showing why your code spirals out:
The way you're doing it, The angle between the circle's radius to the player's position at the beginning of the frame (A in the diagram) and the direction the rigidbody moves (1->2 in the diagram) is a right angle. At position 1, the radius A might be the correct distance, but the hypotenuse of a right triangle is always longer than each leg, so the new radius at position 2 (B) must be larger, and likewise, C must be larger than B.
The result of that is a spiral motion as you continue to accumulate length to your radius by switching from legs to hypotenuses of these right triangles.
Basically, in order for your code to work, you would need to be making infinitely small triangles--Time.deltaTime would need to be infinitely small--as a right triangle with one infinitely small leg is just a line, its other leg and its hypotenuse are the same length.
Of course if Time.deltaTime were infinitely small, the player would never move. ;) So, a different approach is needed:
Instead, we can calculate the player's angular velocity and then move the player according to that.
So, dirst determine the player's new distance from the center first, then how many degrees the player would travel around the circle at that radius:
Vector3 sphereCenterPoint = sphereComponent.transform.position
Vector3 playerToCenter = sphereCenterPoint - player.transform.position;
float playerVerticalSpeed = 20f * player.InputVector.normalized.y;
newVerticalPosition = rigidbody.position + playerToCenter.normalized
* playerVerticalSpeed * Time.deltaTime;
playerToCenter = sphereComponent.transform.position - newVerticalPosition;
float circumferenceOfPlayerPath = 2f * playerToCenter.magnitude * Mathf.PI;
float playerHorizontalSpeed = 20f * player.InputVector.normalized.x;
float degreesTraveled = ( playerHorizontalSpeed * Time.deltaTime / circumferenceOfPlayerPath ) * 360f;
Then, rotate the player's new vertical position around the center point and set the player's rotation and position accordingly. You can use Quaternion.LookRotation to determine the rotation needed to make the rigidbody point forward/up in desired directions:
// rotates newVerticalPosition around sphereCenterPoint by degreesTraveled around z axis
Vector3 newPosition = Quaternion.Euler(0f,0f, degreesTraveled)
* (newVerticalPosition - sphereCenterPoint ) + sphereCenterPoint;
rigidbody.MovePosition(newPosition);
rigidbody.MoveRotation(
Quaternion.LookRotation(Vector3.forward, sphereCenterPoint - newPosition));
To remove a few calculations, you can include the part where you divide by 2 pi and multiply by 360f into the 20f factor:
Vector3 sphereCenterPoint = sphereComponent.transform.position
Vector3 playerToCenter = sphereCenterPoint - player.transform.position;
float playerVerticalSpeed = 20f * player.InputVector.normalized.y;
newVerticalPosition = rigidbody.position + playerToCenter.normalized
* playerVerticalSpeed * Time.deltaTime;
playerToCenter = sphereComponent.transform.position - newVerticalPosition;
float playerHorizontalSpeed = 1146f * player.InputVector.normalized.x;
float degreesTraveled = playerHorizontalSpeed * Time.deltaTime / playerToCenter.magnitude;
// rotates newVerticalPosition around sphereCenterPoint by degreesTraveled around z axis
Vector3 newPosition = Quaternion.Euler(0f,0f, degreesTraveled)
* (newVerticalPosition - sphereCenterPoint ) + sphereCenterPoint;
rigidbody.MovePosition(newPosition);
rigidbody.MoveRotation(
Quaternion.LookRotation(Vector3.forward, sphereCenterPoint - newPosition));