Good day community!
I need help getting a vector. Please first look at the image at the end of the post first to get the idea.
The camera rotates arround a pivot, always looking at it.
I got a vector N which is perpendicular to the surface (I use a Quaternion.Slerp() to align it) and I also got a V vector which point from the camera to the pivot.
I need to get the player walking forward over the surface in to the direction the camera point to, my current code for this is:
if (Input.GetKey(KeyCode.W))
{
Quaternion q = Quaternion.LookRotation(Camera.main.transform.forward, cameraPivot.up);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
moving = true;
}
if (moving)
{
rb.AddRelativeForce(Vector3.forward * force, ForceMode.Force);
}
Problem with this is that if I look down, player moves down into the ground (with gravity disabled) and if I look up, it flyes into the sky, the movement should only be limited to Z and X axis.
I know that I can align the plaver with the direction of the pivot, and instead of rotating the camera arround the pivot, rotate the pivot, so the camera will also rotate,but I can not do this because of the script I got to align the pivot with the surface which is:
using UnityEngine;
public class CameraAlignment : MonoBehaviour
{
float alignSpeed = 10f;
void FixedUpdate()
{
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Planet"))
{
if (go.activeSelf)
{
float atmosthereRadius = go.GetComponent<Planet>().atmosphere;
float distance = Vector3.Distance(go.transform.position, transform.position);
if (distance <= atmosthereRadius)
{
//Align with planet
Vector3 v = go.transform.position - transform.position;
Quaternion q = Quaternion.FromToRotation(-transform.up, v);
Quaternion targetRotation = q * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * alignSpeed);
}
else
{
Vector3 targetPoint = new Vector3(0f, 0f, 0f);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * alignSpeed);
}
}
}
}
}
I don’t know If It is posible to get vector V´ from V and N, this means that V´ is perpendicular to N, but points in the same direction that V but only over N’s Z and X axis, excluding the inclication. The V´ vector should basicaly rotate arround N along the Y axis.
If the image is not shown, this is the link:
https://image.ibb.co/noAvZQ/IMG_20170921_151241495.jpg