Having trouble rotating around transform.up
Hi
I'm trying to develop a 3rd person character controller that behaves properly while transform.up differs from vector3.up. ie, I am making a game with "faux gravity" (a player running around on planet), but I cant seem to get the player to rotate towards input.getAxis direction (horizontal) while that player is being affected by the changing gravity
Here is my player controller, which is controlling movement. rotation, and gravity:
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;
}
}
}
Thanks for any help!
Your answer
Follow this Question
Related Questions
Object rotates normaly while moving, then does zig zags? 0 Answers
How can I bind this script into the state of my camera? 2 Answers
After added a rigidbody in a GameObject, it moves on rotation 2 Answers
Adding and subtracting Angle problem 0 Answers
Rotate a game scene preserving all physics ,Rotate scene preserving all game structure (physics) 1 Answer