Question by
invitadoxr · Jun 17, 2020 at 02:50 PM ·
physicscharactercontrollerjumplag
Character Controller too lag and no and without equal height
Hello everyone and thanks to the community in advance.
the code below is a 3d platform character controller (and wish it still is) that runs relatively well, the camera was made in Cinemachine.
but I feel that the jump and the gravity is too laggy and unreal. also, on occasions it jumps higher or lower when it wants.
I do not know what to do.
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
#region vars
public Transform cam;
public CharacterController cc;
private Vector3 dir = Vector3.zero;
private Vector2 inputClamp = Vector2.zero;
public float speedMov = 8f;
public float speedRot = 10f;
public float gravity = 9.80665f;
public bool isGrounded = false;
public float jumpForce = 15f;
public float jumpOffset = 0.1f;
#endregion
void Update()
{
//get inputs
inputClamp = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
inputClamp = Vector2.ClampMagnitude(inputClamp,1);
DoLookAt();
//move in the look forward
Vector3 v01 = dir;
Vector3 v02 = transform.forward * inputClamp.magnitude;
dir = Vector3.Lerp(v01,v02,0.5f);
cc.Move(dir * speedMov * Time.deltaTime);
//reset for no move again in x and z axis
//with lerp smooth axis and to use for gravity
Vector3 v1 = dir;
Vector3 v2 = new Vector3(0f, dir.y, 0f);
dir = Vector3.Lerp(v1, v2, /**/ 0.5f);
// RaycastHit hit;
isGrounded = Physics.Raycast( transform.position, Vector3.down, cc.bounds.extents.y + jumpOffset );
if(isGrounded){
dir.y = -0.5f;
if(Input.GetButtonDown("Jump")){
// dir.y = jumpForce;// no work complety well
// dir.y = Mathf.Sqrt(jumpForce * -2f * gravity);//no work complety well
dir.y = dir.y + jumpForce - gravity * Time.deltaTime * Time.deltaTime;//no work complety well too
}
}
else{
dir.y = dir.y - gravity * Time.deltaTime;
dir.y = Mathf.Clamp(dir.y, -gravity, gravity);
}
cc.Move(dir * Time.deltaTime);
}//end Update
private void DoLookAt(){
Vector3 camF = cam.forward;
Vector3 camR = cam.right;
//the cam is posible to be in angulated, reset
camF.y = 0;
camR.y = 0;
camF = camF.normalized;
camR = camR.normalized;
Vector3 dirRot = camF * inputClamp.y + camR * inputClamp.x;
if( inputClamp.magnitude > 0.1f ){
Quaternion rot = Quaternion.LookRotation(dirRot);
transform.rotation = Quaternion.Lerp(transform.rotation, rot, speedRot * Time.deltaTime);
}
}
}//end class
Comment
Your answer
Follow this Question
Related Questions
How to jump to a point using CharacterController? 0 Answers
Character controller aplly gravity in FixedUpdate with my own gravity in Update 1 Answer
Swiping problem (jumping) 0 Answers
Character not jumping smoothly using CharacterController 0 Answers
Can someone help me with why my jump wont work properly 0 Answers