Quaternions create problems with my forward vector.

  • edited my question so it is more clear.

So I have a player(capsule) and I want his bottom to face to the center of the cube. The problem is that I only can get it working while losing control over my forward vector, I need my forward vector to tell my player where he can move 2. Also its not nice to see a player ingame rotating over his Y axis without a input of a controls. This is hapenning:

The cube that the capsule is holding is the forward vector.

The problem is that it does point to the center of the cube while moving over 1 axis but when moved over the other axis it starts acting wierd. This is the code:

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

    public Transform ground;
    public Transform player;

    void Update()
    {
        Landing();
    }

    public void Landing()
    {
            Vector3 dir = ground.position - player.position;
        Quaternion rot = Quaternion.LookRotation(player.forward, -dir);

        player.rotation = rot 
    }
}

if I add this code however it works but I lose control over my forward direction it just keeps spinning:

Quaternion bla2 = Quaternion.LookRotation(player.right, -dir);

How can I fix the rotation and also keep my forward vector, becuase you don’t see random spinning characters in games haha.

You may want to consider setting your rotation in a manner more like:

Vector3 newRight = Vector3.Cross(-dir.normalized, player.forward);
Quaternion newForward = Quaternion.LookRotation(Vector3.Cross(newRight, -dir), -dir);
player.rotation = newForward;

Doing this makes use of LookRotation(), which I’m using here to maintain the same forward direction (player.forward), but then applying the new upward direction to it as the second parameter. Most importantly, this approach means giving priority to the forward vector as your current direction.

Old version, set aside for reference for responses

Quaternion newForward = Quaternion.LookRotation(player.forward, -dir);

player.rotation = newForward;