#Quarternions
1 messages · Page 1 of 1 (latest)
1 sec I think I got it
!code
📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.
identity is just like vector(0,0,0)
might be pointless or counter-productive
no, identity is (0, 0, 0, 1)
euler angles might not be the best thing if you have a good vector to use either
like vector3.zero in concept
vectors help with understanding quaternions
In the end, quaternions are almost always used to rotate vectors
Quarternions
multiplying quaternions is same as adding vectors
or vectors are used to position quaternions
like how if you add movement left then movement up, you'd be at 1,1
quaternions' are just like vectors that are added up to end up with rotation
I would recommend you to place // in your code
all quaternions are essentially starting from 0 every time just like vectors. like how you add position to vector when moving, you add rotation to quaternion to actually rotate as intended
anyways yeah, it gets easier if you actually understand quaternions. trying to do anything without understanding them is a huge pain
@tiny mantle like at the beginning and end
what do you mean?
On lines that aren't actually code
Damn I really thought this would work. @vast sage I tried the multiplying part you recommended. The thing is, it seems the y rotation keeps resetting to 0 every frame. I can see it rotating for a split second before it goes back to its original rot
_objToPosition.transform.rotation = new Quaternion(Quaternion.FromToRotation(Vector3.up, hitInfo.normal).x, _objToPosition.transform.rotation.y, Quaternion.FromToRotation(Vector3.up, hitInfo.normal).z, 0);
_objToPosition.transform.position = Vector3.Lerp(_objToPosition.transform.position, hitInfo.point, Time.deltaTime * 15f);
float scrollValue = Mouse.current.scroll.y.ReadValue();
_objToPosition.transform.rotation *= Quaternion.Euler(0, scrollValue/10, 0);
ah
Like every second line in that paste
sorry for messy code
but I need the self-constructed quaternion
I removed the le0rps
your nod should begin with //
because I thought that might cause the weird stuff and it did
Still messing with .xyz components of Quaternion...
Don't touch those, forget they exist
hmm fine
It's not the same as a x, y, z angle in a Vector3 euler angle
@burnt tusk you're trying to rotate camera?
@pale marlin like that
// Create a new Quaternion object:
Quaternion rotation = Quaternion.identity;
// Set the rotation using Euler angles:
rotation = Quaternion.Euler(x, y, z);
// Apply the rotation to a game object:
transform.rotation = rotation;
// Alternatively, you can rotate the game object incrementally:
transform.rotation *= rotation;
// You can also interpolate between two rotations using the Quaternion.Lerp or Quaternion.Slerp functions:
Quaternion newRotation = Quaternion.Lerp(startRotation, endRotation, t);
// In addition to these basic steps, there are many other functions and properties available for working with quaternions in Unity, such as Quaternion.AngleAxis, Quaternion.LookRotation, and Quaternion.Inverse. It's a good idea to read up on the Unity documentation for more information on how to use quaternions effectively in your game.
Im making a building system where the player can freely place objects. THe object aligns with the terrain and should be rotatable around the y axis using the scroll wheel
yes
how did you do that
📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.
that's the answer then
up is terrain normal, use up as vector to rotate quaternion around...
don't remember what the code was, quaternion.rotatearound?
what
Pretty sure all you need is something like this @burnt tusk cs Quaternion rot = Quaternion.FromToRotation(Vector3.up, hitInfo.normal); rot = rot * Quaternion.Euler(0, scrollValue, 0); _objToPosition.transform.rotation = rot;
Align to ground, then rotate it with the scroll value, then apply it
Isn't that all?
hmm
or just use world up, idk
either way it's one of the simple use cases
idk for sure
Quaternion.Euler(0, scrollValue, 0) is using the Y axis
rotatearound doesn't need it though
Doesnt need what?
.euler
@vast sage
all he needs is to rotate around up vector
Which is literally what * Quaternion.Euler(0, scrollValue, 0) does
You realize there are dozens of ways to rotate an object
The full code```c
private void BuildMode()
{
_objToPosition.GetComponent<Renderer>().material = _buildingMatPositive;
if (_objToPosition == null || !RayHit(_buildModeLayerMask, out RaycastHit hitInfo)) return;
_objToPosition.transform.position = Vector3.Lerp(_objToPosition.transform.position, hitInfo.point, Time.deltaTime * 15f);
float scrollValue = Mouse.current.scroll.y.ReadValue();
Quaternion rot = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
rot = rot * Quaternion.Euler(0, scrollValue, 0);
_objToPosition.transform.rotation = rot;
Vector3 bounds = _objToPosition.GetComponent<Collider>().bounds.extents;
if (Physics.OverlapBox(_objToPosition.transform.position, bounds, Quaternion.FromToRotation(Vector3.up, hitInfo.normal), ~(1 << LayerMask.NameToLayer("Ground"))).Length > 1)
{
// Collides
_objToPosition.GetComponent<Renderer>().material = _buildingMatNegative;
return;
}
if (Vector3.Angle(hitInfo.normal, Vector3.up) > 20f)
{
_objToPosition.GetComponent<Renderer>().material = _buildingMatNegative;
Debug.Log(Vector3.Angle(hitInfo.normal, Vector3.up));
return;
}
if (Mouse.current.leftButton.wasPressedThisFrame) Instantiate(_objToPosition, hitInfo.point, _objToPosition.transform.rotation);
}```
It looks like something keeps resetting the rot
But nothing is
afaik
@burnt tusk you're setting rotation instead of adding it
if your doing rotations it does does it not, rotation = Quarternion.eular(x,y,z); then you apply the rotation var to any use case like transform.rotation = rotation
"it does does it not" come on it's too late for this kind of english
Try swapping this linecs rot = rot * Quaternion.Euler(0, scrollValue, 0);
To thiscs rot = Quaternion.Euler(0, scrollValue, 0) * rot;
Does it work any different?
honestly this is much easier
nope
i'm not sure why fromtorotation is used tbh
let me see
To align the object to the ground
You seem to be missing some context here
Compared to how much you insist that "RotateAround" will fix it
from up to hitnormal.. is added to change... no wonder it's not working
your current rotation isn't used at all
It was originally used
maybe this? rot = Quaternion.Euler(0,rot * scrollValue, 0);
@burnt tusk Does it align to ground normally if you leave out the rot = rot * Quaternion.Euler(...) part?
rotatearound was made for this exact purpose in a really noob-friendly way
i'm not sure why you insist on doing anything else
it already uses object's rotation on background and it's simple to understand that you just spin it around that axis
It does
oh wait, maybe i confused it with transform.rotate
Wait wtf.. You changed the scroll code completely?
float scrollValue = Mouse.current.scroll.y.ReadValue();```
??
Its supposed to be stored in a float
?
what's wrong with my scroll value
It's using the amount that the scroll was moved this frame
yes?
You should add that scroll change to a float
And then use that float in the Quaternion.Euler
In your class:cs private float scrollValue;
In the method:cs scrollValue += Mouse.current.scroll.y.ReadValue();
What
I'm so fucking stupid
how did I not see that
the scrollvalue you mentioned
of course it keeps resetting to 0
See, there's 1000 ways to rotate a thing
Just because you dont understand quaternion multiplication doesnt mean you have to insist on using something else
k well that's fixed now, thanks! Now I need to find out how to smooth it like it was before
Yeah that was the issue. I just didnt read that part at all at first
i understand quaternions. i made an anti-gravity fps, i just don't remember how.
Well you were pretty confident that Quaternion.Euler is not the method to use here 🤷♂️
there was a method that does a really simple rotation around vector
You could use SmoothDampAngle or LerpAngle for the scrollValue
i'm just not sure which one it was
Im trying quaternion/.lerp
Yeah that works too
Use Slerp, it's smoother on larger angle differences
Yescs Quaternion.Euler(0, angle, 0);
Does the same ascs Quaternion.AngleAxis(angle, Vector3.up);
If you'd need an artitrary axis then you would use AngleAxis
In this case we only need the Y axis so Euler is fine
yeah it doesnt
Ill try slerp
sounds so nice
Then you did it wrong
prob
Lerp and Slerp are used exactly in the same way
Slurp
slerp is just more expensive in exchange for more accuracy
_objToPosition.transform.rotation = Quaternion.Slerp(_objToPosition.transform.rotation, rot, 1000);
I also tried 1 and 20 for the end value
Slerp (and other lerp methods) take a t value between 0 and 1
but all the same non-existant result
You gave it 1000
...
Replace 1000 with something like Time.deltaTime * 20f
So that it doesn't stutter based on your frame rate
because otherwise it'd be different depending on fps
makes it frame independent, and relies off actual time
Ahh alright, thanks
always imagine you have either 1fps or 1000fps,
otherwise you'll get shitty framerate locks like bethesda and fromsoftware used to do
where everything breaks if you try to play at 100+fps...
Writing a script that changes your Application.targetFrameRate between different numbers can help find these time-dependent issues
might be hard to notice though, better just think ahead
Obviously
ayy now it's smoother than my-... not making that joke, don't wanna get warned
i don't think you get warned in threads
not here.
you can also use
Vector3 vector = new Vector3(0f, 0f, 0f);
Quaternion quaternion = new Quaternion(vector.x, vector.y, vector.z,
Mathf.Sqrt(1 - (vector.x * vector.x + vector.y * vector.y + vector.z * vector.z)));
Thats awful
if that's move convenient than Quaternion.Euler(0f, 0f, 0f)
this... reminds me of something... is that dot product?
no no, everything is alright

Im not saying it wont work, it looks awful
The code
Is ugly
yeah, I know
Ok just making sure you were jk 😆
that also looks a lot like the 99% of old quaternion code i removed before i knew what i was copying
that's what you'd do if there was no Quaternion.Euler
Well that's working amazingly (Thanks yall!). Now I just gotta get this shitty movement system working that took me more than 20 hours
cool flex then
oh boy is that movement choppy
i ended up rewriting my movement... more than 5 times, i think
oof
it's one of the hardest things for some reason
I don't know what's that, but it's nice if works
This movement script uses the (I believe theyre called) behaviour trees, with the animation stuffs you know
Basically all the movement animations line up with the speed and stuff
Looks amazing on a flat surface
But put that dude on a nice terrain? He'll just defy gravity
Rigidbodies + camera + mishandled interpolation + using wrong methods (Update, FixedUpdate, LateUpdate)
Easily makes your game choppy
animation should follow the gameplay, but it seems it's the other way around in the video
Yes Im pretty sure it is