- Home /
Y position increasing on character controller
Hello all. I've run into a weird issue when moving the character controller in local space. I'm rotating an object around a sphere using joystick inputs. The object is the child of the sphere, and movement is happening in local space. The issue I run into is that the radius on which the object rotates slowly increases as it moves. Can't for the life of me figure out why. So, for example, if the object starts touching the outside of the sphere, after several rotations it's moved away from the surface. Any recommendations on how to limit this would be appreciated.
public class Movement : MonoBehaviour
{
public float speed = 4;
Vector3 movement;
Vector3 up;
public Transform planet;
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
//Movement
movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized * speed;
//Translate movement from world to local space
movement = transform.TransformDirection(movement);
//Move
controller.Move(movement);
//Rotate so that up is always away from center
up = transform.position - planet.position;
transform.rotation = Quaternion.FromToRotation(transform.up, up) * transform.rotation;
}
}
Answer by PedroFabri · Jul 21, 2020 at 01:00 AM
My Unity is adding 0.13 to the Y axis when I move my character too. I can't figure out why, so, at the end of my moviment, a zero my Y again:
transform.position = new Vector3(transform.position.x, 0, transform.position.z);
Not the best solution but it's what I can do for now...
Answer by TheShrikeFromCodeStudio · Jan 23 at 07:23 PM
I had the same problem and found your unanswered post. After a lot of research I came across this video which was very helpful.
https://www.youtube.com/watch?v=UUJMGQTT5ts
Basically my problem was the setting in the character controller for "skin width". This is a buffer area around the the collision capsule that will trigger a collision with things such as the ground, Mine was set too high for the scale of my world and thus noticeable raised my character off the ground. It should be set to some ratio of the controller capsule "radius" setting. The recommendation was 10% but had a radius of 0.05 and set siskin width to .001 which seemed to fix my issue.
Hope this helps some one in the future.
Your answer
Follow this Question
Related Questions
Character Controller 1 Answer
Force on Character Controller (Knockback) 4 Answers
rotate character in 2.5D 1 Answer
What is wrong with my CharController script? 1 Answer
Character Controller teleporting to ground instead of falling 1 Answer