- Home /
Player character X rotation going haywire on game start
Hey all, so randomly my player characters rotation is messing with me. I was working on an orbit cam that I could rotate around the player on the Y axis depending on which key was pressed sort of like a Runescape deal. I added a dummy object(sphere), attached the camera to it and set the transform position of the dummy object to my player and then added the rotation script to the dummy, I successfully did this and everything was working perfect.
After a happy victory, I decided to attempt and correct the player falling over problem when it attempts to walk over a bump or incline (I'm using a custom thirdperson character) as freezing the rigidbody rotation constraints was not solving this issue. I attempted to mess around with Quaternion and freeze the rotation via script, unfortunately I am still a noob to all of this and had no idea what I was doing, I was able to freeze all 3 rotations and the player was staying upright while going up an incline, but unfortunately now the player would not turn on the Y axis when walking in different directions. So I deleted all my code relating to freezing the rotations and decided to tackle something else. This is when the issue started.
For a couple of game starts the player was doing fine just as it was when I finished the orbit cam setup, but now, every time I start the game, my players X-axis goes automatically to -90 and bounces around! (Even with the X constraint frozen via rigidbody), if I click around the map rapidly the player will eventually bounce back to standing upright, but even then the X axis is a bit touchy and he very slowly starts to fall over (WITH CONSTRAINTS FROZEN?) So my question is, is there a way I could freeze my players X and Z rotation indefinitely via script? as I attempted to do earlier with Quaternions and failed? or even at least correct this issue to where my player will once again stand upright. did me messing with Quaternions hard-code some kind of glitch?
My Code:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class PlayerController : MonoBehaviour
{
public float speed = 6;
public Rigidbody rb;
public Animator anim;
private Vector3 targetPosition;
private bool isMoving;
const int LEFT_MOUSE_BUTTON = 0;
void Start ()
{
targetPosition = rb.transform.position;
isMoving = false;
}
void Update()
{
if (Input.GetMouseButtonDown (LEFT_MOUSE_BUTTON))
SetTargetPosition ();
if (isMoving)
MovePlayer();
if (PointerOverUI())
{
targetPosition = transform.position;
isMoving = false;
}
else
{
MovePlayer();
}
}
public bool PointerOverUI() // method for checking if pointer is over a UI
{
return UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject();
}
void SetTargetPosition ()
{
Plane plane = new Plane (Vector3.up, rb.transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float point = 0f;
if (plane.Raycast(ray, out point))
{
targetPosition = ray.GetPoint(point);
}
if (PointerOverUI()) // If the pointer is clicked over a UI, player will not go to clicked point
{
targetPosition = transform.position;
}
else // if the pointer is not over UI, then player will move
{
MovePlayer();
}
isMoving = true;
}
void MovePlayer ()
{
rb.transform.LookAt(targetPosition);
rb.transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
if (transform.position == targetPosition)
isMoving = false;
if (isMoving == false) {
anim.SetInteger("Condition", 0);
return;
} else {
anim.SetInteger("Condition", 1);
}
Debug.DrawLine (transform.position, targetPosition, Color.red);
}
}
and my dummy object camera script, just in case that happened to have something to do with it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateCamera : MonoBehaviour {
public int speed = 150;
public GameObject player;
void Update()
{
Vector3 pos = player.transform.position;
transform.position = pos;
if (Input.GetKey(KeyCode.A)) // rotate camera left
transform.Rotate(Vector3.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D)) // rotate camera right
transform.Rotate(-Vector3.up * speed * Time.deltaTime);
}
}
(I HAVE NO COMPILER/CONSOLE ERRORS)
I can't see anything that would affect my players rotation to the point to where he'd fall over, go haywire on the X-axis and ignore the rigidbody constraints? Then again, as i said, I am still a super noob, so any insight or help would be so much apreciated! thank you!
Okay, so I think I figured it out. I forgot to remove the:
else
{
$$anonymous$$ovePlayer();
}
in my the update function of my PlayerController... DOH!
UPDATE: Okay never$$anonymous$$d, it seems to have dampened the glitch, seems at least half the time he'll stand upright now, am I missing something else? BUT! if anyone can tell me how to lock the rotation of my players X and Z axis indefinitely via script and not the rigidbody constraints, perhaps that would correct it? thanks!
Rigidbody constraints do not lock their movement, they constrain what the physics system can do with the rigidbody. You're moving and rotating the object directly so you need to constrain the motion yourself (eg with something along the lines ShadyProductions' answer suggests)
Answer by ShadyProductions · May 23, 2017 at 11:41 AM
This will lock the X rotation to 0, but leave y and z rotation the same as the transform.
Vector3 rotation = new Vector3(0, rb.transform.rotation.y, rb.transform.rotation.z);
rb.transform.rotation = Quaternion.Euler(rotation);
Put this inside the MovePlayer(); method
Your answer
Follow this Question
Related Questions
how to tilt boat on turning 2 Answers
Rotation Jumping values (0 to 180) 1 Answer
Object slope rotation issue 0 Answers
Problem with rigidbodies and rotation with quaternion 1 Answer