- Home /
How does one constrain rotations of a CharacterController without a Rigidbody?
Hello,
I'm working on a player character and a follower character (the follower follows the player around), based on the 3DBuzz Third Person Character Controller Tutorial. I haven't followed the tutorial myself, but instead I received the files from someone who did.
For the following of the follower I use a simple transform.LookAt() and then use the controller to move the character forward. None of the characters in either my scene or the tutorial files are using a Rigidbody, but in the LookAt of the Follower I tell it to ignore the X rotation of the Player. Like this:
transform.LookAt(new Vector3(0, otherCharacter.transform.position.y, otherCharacter.transform.position.z);
Now my problem with this is that the Follower will only look to the left, as if it ignores the other axes too. When I tell it to ignore the Y axis, it rotates almost correctly, is able to follow you around just fine, but when the Player's Y-position gets greater than the Follower's, it will still look up and down, which I don't want it to do. Attaching a Rigidbody won't actually do anything, since when I let it use gravity, it'll fall through the floor (even tho it has the CharacterController, and thus a collisionbox), and if I take the gravity out, the constraints won't do anything.
This is the Walk() function I made for the follower, it is called whenever the Follower is indeed the follower (since the player can switch between playing the Player and the Follower)
void Walk()
{
transform.LookAt(new Vector3(0, otherCharacter.transform.position.y, otherCharacter.transform.position.z));
if (Vector3.Distance(transform.position, otherCharacter.transform.position) > 2)
{
follow = true;
}
if (Vector3.Distance(transform.position, otherCharacter.transform.position) < 2)
{
follow = false;
}
//VAxis and HAxis are the equivalent of the Vertical and Horizontal axes called when using the D-pad to control the character.
if (follow)
{
Controller.VAxis += Time.deltaTime;
}
else
{
Controller.VAxis -= Time.deltaTime * 3;
}
Controller.VAxis = Mathf.Clamp(Controller.VAxis, 0, 0.9f);
}
So now my question is: How can I fix this?
If there's anything you need from me, just tell me. I hope anyone can help.
transform.rotation.eulerAngles