Unity c# transform Function - c#

I am new to unity I am learning to make a driving game and i am following a course from udemy, so far thigns seems to be nice but what they didnt explain is why unity takes Floating points for its transform functions such as transform.Rotate() transform.Translate(). and also how do we calculate our value for smooth motion and what data type it pretend if we do not use f with number for example (transform.Rotate(0,0,45))<- this function make the sprite rotate in z axis super fast while z=0.1f makes it smooth in in real life 0.1f is equal to what value for us humans to understand? and how it unity reads and understands it?
Tried Values as Floating and without but couldnt understand difference also want to udnerstand how unity reads it and interpret Floating values
whats the difference between:
float steerSpeed = 0.1f;
update() {
// difference between below
float steerAmount = Input.GetAxis(Horizontal) * steerSpeed;
// and this
float steerAmount = Input.GetAxis(Horizontal)
transform.Rotate(0, 0, steerAmount);
}

In C# (and some of the other programming languages), the f suffix means float.
For example, float x = 0.1f means the value of x is 0.1.
So, if you set the value of z in transform.Rotate to 0.1f, it rotates slower than z = 45.

Related

how does Unity implements Vector3.Slerp exactly?

For my research I need to know, how exactly Unity implements the Slerp function for Vector3.
The Unity3D documentation descripes, that the input vectors are treated as directions rather than points in space. But it doesn´t explain, if there are quaternions used intern.
The Unity3D-C#-Reference mentionied Vector3.Slerp here:
[FreeFunction("VectorScripting::Slerp", IsThreadSafe = true)] extern public static Vector3 Slerp(Vector3 a, Vector3 b, float t);
However, I cannot find the definition anywhere. I think it´s a C++ Reference. Unity's C++-Code is only available with a licence (as far as I know).
Can someone help me determine this question? All I need to know is if Unity3D internally uses Quaternions for Vector3.Slerp(Vector3, Vector3, float).
Thank you in advance for your help.
I'm of course not sure because we don't have the source code for these internal methods but I'm pretty sure they would not use Quaternion which would be pretty imperformant but rather use pure and simple float based math like sinus, cosinus etc something that in c# would look somewhat similar to e.g. the solution mentioned here
Vector3 Slerp(Vector3 start, Vector3 end, float percent)
{
// Dot product - the cosine of the angle between 2 vectors.
float dot = Vector3.Dot(start, end);
// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point
// precision can be a fickle mistress.
Mathf.Clamp(dot, -1.0f, 1.0f);
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float theta = Mathf.Acos(dot) * percent;
Vector3 RelativeVec = end - start * dot;
RelativeVec.Normalize();
// Orthonormal basis
// The final result.
return ((start*Mathf.Cos(theta)) + (RelativeVec * Mathf.Sin(theta)));
}
Though theirs are of course in the underlying c++ environment and won't use Mathf and therefore should be a bit better in performance.

Unity increasing the scale of a GameObject not working?

GameObject:
I have a gameObject "Sphere" with the following properties:
Starting scale of 1.5 (x, y, z).
A script that makes sure that the scale is between 0 and 150.
What do I have:
Now, I have implemented a function that the user can scale the GameObject by using the HTC Vive Controllers (we are using Virtual Reality).
This function checks the distance between the controllers (often between -1 and 1 to decide if we want to upscale or downscale the object).
So when I have this value between -1 and 1, I am scaling the GameObject by the value multiple the sensitivity (this is editable in the Unity Editor).
What do I want:
This works pretty fine, although, I want to increase the sensitivity over time on a not hard-coded way. So when the GameObject is very small, the scaling will be very slow. When the GameObject is pretty big, the scaling will go quick.
What have I tried:
I have this value (between -1 and 1), then I will multiply this value with the sensitivity.
Then I will multiply by the current scale / the maximum scale.
However, this is causing an issue that the zooming in is going faster then zooming out.
The code that I am using looks like below:
float currentControllerDistance = Vector3.Distance(LeftHand.transform.position, RightHand.transform.position);
float currentZoomAmount = currentControllerDistance - ControllersStartPostionDifference; // Value is between -1 and 1.
currentZoomAmount = currentZoomAmount * ScalingSensitivity; // Multiplying by the value in the Unity Editor.
float currentPercentage = ObjectToScale.transform.localScale.x / ObjectMaximumScale.x; // Current scale percentage in comparison to the maximum scale.
currentZoomAmount = currentZoomAmount * currentPercentage; // Changing the ObjectToScale by adding the currentZoomAmount.
ObjectToScale.transform.localScale = new Vector3(ObjectCurrentScale.x + currentZoomAmount, ObjectCurrentScale.y + currentZoomAmount, ObjectCurrentScale.z + currentZoomAmount);
Does someone have any idea how to do this kind of scaling?
Thanks in forward.
If I understood the question correctly, you're looking for way to specify the rate of change of your scaling so that it changes faster when closer to the maximum scale, which sounds like a job for an easing function.
If your project already uses a tweening library like DOTween, this should be easily done with that library's capabilities. If not, you can try using the equation for the cubic bézier, which is one of the simpler curves:
Cubic Bézier
This is simply y = x^3, so you can try ObjectMaximumScale.x * currentPercentage * currentPercentage * currentPercentage to get a value that goes from 0 to ObjectMaximumScale.x when fed a value between 0 and 1 respectively.

Unity rotate object depending on other objects angle

I have two Gameobjects. The basic Question:
"When I spin circle 1, I want to spin circle 2 in the same way manipulated by factor x"
How do I sync the rotation around each of their local axis of circle 2 with the interactable rotation of circle one and scale that rotation by factor x? Setting the transform.right equal doesnt work, there are still to many degrees of freedom.
(Local Axis, because I want one or both gameobjects to be also tilted, but unrelated to one another.)
Trying math with rotational matrices didnt really work out based on the fact of them being evaluated every frame and thereby spinning Object 2 for eternity.
Thanks so much!
Assuming you rotate only around one single axis (as shown in the images) you can get the rotation difference in degrees of circle2 using e.g. Quaternion.Angle every frame
private Quaternion lastCircle2Rot;
//...
float rotDelta = Quaternion.Angle(lastCircle2Rot, circle2.transform.rotation);
lastCircle2Rot = circle2.transform.rotation;
than turn the circle1 accordignly using e.g. Transform.RotateAround
public float multiplier;
// e.g. rotate around local x = right
Vector3 YourAxis = circle1.transform.right;
circle1.transform.RotateAround(Vector3.zero, YourAxis, rotDelta * multiplier);
if using e.g. the right or another "clean" vector you could also simply use
circle1.transform.Rotate(Vector3.right, rotDelta * multiplier);
as it is done in local space by default.

Projectile Trajectory : Reaching specific coordinates

I am trying to implement a function in my game which will auto-lock a target and throw a projectile so that it lands perfectly on it.
I did the maths to calculate a parabola from Player 1 -> Target wherever their positions are but realised I wanted to use Unity's physics system rather than having the ball follow a path.
The throw velocity is constant, Player 1 and Target are moving objects but their positions will be registered once only to calculate the initial angle of the throw.
I believe this is the formula I need to use:
But how can I apply it for my Player and Target both having 3D coordinates?
Here is the pseudo-code of what I tried to write in Unity to make more easily readable.
float velocity = 100f;
float g = Physics.gravity;
Transform x = Target.position.x - Player.position.x;
Transform y = Target.position.z - Player.position.z;
double theta;
theta = **big formula using the values above**
And after that I do not know how to use this value to add force to the projectile.
I wanted to use AddForce(x,y,z, ForceMode.Impulse); but I clearly cannot use an initial angle here, only an x and y value.
Using RigidBody.velocity = Vector3(vx, vy, vz); gives me the same problem.
I believe I am missing something trivial but I really am stuck on this. Would anyone be able to help?

How do I account for gravity using a wiimote's accelerometer?

For a project my team and I have been trying to track a wiimote in a 3D space using the built in accelerometer and the WiiMotion Plus gyroscope.
We’ve been able to track the rotation and position using an ODE (Found at http://www.alglib.net/,) but we’ve run into a problem with removing the gravity component from the accelerometer.
We looked at Accelerometer gravity components which had the formula (implemented in C# / XNA)
private Vector3 RemoveGravityFactor(Vector3 accel)
{
float g = -1f;
float pitchAngle = (Rotation.Z);
float rollAngle = (Rotation.Y);
float yawAngle = (Rotation.X);
float x = (float)(g * Math.Sin(pitchAngle));
float y = (float)(-g * Math.Cos(pitchAngle) * Math.Sin(rollAngle));
float z = (float)(-g * Math.Cos(pitchAngle) * Math.Cos(rollAngle));
Vector3 offset = new Vector3(x, y, z);
accel = accel - offset;
return accel;
}
But it doesn’t work at all. As a reference, the acceleration is straight from the accelerometer, and the rotation is measured in radians after it has been worked through the ODE.
Also, We are having problems with understanding how this formula works. Due to the fact that our tracking is taking into account all dimensions, why is Yaw not taken into account?
Thanks in advance for any advice or help that is offered.
EDIT:
After discussing it with my teammates and boss, we've come to find that this formula would actually work if we were using X, Y, and Z correctly. We've come to another stump though.
The problem that we're having is that the Wiimote library that we're using returns relative rotational values based on the gyroscope movement. In otherwords, if the buttons are facing up, rotating the wiimote left and right is yaw and if the buttons are facing toward you, yaw is the same when it SHOULD be the rotation of the entire wiimote.
We've found that Euler angles may be our answer, but we're unsure how to use them appropriately. If there is any input on this new development or any other suggestions please give them.
I'd bet that your accelerometer was not calibrated in zero gravity, so removing the effect of gravity will be difficult, at the very least.
First I'd suggest not using individual components to store the rotation (gimbal lock), a matrix would work better. calibrate by holding it still and measuring (it will be 1g downward). then for each rotation, multiple the rotation matrix by it. then you can tell which way is up and subtract a matrix of 1g down from the vector representing the acceleration. I know that doesn't make a lot of sense but I'm in a bit of a rush, add comments if you have questions.

Categories

Resources