Are eulerAngles reliably to use for local rotation limits? - unity3d

I want to allow the camera a certain local rotation (orbit around the player).
To do that, I calculate a rotation first, then I look at the EulerAngles, and then I state something like this:
if ((rotation.eulerAngles.y < 250) && (rotation.eulerAngles.y > 30))
{
//apply rotation
}
I would like to ask if using eulerAngles this way / for this purpose is safe or if there is a better method to limit a local rotation?
I'm asking because somebody said that "eulerAngles are ONE expression of a many possible eulerAngles expressions for a certain rotation." (I hope I got that right). That made me wonder if they are reliable.
Thank you!

Related

Stuck Implementing A Way To Rotate A Rigidbody Towards A Point With Rigidbody.AddTorque

So I am kinda stuck on what to do now because as I said I am trying to use Rigidbody.AddTorque to rotate a rigid body towards a certain point, which I was going to use to align a player with a gravitational pull so they can be upright. I have got the input part of the code, I just don't have a way to rotate the player to align with it, without violating the laws of physics with Quaternion.FromToRotation and messing with my character controller too, which I am trying to make entirely physics based rotation wise too to avoid any other problems.
I have experimented with a couple of methods, first I tried adapting my character controller code which used Rigidbody.AddForce to move the player and also dampening unwanted movements, as Rigidbody.AddTorque is basically Rigidbody.AddForce but for rotations, however, it was too weak and just flopping around when I tried it, here's the code for the character controller for calculating the force needed for one axes:
if(projected_speed.x*speed == relative_v.x)
{
applied_speed.x = 0f;
}
else if(Mathf.Sign(projected_speed.x)== -1)
{
applied_speed.x = relative_v.x - Mathf.Abs(projected_speed.x*speed);
}
else if (Mathf.Sign(projected_speed.x) == 1)
{
applied_speed.x = projected_speed.x*speed - relative_v.x;
}
Where projected_speed is the speed the controller wants to be at,relative_v is the relative velocity, and applied_speed is the speed that will be actually applied in Rigidbody.AddForce.
Anyways so maybe I didn't use enough force, as the player is under a gravitational pull, but that would have probably made it flip out or something, anyways so the second thing I tried was a PID controller, and I managed to find a page which explains it pretty well, sadly don't have the link anymore, but when I tried this because you have to tune it, I was stuck doing it, and it just wasn't able to do anything and was just rolling around the floor and sometimes spinning, probably as it couldn't cope with gravity, so that didn't work, so does anyone know how I could finally do this and make my character able to right themselves according to gravity?
Sorry for not indicating that my issue is solved, click here for the answer, credit to Ruzihm.

Dynamically control Unity Cinemachine vCams Blends: HOW?

I can't find how to constantly dynamically blend between 3 cameras (I call them middle, upper & lower) based on the rate and height of the hero, constantly.
When following the hero, the middle vCam is the main/base one, I'd like to proportionally blend through upper and lower vCams based on the height of the hero.
The player/hero can move rapidly between heights, so the blend should be weighted with eases. Which is a natural part of Cinemachine blends. And works. But it's like a switch, to me, at my current understanding of Cinemachine blends, rather than a constant dynamic blending based on height.
You may consider removing the upper and lower camera and do your own "Manual blending" with only the middle camera. Recently I've been using Cinemachine and I do something similar of what your desired result is.
Since I don't exactly know how do you want your camera to behave, I show you some of my manual blending I've done, explained:
//Camera Direction
//If I´m not in ground, and I've been on the air for a specific time
if (!onGround && timeSinceLastJump > cameraDelay)
{
//Moves the offset of the camera down to the maximum allowed y offset (In your case it would be where the lower camera is)
if (vcam.GetCinemachineComponent<CinemachineTransposer>().m_FollowOffset.y >= maxYOffset)
vcam.GetCinemachineComponent<CinemachineTransposer>().m_FollowOffset.y -= offsetYSensivity;
//It also zooms out up to a specified level
if (vcam.m_Lens.OrthographicSize < maxFOV)
vcam.m_Lens.OrthographicSize += camSensivity;
}
else
{
//Same but upwards
if (vcam.GetCinemachineComponent<CinemachineTransposer>().m_FollowOffset.y <= minYOffset)
vcam.GetCinemachineComponent<CinemachineTransposer>().m_FollowOffset.y += offsetYSensivity;
//Same but zooming in
if (vcam.m_Lens.OrthographicSize > minFOV)
vcam.m_Lens.OrthographicSize -= camSensivity;
}
This way you can use your player height in the conditions, at the cost of having to design well the camera logic.
Maybe this helps you in some way to do what you want.
From what I remember, you can define the blend style in the Cinemachine blend options. From the description, it seems that it is set to "Cut" when you probably want something similar to EaseIn/EaseOut. It even allows you to define your own custom blend between cameras if the default options do not work for you.
Take a look at the documentation for more details.

Best way to move a game object in Unity 3D

I'm going through a few different Unity tutorials and the way a game object is moved around in each is a little different.
What are the pros/cons to each of these methods and which is preferred for a first person RPG?
// Here I use MovePosition function on the rigid body of this component
Rigidbody.MovePosition(m_Rigidbody.position + movement);
//Here I apply force to the rigid body and am able to choose force mode
Rigidbody.AddForce(15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
// Here I directly change a transforms position value, in this case the cam
Transform.transform.position = playerTransform.position + cameraOffset;
Thanks!!
EDIT;
Something I have noticed is that the applied force seems to memic wheeled vehicles while the position changes memic walking/running.
RigidBodies and Velocities/Physics
The only time, I personally have used the rigidbodys system was when implementing my own boids (flocking behaviour) as you need to calculate a few separate vectors and apply them all to the unit.
Rigidbody.MovePosition(m_Rigidbody.position + movement);
This calculates a movement vector towards a target for you using the physics system, so the object's velocity and movement can still be affected by drag, angular drag and so on.
This particular function is a wrapper around Rigidbody.AddForce I believe.
Pros :
Good if realistic physical reactions is something you are going for
Cons:
A bit unwieldy to use if all you are trying to achieve is moving a object from point A to point B.
Sometimes an errant setting set too high somewhere (for example: Mass > 10000000) can cause really screwy bugs in behaviour that can be quite a pain to pin down and mitigate.
Notes: Rigidbodies when colliding with another Rigidbody would bounce from each other depending on physics settings.
They are also affected by gravity. Basically they try to mimic real life objects but it can be sometimes difficult to tame the objects and make them do exactly what you want.
And Rigidbody.AddForce is basically the same as above except you calculate the vector yourself.
So for example to get a vector towards a target you would do
Vector3 target = target.position - myPosition;
Rigidbody.AddForce(target * 15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
If you don't plan on having any major physics mechanics in your game, I would suggest moving by interpolating the objects position.
As it is far easier to get things to behave how you want, unless of course you are going for physical realism!
Interpolating the units position
Pros :
Perhaps a little strange to understand at first but far simpler to make objects move how you want
Cons:
If you wanted realistic reactions to objects impacting you'd have to do a lot of the work yourself. But sometimes this is preferable to using a physics system then trying, as I've said earlier to tame it.
You would use the technique in say a Pokemon game, you don't stop in Pokemon and wait for ash to stop skidding or hit a wall and bounce uncontrollably backwards.
This particular function is setting the objects position like teleporting but you can also use this to move the character smoothly to a position. I suggest looking up 'tweens' for smoothly interpolating between variables.
//change the characters x by + 1 every tick,
Transform.transform.position.x += 1f;
Rigidbody.MovePosition(m_Rigidbody.position + movement);
From the docs:
If Rigidbody interpolation is enabled on the Rigidbody, calling Rigidbody.MovePosition results in a smooth transition between the two positions in any intermediate frames rendered. This should be used if you want to continuously move a rigidbody in each FixedUpdate.
https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
Rigidbody.AddForce(15 * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
This will make the object accelerate, so it won't travel at a constant velocity (this is because of Newton's second law, Force=mass*acceleration). Also if you have another force going in the opposite direction this force could get cancelled out and the object won't move at all.
Transform.transform.position = playerTransform.position + cameraOffset;
This will teleport the object. No smooth transition, no interaction with any forces already in the game, just an instant change in position.

Smoothing SKCameraNode motion using constraints

Question Is there a way to use constraints to smooth the motion of the camera?
Detail I recently read a post about smoothing camera motion using linear interpolation (lerp) and it made me wonder if I could achieve a similar effect using constraints.
One promising option I saw in the documentation was distance(_: SKRange, toNode: SKNode) but after some experimentation, the results aren't at all what I was hoping for. I've tried using a constant value and the upper limit/lower limit range without success.
Here is the code that keeps the camera centered on a sprite:
let cameraRange = SKRange(constantValue: 0.0)
let heroLocationConstraint = SKConstraint.distance(cameraRange, toNode: hero)
I can imagine a set of constraints that would have to be enabled/disabled based on the player's y position, but that hardly seems like the best way to go. What would be really great is a node tracking variable that implements lerp automatically!

Get the position behind an object (locally) ? - Unity3D

I ran into a problem while making my complex(ish) camera behaviour (using Javascript). The problem that i have run to is that i am not able to get the position behind my player. Please don't tell me to use Vector3.back etc.. , because i want the position locally, to support turning. It won't work if the camera is always set to that position, cos i have a cool movement system in place.
I tried a number of approaches, and confused myself with the math. I also tried complex addition and subtraction. None of them worked at all.
I guess i am probably missing something quite simple, like a way to get into local coordinates etc.. ( or maybe a few math functions )
thanks in advance
-Etaash
You can get the forward vector of any transform, and if you negate that it is the backward vector. So on a script attached to the player you would have:
Vector3 backward = -transform.forward;
So to get a position, you could do something like this:
Vector3 pos = transform.position + (backward * dist); // where dist is a float to scale how far away from the position you want it