Make the correct rotation to face the gameobject to his direction
So, i'm working on a 3d person view character controller that walks in a planet. His gravity is made by a simple force to the direction of the center of the planet, and i apply a rotation to always stay right.
However, when i'm working on the rotation of the player to face his direction, altought seems to work fine, at the borders it just does weird things.
Here is a video showing the problem: https://streamable.com/fgdfm The pink thing is the main gameobject, and the red one is the Model gameObject.
Also, here is the character Controller (there are som notes that are tries, just trying to get it right).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moviment : MonoBehaviour
{
public Rigidbody rb;
public Transform pivot;
public Transform Model;
public Transform Planeta;
public float velocitat = 100f;
public float maxVelo = 5f;
public float minVelo = 4f;
private float X;
private float Z;
public float smooth;
private float speedX;
private float speedY;
private float speedZ;
// Update is called once per frame
void Update()
{
X = Input.GetAxis("Horizontal");
Z = Input.GetAxis("Vertical");
}
private void FixedUpdate()
{
speedX = transform.InverseTransformDirection(rb.velocity).x;
speedY = transform.InverseTransformDirection(rb.velocity).y;
speedZ = transform.InverseTransformDirection(rb.velocity).z;
if (X != 0 || Z != 0)
{
Mou();
}
}
private void Mou()
{
rb.AddForce((X * velocitat * transform.right) + (Z * velocitat * transform.forward));
rb.velocity = transform.TransformDirection(new Vector3(Mathf.Clamp(Mathf.Abs(speedX), minVelo, maxVelo) * X, speedY, Mathf.Clamp(Mathf.Abs(speedZ), minVelo, maxVelo) * Z));
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, pivot.eulerAngles.y, transform.eulerAngles.z);
Quaternion Direction = Quaternion.LookRotation(transform.forward * Z + transform.right * X);
Model.transform.rotation = Quaternion.Slerp(Model.transform.rotation,Direction, smooth * Time.deltaTime);
//TEST1
/*rb.velocity = pivot.transform.TransformDirection(new Vector3(Mathf.Clamp(Mathf.Abs(speedX), minVelo, maxVelo) * X, speedY, Mathf.Clamp(Mathf.Abs(speedZ), minVelo, maxVelo) * Z));
Vector3 Direction = (X * transform.right) + (Z * transform.forward).normalized;
transform.rotation = Quaternion.LookRotation(rb.velocity);*/
//TEST2
/*Vector3 direction = (Planeta.position - transform.position).normalized;
Vector3 Direction = (X * velocitat * pivot.transform.right) + (Z * velocitat * pivot.transform.forward);
Quaternion targetRot = Quaternion.LookRotation(Direction+direction);
Quaternion PlayerTargetRotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * smooth);
transform.rotation = PlayerTargetRotation;
*/
//TEST3
/*
Vector3 direction = ((X * pivot.transform.right) + (Z * pivot.transform.forward)).normalized;
Quaternion targetRot = Quaternion.LookRotation(direction);
Quaternion PlayerTargetRotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * smooth);
transform.rotation = PlayerTargetRotation;
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, pivot.eulerAngles.y, transform.eulerAngles.z);
*/
}
}
This is the gravity itslef:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Planeta : MonoBehaviour
{
public float Gravity;
public void Attract(Transform What)
{
Vector3 direction = (transform.position - What.position).normalized;
What.GetComponent<Rigidbody>().AddForce(direction * Gravity);
What.rotation = Quaternion.FromToRotation(What.up, direction*-1) * What.rotation;
}
}
I know it's a little bit messy, but please, i need some help...
ok it was a bit confusing but you have just commented out other things you have tried.. seems as though from the video that when there is collision with the white I'm Assu$$anonymous$$g that's the boarder of your map? ok looking at your code "Attract()" turns the char rotation when moving close to the object with that script right that's the goal? you want to access that method for 1. and 2. you should be referencing the script not "Transform" to access it or a longer method just more writing is all Planeta.GetComponent(). you may get an error if both are named Planeta so make your class PlanetA ins$$anonymous$$d. Next i need to know is the camara the main parent and has a child player object or the other way around? We are gunna break your scene down here, and strip the controls. First off take any controllers off camera and make sure that your player controller is still on the player as it seems to be doing as it should in the video. put the camera as the child and position it where it looks good only for now. Next lets test whats happening, when the player moves the camera will follow the player object without any controls but does it stay in position? or is it moving the camera?. It's angle should never change only spin with char. ok good, now get the Unity Camera Controllers for this test so that you can look around the player object I think they are called FollowCamera and Camera$$anonymous$$ovement like before. Switch in the hierarchy to be the $$anonymous$$ainCamera on its own and Player on its own I'm pretty sure, It has been a while since i used it last, and add FollowCamera or FollowPlayer w.e it's called to the Camera. Then, grab the player object and drag it into where the follow cam script says Player or Player Object. it's more then likely calling for a GameObject or Transform Do another test to see if the camera follows the player as it did when it was a child shouldnt need to change anything else about the camera just yet just move it in the hierarchy. next add the camera movement script to the camera so you can move the camera again like you did before. try another test. is everything working as it should Great! Or are we back at step 1? Breaking down your issue is the only way to find the issue. when the issue begins to occur and when it isn't occurring are very important to debugging. Lemme know or upvote if this did help.