I am trying to make a space game in which a player can be sent in space, this player has a rigid body, so it can rotate on all axis. But with this came a problem about the view.
I must have a view relative to the player orientation, and this in a restricted view cone.
Lets say a view cone restriction of -60 and 60, you can be thrown in space, you would be rotating on the z axis, so would be your view. But what i want is the view to be realist.
I made a planet that we can land on, but, if you land under the planet, you see upside down, and if you go on the sides, you see the planet surface on the side. → This is what i want to fix.
(i already have the gravity code for the player orientation for the planet, but not the view)
using UnityEngine;
using System.Collections;
public class GravitationMovement : MonoBehaviour
{
public Transform gravitySource;
public float gravScalar = 4.0f;
public Vector3 rotOffset = Vector3.zero;
void FixedUpdate ()
{
var direction = gravitySource.position - transform.position;
rigidbody.AddForce(direction.normalized * gravScalar);
transform.rotation = Quaternion.LookRotation(direction.normalized) * Quaternion.Euler(rotOffset);
}
}
I now need a script to control how the view would turn relative to the parent.
Here is what i tried, and it failed hard, i think i need quaternions, but i couldn’t find helpful references anywhere, even on the wiki.
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour
{
bool lockView = false;
float sensitivityPitch = 4F;
float sensitivityYaw = 4F;
float rotationPitch;
float rotationYaw;
float rotationRoll;
float rotMinimumPitch = 0;
float rotMaximumPitch = 0;
float rotMinimumYaw = 0;
float rotMaximumYaw = 0;
void Update ()
{
rotationYaw += Input.GetAxis("Mouse X") * sensitivityYaw;
rotationPitch += Input.GetAxis("Mouse Y") * sensitivityPitch;
Mathf.Clamp(rotationYaw, rotMinimumYaw, rotMaximumYaw);
rotMinimumPitch = transform.parent.eulerAngles.x - 10;
rotMaximumPitch = transform.parent.eulerAngles.x + 10;
rotMinimumYaw = transform.parent.eulerAngles.y - 10;
rotMaximumYaw = transform.parent.eulerAngles.y + 10;
print("minpitch = " + rotMinimumPitch);
print("maxpitch = " + rotMaximumPitch);
print("minyaw = " + rotMinimumYaw);
print("maxyaw = " + rotMaximumYaw);
if (rotationPitch > rotMinimumPitch)
{
if (rotationPitch < rotMaximumPitch)
{
Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
transform.localEulerAngles = rot;
}
else
{
transform.eulerAngles = new Vector3(rotMaximumPitch, transform.eulerAngles.y, transform.eulerAngles.z);
}
}
else
{
transform.eulerAngles = new Vector3(rotMinimumPitch, transform.eulerAngles.y, transform.eulerAngles.z);
}
if (rotationYaw > rotMinimumYaw)
{
if (rotationYaw < rotMaximumYaw)
{
Vector3 rot = new Vector3(-rotationPitch, rotationYaw, 0);
transform.localEulerAngles = rot;
}
else
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, rotMaximumYaw, transform.eulerAngles.z);
}
}
else
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, rotMinimumYaw, transform.eulerAngles.z);
}
}
}