Hi there (from Germany),
my question might also fit into physics- or graphics section depending on what exactly is going on with my code, however I decided to post it here at scripting, as I assume my code might be less-then-optimal:
My Game ( A Jump and Run with a rolling ball instead of a character) is runin fine for the most part, however I face some short jittters from time to time. I had a glance in the profiler and it is runnin fine at 60FPS with VSync turned on. Most of the time the CPU is waiting “for target FPS” which from my understanding is the way VSYnc works. If I turn it off it runs at a couple of hundreds of FPS (with other issues).
Ok, so now I believe some excessive rendering or stuff like that should not be the case. It seems to me that GPU as well as CPU are ideling mostly. I therefore assume that the way I move my Character and/or the way the cam follows him might cause some “real” jitter rather then an FPS lag. So, if you don’t mind and someone outthere has the time, could you have a short glance over my movement algorithms? I attach the main parts of my Player Script as well as of the camera script.
PLAYER
private CharacterController cc;
//.............. a lot of further variables
// Update is called once per frame
void Update () {
//MOVE
currentForwardSpeed = Mathf.SmoothDamp(currentForwardSpeed, Input.GetAxisRaw ("Horizontal") * speed, ref forwardSpeedV, speedSmooth );
currentMovement.x = currentForwardSpeed;
currentMovement.y = cc.velocity.y;
//JUMP
if (!cc.isGrounded) {
jumpAllowTimeTrack -= Time.deltaTime;
currentMovement.y -= gravity * Time.deltaTime;
} else {
currentMovement.y = 0;
jumpAllowTimeTrack = jumpAllowTime;
}
if (jumpAllowTimeTrack >= 0 && Input.GetButtonDown("Jump")) {
currentMovement.y = jumpSpeed;
}
if (onMovingGround) {
currentMovement.x += movingGroundSpeed;
}
//MOVE and JUMP
cc.Move (currentMovement * Time.deltaTime);
//ROLL & TURN
currentRoll = Mathf.SmoothDamp (currentRoll, Input.GetAxisRaw ("Horizontal") * rotationSpeed, ref rollSpeedV, speedSmooth);
transform.Rotate (new Vector3( 0, 0, -currentRoll * Time.deltaTime), Space.World);
if (Input.GetAxisRaw ("Horizontal") < 0 && goingRight) {
goingRight = false;
turnaround = true;
//Debug.Log("Turnaround");
} else if (Input.GetAxisRaw ("Horizontal") >= 0 && !goingRight) {
goingRight = true;
turnaround = true;
//Debug.Log("Turnaround");
}
if (turnaround) {
transform.Rotate (new Vector3( 0, 180, 0), Space.World);
turnaround = false;
/*if (changeDirectionTemp >= 180) {
turnaround = false;
changeDirectionTemp = 0;
} else {
changeDirectionVal= Mathf.Lerp(0f, 180f, Time.deltaTime * 10);
changeDirectionTemp += changeDirectionVal;
transform.Rotate (new Vector3( 0, changeDirectionVal, 0), Space.World);
}*/
}
}
CAMERA
public float speed;
private GameObject player;
private Vector3 offset;
private Vector3 clampedPosition;
private float camMinHeight;
// Use this for initialization
void Start () {
player = GameObject.FindWithTag ("Player");
offset = transform.position - player.transform.position;
camMinHeight = offset.y;
}
// Update is called once per frame
void LateUpdate () {
if (player != null) {
clampedPosition = new Vector3 (player.transform.position.x + offset.x,
Mathf.Clamp (player.transform.position.y + offset.y, camMinHeight, 1000.0f),
player.transform.position.z + offset.z);
transform.position = Vector3.Slerp(transform.position, clampedPosition, speed * Time.deltaTime);
}
}
Thank you guys, in advance
Patrick