Node rotating in reverse

Godot Version

4.4

Question

So I was trying to get this this in my game to face the way it is spinning but it always faces the other direction and its very weird like it just rotates the oppisite way any help would be great
Heres my code:
` using Godot;
using System;

public partial class Goldfish : Node3D
{
#region Fish Attributes
//Pricing
[Export]
public float buyPrice = 5;
[Export]
public float baseSellPrice = 10;
[Export]
public float Hunger = 50;
[Export]
public float Happiness = 50;
#endregion

Vector3 newDirection = Vector3.Zero;

float SwimSpeed = 20f;
Random rand = new Random();

float TimeElapsed = 0f;
[Export]
float TimeLimit = 3f;

public override void _Ready()
{
    ChangeDirection(); // Starts with it goig in a random direction
}

public override void _PhysicsProcess(double delta)
{
    TimeElapsed += (float)delta;

    if (TimeElapsed >= TimeLimit) // Checks when to switch directions again
    {
        ChangeDirection();
        TimeElapsed = 0f;
    }
    Translate(newDirection * SwimSpeed * (float)delta);
    ChangeRotation((float) delta);
}

void ChangeDirection()
{
    float x = (float)(rand.NextDouble() * 2f - 1f);
    float y = (float)(rand.NextDouble() * 2f - 1f);
    float z = (float)(rand.NextDouble() * 2f - 1f);

    newDirection = new Vector3(x, y, z).Normalized();
    //GD.Print("ChangeDirection() Called");
}

void ChangeRotation(float delta) // Rotates to match direction fish is facing
{
    if (newDirection != Vector3.Zero)
    {
        // Get the current rotation as a Quaternion
        Quaternion currentRotation = new Quaternion(GlobalTransform.Basis).Normalized();

        // Get the target rotation using LookingAt
        Basis targetBasis = Basis.LookingAt(newDirection, Vector3.Up);
        Quaternion targetRotation = new Quaternion(targetBasis).Normalized();

        // Interpolate rotation using SLERP
        Quaternion newRotation = currentRotation.Slerp(targetRotation, 2f * delta).Normalized();
        // Apply the new rotation
        GlobalBasis = new Basis(newRotation);
        //GD.Print("ChangeRotation() Called");
    }
}

}`

LookingAt uses -Z as forward, is your model facing forward down +Z instead?

If it’s always looking in the opposite direction you want, your options could be rotate the model to naturally face the opposite direction, make the model a child of a node that does this 180 rotation for you, or change your code to negate newDirection as the parameter to LookingAt().

1 Like

OMG yes it works now thank you for the help

I’m getting this error does this have anything to do with my problem because it is still not working great

E 0:00:00:355 get_quaternion: Basis [X: (1.0, 0.0, -0.0), Y: (0.0, 0.0, 1.0), Z: (-0.0, 1.0, -0.0)] must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors.
<C++ Error> Condition β€œ!is_rotation()” is true. Returning: Quaternion()
<C++ Source> core/math/basis.cpp:730 @ get_quaternion()