- Home /
Jerky motion when standing on hovering object,
Hi everyone,
I've been looking for a solution for a good couple of hours now and have come up a complete blank. First of all, this is not an issue related to a 2D platform game, it's a 3D FPS. In my level, I have several floating pillars that move up and down (the x and z positions are always the same). When the player stands on one of the pillars, the motion is incredibly jerky going up, but smooth going down. I think the player is sinking into the pillar slightly and then catching up.
I've tried different colliders, all sorts of combinations with rigidbodys and character controllers. I've tried setting the pillar as a parent of the character and then having the player hover slightly. I've just hit a brick wall and would appreciate an outside perspective. Here's my code just in-case the issue is in there.
Player movement
public float walkSpeed = 6.0F;
public float runSpeed = 10.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void FixedUpdate(){
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButtonDown("Jump")){
moveDirection.y = jumpSpeed;
}
if (Input.GetButton("Sprint")){
walkSpeed = runSpeed;
}
else{
walkSpeed = 6.0F;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void OnCollisionStay(Collision hit) {
if (hit.gameObject.tag == "Column") {
transform.parent = hit.transform;
} else {
transform.parent = null;
}
}
Pillar movement
float height;
float speed;
Vector3 start;
Vector3 end;
Vector3 startOrEnd;
void Start(){
speed = 0.01F;
start = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
}
void Update () {
motionControl(speed);
}
float randomNumber(float min, float max){
float number = Random.Range(min, max);
return number;
}
void motionControl(float speed){
if (gameObject.transform.position == start) {
end = new Vector3(gameObject.transform.position.x, randomNumber(0, 10), gameObject.transform.position.z);
startOrEnd = end;
}else if (gameObject.transform.position == end) {
startOrEnd = start;
}
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, startOrEnd, speed);
}
Thanks in advance for you help everyone!,