Question by
chouchene4561 · Oct 05, 2021 at 09:26 AM ·
buildissueplay modepreformance
Unity lags in play mode but works well after building the game ? over 100 fps ?
the issue is the values arent the same in low and high frame rate and i have a problem with unity where i get 40 / 60 fps on play mode but if i build the game i get like 150 /200 ? and the movement values arent the same for example while wallruning i cant reach the other wall but on the play mode i can idk how to fix the issue or know why is it happening this is my fps controller may be in the code idk its messy tho im sorry :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public enum States
{
Walking,
WallRunig,
Grappling,
verticalWallRuning,
}
States state;
[Header("Controller Settings")]
[SerializeField] CharacterController cc;
[SerializeField] float Movement_Speed;
[SerializeField] float walkSpeed = 15f;
[SerializeField] float crouchSmoother = 8.5f;
[SerializeField] float sprintSpeed = 23f;
[SerializeField] float JumpForce = 15.0f;
[SerializeField] float CrouchSpeed;
bool crouching = false;
[SerializeField] float WallJump = 5f;
float originalHight;
[SerializeField] float crouchHight;
private Vector3 forward, right;
private Vector3 moveDir = Vector3.zero;
bool stopped = false;
[Header("Gravity")]
[SerializeField] float CurrentGravity = 13.0f;
[SerializeField] float NormalGravityOnGround = 14f;
[SerializeField] float wallJumpGravity = 18f;
[SerializeField] float wallGravity = .5f;
[SerializeField] float HookGravity = 19f;
[SerializeField] [Range(.0f, .5f)] float moveSmoothTime = 0.3f;
public Vector2 currentDir, currentDirVelocity;
float velovityY = 0.0f;
[Header("WallRun")]
[SerializeField] private float wallSpeed;
[SerializeField] LayerMask Wall;
[SerializeField] float SideWallforce;
private Vector3 WallDir;
private bool Right, left;
[SerializeField] float Limit;
private float YonWall;
[SerializeField] float MaxWallDistance;
float LWallDistance, RwallDistance;
RaycastHit Lhit;
RaycastHit hit;
[Header("Camera")]
[SerializeField] Camera cam;
[SerializeField] private float fov;
[SerializeField] private float wallrunFov;
[SerializeField] private float wallrunfovTime;
[SerializeField] private float camTilt;
[SerializeField] private float camTiltTime;
[SerializeField] private float GrappleFov;
[SerializeField] private float GrappleFovTime;
public float tilt { get; private set; }
[Header("Grabble")]
[SerializeField] float GrappleRange;
[SerializeField] float GrappleSpeed;
private Vector3 hookPosition;
[SerializeField] float GrappleJump = 1000f;
[SerializeField] LayerMask Grapple;
LineRenderer lr;
[Header("vertical Wallrun settings")]
[SerializeField] private float VerticalgravityForce = 5f;
Vector3 verticalWallRunDirection = Vector3.zero;
public LayerMask verticalWRLayer;
[SerializeField] float maxcastdistance;
bool JumpedFromWall = false;
[SerializeField] float verticalWallSPeed = 10f;
[SerializeField] float CLimbingJump = 750f;
[SerializeField] float verticalWallFov = 4f;
RaycastHit vhit;
private void Awake()
{
lr = GetComponent<LineRenderer>();
}
void Start()
{
originalHight = cc.height;
state = States.Walking;
}
private void LateUpdate()
{
if (state == States.Grappling)
{
DrawRope();
}
}
private void Update()
{
if (state == States.Walking)
{
movementInput();
normalMovement();
Crouch();
sprint();
resetfov();
lr.positionCount = 0;
}
else if (state == States.WallRunig)
{
wallruning();
}
else if (state == States.Grappling)
{
GrappleOnAir();
}
else if (state == States.verticalWallRuning)
{
VerticalWallrun();
}
RwallDistance = Vector3.Distance(transform.position, hit.point);
LWallDistance = Vector3.Distance(transform.position, Lhit.point);
float vWallDistance = Vector3.Distance(transform.position, vhit.point);
if (stopped && cc.isGrounded)
{
stopped = false;
}
else if (stopped && LWallDistance >= MaxWallDistance && RwallDistance >= MaxWallDistance)
{
stopped = false;
}
if (JumpedFromWall && cc.isGrounded)
{
JumpedFromWall = false;
}
else if (JumpedFromWall && vWallDistance >= MaxWallDistance)
{
JumpedFromWall = false;
}
checkWall();
GrappleCheck();
//checking for vertical climbing;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out vhit, maxcastdistance, verticalWRLayer) && !JumpedFromWall && !cc.isGrounded)
{
state = States.verticalWallRuning;
}
}
void movementInput()
{
Vector2 Targetdir = new Vector2(Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical"));
Targetdir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, Targetdir, ref currentDirVelocity, moveSmoothTime);
if (cc.isGrounded)
{
velovityY -= CurrentGravity * Time.deltaTime;
CurrentGravity = NormalGravityOnGround;
}
if (!cc.isGrounded)
{
velovityY -= CurrentGravity * Time.deltaTime;
}
if (Input.GetButtonDown("Jump") && cc.isGrounded) velovityY =transform.up.y * JumpForce ;
cc.Move(moveDir * Time.deltaTime);
}
void normalMovement()
{
forward = cam.transform.forward.normalized;
right = transform.right.normalized;
moveDir = (forward * currentDir.y + right * currentDir.x) * Movement_Speed;
moveDir.y = Vector3.up.y* velovityY;
}
void Crouch()
{
if (Input.GetKey(KeyCode.C))
{
crouching = true;
}
else crouching = false;
if (crouching)
{
cc.height = Mathf.Lerp(cc.height, crouchHight, Time.deltaTime * crouchSmoother);
Movement_Speed = CrouchSpeed;
}
else if (!crouching)
{
cc.height = Mathf.Lerp(cc.height, originalHight, Time.deltaTime * crouchSmoother * 2);
Movement_Speed = walkSpeed;
}
}
void sprint()
{
if (Input.GetKey(KeyCode.LeftShift))
{
Movement_Speed = sprintSpeed;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
Movement_Speed = walkSpeed;
}
}
void checkWall()
{
if (Physics.Raycast(transform.position, transform.right * SideWallforce, out hit, Limit, Wall) && !cc.isGrounded && !stopped)
{
Right = true;
left = false;
WallDir.y = YonWall;
YonWall = wallGravity * Time.deltaTime;
state = States.WallRunig;
}
else if (Physics.Raycast(transform.position, -transform.right * SideWallforce, out Lhit, Limit, Wall) && !cc.isGrounded && !stopped)
{
left = true;
Right = false;
WallDir.y = wallGravity * Time.deltaTime;
state = States.WallRunig;
}
else
{
Right = false;
left = false;
state = States.Walking;
}
}
void startwallrun()
{
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, wallrunFov, wallrunfovTime * Time.deltaTime);
if (Right == true)
{
WallDir = (forward * wallSpeed + right * SideWallforce);
tilt = Mathf.Lerp(tilt, camTilt, camTiltTime * Time.deltaTime);
}
else if (left == true)
{
WallDir = (forward * wallSpeed + -right * SideWallforce);
tilt = Mathf.Lerp(tilt, -camTilt, camTiltTime * Time.deltaTime);
}
cc.Move(WallDir * Time.deltaTime);
}
void stopWallrun()
{
state = States.Walking;
Right = false;
left = false;
stopped = true;
}
void wallruning()
{
startwallrun();
if (Input.GetButtonDown("Jump"))
{
stopWallrun();
velovityY = transform.up.y * WallJump * Time.deltaTime;
moveDir.y = Vector3.up.y * velovityY;
CurrentGravity = wallJumpGravity;
}
else if (!Right && !left)
{
stopWallrun();
}
}
void resetfov()
{
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, fov, wallrunfovTime * Time.deltaTime);
tilt = Mathf.Lerp(tilt, 0, camTiltTime * Time.deltaTime);
}
void GrappleCheck()
{
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit, GrappleRange, Grapple) && Input.GetKey(KeyCode.E))
{
hookPosition = hit.point;
state = States.Grappling;
}
}
void GrappleOnAir()
{
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, GrappleFov, GrappleFovTime * Time.deltaTime);
velovityY = -1f * Time.deltaTime;
Vector3 GrappleDirection = (hookPosition - transform.position).normalized;
cc.Move(GrappleDirection * GrappleSpeed * Time.deltaTime);
if (Input.GetKeyUp(KeyCode.E))
{
velovityY = transform.up.y * GrappleJump * Time.deltaTime;
CurrentGravity = HookGravity;
moveDir -= hit.normal * GrappleJump;
}
if (Vector3.Distance(transform.position, hit.point) >= 10f)
{
velovityY = transform.up.y * GrappleJump * Time.deltaTime;
moveDir -= hit.normal * GrappleJump;
state = States.Walking;
}
}
void DrawRope()
{
lr.enabled = true;
lr.positionCount = 2;
lr.SetPosition(0, transform.position);
lr.SetPosition(1, hookPosition);
}
void VerticalWallrun()
{
StartverticalWallrun();
velovityY = 1 * Time.deltaTime;
}
void StartverticalWallrun()
{
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView,verticalWallFov, GrappleFovTime * Time.deltaTime);
verticalWallRunDirection = (forward * VerticalgravityForce + transform.up * verticalWallSPeed) * Time.deltaTime;
cc.Move(verticalWallRunDirection * Time.deltaTime);
if (Input.GetButtonDown("Jump"))
{
stopverticalWallrun();
velovityY = transform.up.y * CLimbingJump * Time.deltaTime;
moveDir.y = Vector3.up.y * velovityY;
}
}
void stopverticalWallrun()
{
JumpedFromWall = true;
velovityY = velovityY -= CurrentGravity * Time.deltaTime;
state = States.Walking;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Finish") && state == States.verticalWallRuning)
{
stopverticalWallrun();
velovityY = transform.up.y * CLimbingJump * Time.deltaTime;
moveDir.y = Vector3.up.y * velovityY;
}
}
}
Comment