Hi
I'm having quite a tough time trying to figure out how I can create a player controller that acts as a typical 3rd person rpg player control would act, but on a spherical planet - ie, the gravity is player position - planet, so the player can run around on small planetoids. I can't seem to figure out how to rotate the player around local y axis while maintaining this spherical gravity
Here is my player controller which is controlling gravity, movement, and hopefully rotation
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public GameObject _planetoid;
Rigidbody _rigidbody;
float moveSpeed = 1f;
float jumpSpeed = 3f;
bool isGrounded;
private Animator _anim;
float _speed = 0.0f;
float h = 0.0f;
float v = 0.0f;
float rotationVelocity = 200f;
Quaternion targetRotation;
Vector3 moveDirection;
Vector3 gravityDirection;
float gravityStrength = 9.81f;
void Awake () // ****************************************************************************** awake
{
_anim = GetComponent<Animator>();
_rigidbody = GetComponent<Rigidbody>();
h = 0.0f;
v = 0.0f;
targetRotation = transform.rotation;
}
void Update () // ****************************************************************************** update
{
OnDrawLocalTransforms();
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
_speed = new Vector2(h, v).sqrMagnitude;
_anim.SetFloat("Speed", _speed);
_anim.SetFloat("Direction", h, 0.1f, Time.deltaTime);
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")).normalized;
if (Input.GetAxis("Jump") != 0f)
{
if(isGrounded)
OnJump();
}
}
void FixedUpdate()
{
_rigidbody.MovePosition(_rigidbody.position + transform.TransformDirection(moveDirection) * moveSpeed * Time.deltaTime);
gravityDirection = (_planetoid.transform.position - transform.position).normalized;
GetComponent<Rigidbody> ().AddForce (gravityDirection * gravityStrength);
//******** THIS ALLOWS PLAYER TO RUN CORRECTLY ON PLANET ********//
//** HOWEVER, REMOVING THESE 3 LINES ALLOWS PLAYER TO ROTATE AROUND UP VECTOR **//
targetRotation = Quaternion.FromToRotation (Vector3.up, -gravityDirection);
targetRotation *= Quaternion.AngleAxis(rotationVelocity * h * Time.deltaTime, transform.up);
transform.rotation = targetRotation;
//*****************************************************************//
OnTurn(); // **** THIS WILL ROTATE PLAYER TOWARDS INPUT AXIS IF ABOVE IS REMOVED
}
void OnTurn()
{
targetRotation *= Quaternion.AngleAxis(rotationVelocity * h * Time.deltaTime, transform.up);
transform.rotation = targetRotation;
}
void OnJump()
{
_rigidbody.velocity = transform.up * jumpSpeed;
}
public void OnDrawLocalTransforms()
{
Debug.DrawRay(transform.localPosition, transform.up, Color.green);
Debug.DrawRay(transform.localPosition, transform.forward, Color.blue);
Debug.DrawRay(transform.localPosition, transform.right, Color.red);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "planetoid") // is grounded
{
isGrounded = true;
}
}
// void OnCollisionStay(Collision collision)
// {
// if (collision.gameObject.tag == "planetoid") // is grounded
// {
//
// }
// }
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "planetoid") // in air
{
isGrounded = false;
}
}
}
I'm hoping someone here may be able to see what I'm doing wrong, or give any advice on how I can go about rotating my player properly. Thanks