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");
}
}
}`