#Quarternions

1 messages · Page 1 of 1 (latest)

pale marlin
#

@burnt tusk

burnt tusk
#

1 sec I think I got it

pale marlin
#

i found this might explain it a little

#

if it wasnt so jammed

tiny mantle
#

!code

dire roverBOT
#
Posting 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.

loud geode
#

identity is just like vector(0,0,0)
might be pointless or counter-productive

tiny mantle
#

no, identity is (0, 0, 0, 1)

loud geode
#

euler angles might not be the best thing if you have a good vector to use either

#

like vector3.zero in concept

pale marlin
#

wait is that how it works

loud geode
#

vectors help with understanding quaternions

vast sage
#

In the end, quaternions are almost always used to rotate vectors

pale marlin
#

Quarternions

loud geode
#

multiplying quaternions is same as adding vectors

tiny mantle
loud geode
#

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

tiny mantle
loud geode
#

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

pale marlin
#

@tiny mantle like at the beginning and end

tiny mantle
vast sage
burnt tusk
#

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);
pale marlin
#

ah

vast sage
#

Like every second line in that paste

burnt tusk
#

sorry for messy code

#

but I need the self-constructed quaternion

#

I removed the le0rps

tiny mantle
burnt tusk
#

because I thought that might cause the weird stuff and it did

vast sage
#

Don't touch those, forget they exist

burnt tusk
#

hmm fine

vast sage
#

It's not the same as a x, y, z angle in a Vector3 euler angle

loud geode
#

@burnt tusk you're trying to rotate camera?

pale marlin
#

there all good

tiny mantle
#

@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.
burnt tusk
tiny mantle
pale marlin
#

how did you do that

tiny mantle
#

you mean !code ?

dire roverBOT
#
Posting 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.

tiny mantle
loud geode
#

up is terrain normal, use up as vector to rotate quaternion around...

pale marlin
#

oh thats what that is for

#

damn

loud geode
#

don't remember what the code was, quaternion.rotatearound?

burnt tusk
#

what

vast sage
#

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?

burnt tusk
#

hmm

loud geode
#

or just use world up, idk

burnt tusk
#

1 sec

#

I think it might bug something out but let me try

loud geode
#

either way it's one of the simple use cases

burnt tusk
#

idk for sure

vast sage
loud geode
#

rotatearound doesn't need it though

vast sage
#

Doesnt need what?

loud geode
#

.euler

vast sage
#

What does that even mean?

#

Transform.RotateAround doesnt need Quaternion.Euler?

loud geode
#

all he needs is to rotate around up vector

vast sage
#

You realize there are dozens of ways to rotate an object

burnt tusk
#

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

loud geode
#

@burnt tusk you're setting rotation instead of adding it

burnt tusk
#

oh

#

1 sec

#

+=?

pale marlin
#

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

burnt tusk
#

"it does does it not" come on it's too late for this kind of english

loud geode
#

transform.rotation *= change

#

i think. if i remember correctly.

burnt tusk
#

and what is the change supposed to be

#

how do I calculate that

vast sage
loud geode
#

honestly this is much easier

loud geode
#

i'm not sure why fromtorotation is used tbh

burnt tusk
vast sage
#

You seem to be missing some context here

#

Compared to how much you insist that "RotateAround" will fix it

loud geode
#

from up to hitnormal.. is added to change... no wonder it's not working

#

your current rotation isn't used at all

vast sage
#

It was originally used

pale marlin
#

maybe this? rot = Quaternion.Euler(0,rot * scrollValue, 0);

vast sage
#

@burnt tusk Does it align to ground normally if you leave out the rot = rot * Quaternion.Euler(...) part?

loud geode
#

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

loud geode
#

oh wait, maybe i confused it with transform.rotate

vast sage
#

Wait wtf.. You changed the scroll code completely?

#
float scrollValue = Mouse.current.scroll.y.ReadValue();```
#

??

#

Its supposed to be stored in a float

burnt tusk
#

?

vast sage
#

And you add the scroll value to that float

#

A float in your class

burnt tusk
#

what's wrong with my scroll value

vast sage
#

It's using the amount that the scroll was moved this frame

burnt tusk
#

yes?

vast sage
#

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();

burnt tusk
#

hmm I see

#

oh

#

I see

#

wow

#

😐

vast sage
#

What

burnt tusk
#

I'm so fucking stupid

#

how did I not see that

#

the scrollvalue you mentioned

#

of course it keeps resetting to 0

vast sage
burnt tusk
#

k well that's fixed now, thanks! Now I need to find out how to smooth it like it was before

vast sage
loud geode
#

i understand quaternions. i made an anti-gravity fps, i just don't remember how.

vast sage
#

Well you were pretty confident that Quaternion.Euler is not the method to use here 🤷‍♂️

loud geode
#

there was a method that does a really simple rotation around vector

vast sage
loud geode
#

i'm just not sure which one it was

burnt tusk
vast sage
vast sage
#

Use Slerp, it's smoother on larger angle differences

loud geode
#

see, fuck this shit.

#

exact thing he needs in this case in 1 line*

vast sage
#

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

loud geode
#

euler one works even if something is tilted?

#

oh yeah if you add it to rotation..

burnt tusk
burnt tusk
#

sounds so nice

vast sage
burnt tusk
#

prob

vast sage
#

Lerp and Slerp are used exactly in the same way

vast sage
loud geode
#

slerp is just more expensive in exchange for more accuracy

burnt tusk
#

_objToPosition.transform.rotation = Quaternion.Slerp(_objToPosition.transform.rotation, rot, 1000);

#

I also tried 1 and 20 for the end value

vast sage
#

Slerp (and other lerp methods) take a t value between 0 and 1

burnt tusk
#

but all the same non-existant result

vast sage
#

You gave it 1000

burnt tusk
#

...

vast sage
#

Replace 1000 with something like Time.deltaTime * 20f

burnt tusk
#

Why

#

Why the time.deltatime

vast sage
loud geode
#

because otherwise it'd be different depending on fps

pale marlin
#

makes it frame independent, and relies off actual time

burnt tusk
#

Ahh alright, thanks

loud geode
#

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

vast sage
#

Writing a script that changes your Application.targetFrameRate between different numbers can help find these time-dependent issues

loud geode
#

might be hard to notice though, better just think ahead

vast sage
#

Obviously

burnt tusk
#

ayy now it's smoother than my-... not making that joke, don't wanna get warned

loud geode
#

i don't think you get warned in threads

burnt tusk
#

Fogsights everywhere man

#

you gotta be careful

loud geode
#

not here.

tiny mantle
vast sage
#

Thats awful

tiny mantle
loud geode
#

this... reminds me of something... is that dot product?

tiny mantle
vast sage
#

The code

#

Is ugly

tiny mantle
vast sage
#

Ok just making sure you were jk 😆

loud geode
#

that also looks a lot like the 99% of old quaternion code i removed before i knew what i was copying

tiny mantle
burnt tusk
loud geode
#

cool flex then

pale marlin
#

oh boy is that movement choppy

loud geode
#

i ended up rewriting my movement... more than 5 times, i think

burnt tusk
#

oof

loud geode
#

it's one of the hardest things for some reason

tiny mantle
burnt tusk
#

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

tiny mantle
burnt tusk
#

But put that dude on a nice terrain? He'll just defy gravity

vast sage
#

Rigidbodies + camera + mishandled interpolation + using wrong methods (Update, FixedUpdate, LateUpdate)
Easily makes your game choppy

loud geode
#

animation should follow the gameplay, but it seems it's the other way around in the video

burnt tusk
#

Yes Im pretty sure it is