- Home /
Why is my game running different on build?
I tried to make my code run frameindependent using Time,deltaTime in FixedUpdate(), and thought i had succeded, since it ran at the same speed in both Vsync on and off mode so the framerate was different but it still worked the same. But once its compiled and run outside of the editors player, it behaves completely different, the timespans in which the Actions can be performed, seem the same. For example a jump function that allows you to jump for one second still lets you jump for one second, but the maximum height and speed of the jump differ. Another problem is that the different speedcalculations, jumpspeed/ runspeed are not just all slowed down or speed up at the same time, but only some of them get up and some of them downscaled. Has anybody an idea, why this happens?
Typically, physics-driven actions wind up giving different results when they're being erroneously multiplied by Time.deltaTime at inappropriate times.
Could you give some script examples of how you're applying force in your jump or movement functions? Hopefully this could help shed some light on exactly why you're running into this problem, as well as helping demonstrate to others running into similar issues.
void JumptimeCalc()
{
if (OnGround() == false && jumptime > 0)
{
jumptime = jumptime - (1f * Time.deltaTime);
//Debug.Log(jumptime);
}
if (OnGround() == true && !jumpbutton || OnBall() && !jumpbutton || comforttime > 0 && comforttime <= maxComforttime && !jumpbutton)
{
jumptime = maxjumpheight;
jumpedAlready = false;
}
if (comforttime <= 0 && !jumpbutton && !OnGround())
{
jumptime = 0;
}
}
void Jump()
{
if (jumpbutton && !perfor$$anonymous$$gGroundSmash && jumpedAlready == false &&
jumptime > 0)
{
rigidbody2D.velocity = Vector2.up * (jumpspeed * Time.deltaTime * jumptime);
}
if (jumpbuttonup && hasSpacebeenpressedMidair < 1)
{
canifloat = true;
}
else if (jumpbuttonup && hasSpacebeenpressedMidair <= 10)
{
canifloat = false;
}
if (canifloat == true && (jumpbutton) && (floattime > 0))
{
hasSpacebeenpressedMidair = hasSpacebeenpressedMidair + (1f *Time.deltaTime);
rigidbody2D.velocity = Vector2.up * (floatspeed * Time.deltaTime);
// Debug.Log(hasSpacebeenpressedMidair);
}
if (jumpbuttonup || jumptime <= 0)
{
jumpedAlready = true;
}
void Horizontal()
{
if (leftbutton && weretomove == 0 || rightbutton && weretomove == 0)
{
if (leftbutton) { weretomove = -1; lastDirectionPressed = -1; }
if (rightbutton) { weretomove = +1; lastDirectionPressed = +1; }
}
if (leftbutton && rightbutton)
{
weretomove = 0;
playerSpeed = 0;
acceleration = 0;
}
//if(!leftbutton&&!rightbutton){ playerSpeed = 0;}
rigidbody2D.velocity =
new Vector3((playerSpeed * speedmultiplier * lastDirectionPressed * (acceleration /
maxacceleration)) * Time.deltaTime * friction,
rigidbody2D.velocity.y * slideValue, 0);
if (dashbutton) {
rigidbody2D.velocity = new Vector3(dogeSpeed * lastDirectionPressed *
Time.deltaTime, rigidbody2D.velocity.y, 0); }
if (dashbutton && (maxDogeTime - dogetime) == 0
|| dashbutton && dogeSpeed == 0) {
rigidbody2D.velocity = new Vector3(playerSpeed * speedmultiplier * weretomove *
(acceleration / maxacceleration) * friction * Time.deltaTime,
rigidbody2D.velocity.y * slideValue, 0); }
}
Jumptime is used to calculate how long the Player is still able to hold the Jumpbutton and still move up after leaving the Ground
Jump is used to give out a Vector containing the Vetical velocity
In Horizontal the Player is moved, the rigidbody.velocity.y is the "Jump" or the vector that Works against Gravity.
When run in FixedUpdate the game plays the same in both Vsynch an free mode in the Editor after compiling the Player barly moves in both axes, when i run the Methods with Time.deltaTime in Update the Game behaves different even in the Editor dependent on the mode, in Vsynch floaty and in free mode he does not really move, if run in Fixed Update both with and without Time.deltaTime the player behaves the Same in both playmodes but different after compiling where it behaves: with Time.deltaTime: doesnt move but can jump incredibly high, although only one second as intended without Time.deltaTime the horizontal Speed works again, but the player is still jumping to high by a factor so big, i couldnt really tell how much mor it was because he got stopped by a ceiling. Could this have something to do with how Unity calculates Gravity?
Answer by bakflemmli · Jul 14, 2021 at 12:38 AM
[ [1]: using UnityEngine;
public class PlayerMovement : MonoBehaviour { PlanterController plantercontroller;
[SerializeField] private LayerMask groundlayerMask;
[SerializeField] private LayerMask balllayerMask;
private BoxCollider2D boxCollider2D;
public BoxCollider2D walljumpcheckbox;
private Rigidbody2D rigidbody2D;
public Collider2D[] hitboxes;
Vector2 usualHeight;
Vector2 halfHeight;
float normallocalscale;
public Animator animator;
bool playjumpani;
bool playrunani;
bool playduckani;
public bool playbaseslashani;
public bool playduckslashani;
public bool playairslashani;
public float slashanispeed;
public float maxslashanispeed = 25f;
public float duckslashanispeed;
public float maxduckslashanispeed = 25f;
public float airslashanispeed;
public float maxairslashanispeed = 25f;
public float baseslashexposedtime = 0f;
public float playerSpeed;
float speedmultiplier=1;
public float dogeSpeed;
public float jumpspeed = 200f;
public float floatspeed= 500f;
public float acceleration = 0;
public float maxacceleration = 40f;
public float dogeSpeedMultiplier = 10f;
public float defaultplayerspeed = 1000f;
public float maxplayerspeed = 3f;
public float maxComforttime = 0.5f;
public float weretomove;
public float slideValue = 1f;
public float lastDirectionPressed = 0;
public float Ducktime;
public float RightCameraMoveTimer;
public float LeftCameraMoveTimer;
public float hasSpacebeenPressedmidairMax = 1f;
public float maxFloattime = 2f;
public float walljumpdegrader = 0.75f;
public float smashspeed = 200f;
public bool canIwallJumpLeft;
public bool canIwallJumpRight;
bool jumpedAlready = false;
bool easyWalljumpwasgranted;
public bool walljumpedalready;
bool performingGroundSmash;
public bool draging;
public bool dashing;
public bool dashend;
public bool climbingUpsideDown;
bool ducking;
//state of the Jump
public bool leavingGround;
public bool falling;
public bool flapping;
public bool landing;
bool climbing;
float jumptime;
float maxeasywalljumptime = 200f;
public float easywalljumptimer = 200;
public float comforttime;
public float maxjumpheight = 1f;
public float floattime;
bool canifloat = false;
bool haveIbeenfloating;
public float hasSpacebeenpressedMidair = 0;
public float dogetime = 0f;
public float maxDogeTime = 0.75f;
public float friction = 1f;
public float airlag;
public float maxairlag = 0.25f;
private Vector2 moveDir;
private Vector2 verticalDir;
Vector2 rsmoveDir;
//ButtonInputbools
public bool rightbuttondown;
public bool rightbutton;
public bool rightbuttonup;
public bool leftbuttondown;
public bool leftbutton;
public bool leftbuttonup;
public bool rsrightbuttondown;
public bool rsrightbutton;
public bool rsrightbuttonup;
public bool rsleftbuttondown;
public bool rsleftbutton;
public bool rsleftbuttonup;
public bool upbuttondown;
public bool upbutton;
public bool upbuttonup;
public bool rsupbuttondown;
public bool rsupbutton;
public bool rsupbuttonup;
public bool downbuttondown;
public bool downbutton;
public bool downbuttonup;
public bool rsdownbuttondown;
public bool rsdownbutton;
public bool rsdownbuttonup;
public bool attackbuttondown;
public bool attackbutton;
public bool attackbuttonup;
public bool jumpbuttondown;
public bool jumpbutton;
public bool jumpbuttonup;
public bool dashbuttondown;
public bool dashbutton;
public bool dashbuttonup;
public bool holdbutton;
public bool holdbuttondown;
public bool holdbuttonup;
public bool sprintbutton;
// UI
//Stats
public float maxStamina = 100;
// current Stats
public float currentStamina;
// Start is called before the first frame update
void Start()
{
rigidbody2D = transform.GetComponent<Rigidbody2D>();
//rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
boxCollider2D = transform.GetComponent<BoxCollider2D>();
normallocalscale = transform.localScale.x;
jumptime = maxjumpheight;
floattime = 0;
comforttime = 0;
usualHeight = boxCollider2D.size;
halfHeight.y = (usualHeight.y / 2f);
currentStamina = maxStamina;
GameObject.Find("Staminabar").GetComponent<PlantersStaminaBar>().SetMaxStamina(maxStamina);
baseslashexposedtime = 0;
}
void JumpButtonDownTrue()
{
jumpbuttondown = true;
}
void Jumptrue()
{
jumpbutton = true;
}
void Jumpfalse()
{
jumpbutton = false;
}
void JumpButtonUptrue()
{
jumpbuttonup = true;
}
void Dashtrue()
{
dashbutton = true;
}
void Dashfalse()
{
dashbutton = false;
}
void DashButtonUptrue()
{
dashbuttonup = true;
}
void Sprinttrue()
{
sprintbutton = true;
}
void Sprintfalse()
{
sprintbutton = false;
}
void Holdtrue()
{
holdbutton = true;
}
void Holdfalse()
{
holdbutton = false;
}
void AttackButtonDownTrue()
{
attackbuttondown = true;
}
void AttackTrue()
{
attackbutton = true;
}
void AttackFalse()
{
attackbutton = false;
}
void AttackButtonUpTrue()
{
attackbuttonup = true;
}
void Awake()
{
plantercontroller = new PlanterController();
plantercontroller.BaseMovementPlanter.Jump.started += lmdaex => JumpButtonDownTrue();
plantercontroller.BaseMovementPlanter.Jump.performed += lmdaex => Jumptrue();
plantercontroller.BaseMovementPlanter.Jump.canceled += lmdaex => Jumpfalse();
plantercontroller.BaseMovementPlanter.Jump.canceled += lmdaex => JumpButtonUptrue();
plantercontroller.BaseMovementPlanter.Attack.started += lmdaex => AttackButtonDownTrue();
plantercontroller.BaseMovementPlanter.Attack.performed += lmdaex => AttackTrue();
plantercontroller.BaseMovementPlanter.Attack.canceled += lmdaex => AttackFalse();
plantercontroller.BaseMovementPlanter.Attack.canceled += lmdaex => AttackButtonUpTrue();
plantercontroller.BaseMovementPlanter.Dash.performed += lmdaex => Dashtrue();
plantercontroller.BaseMovementPlanter.Dash.canceled += lmdaex => Dashfalse();
plantercontroller.BaseMovementPlanter.Dash.canceled += lmdaex => DashButtonUptrue();
plantercontroller.BaseMovementPlanter.Sprint.performed += lmdaex => Sprinttrue();
plantercontroller.BaseMovementPlanter.Sprint.canceled += lmdaex => Sprintfalse();
plantercontroller.BaseMovementPlanter.Hold.performed += lmdaex => Holdtrue();
plantercontroller.BaseMovementPlanter.Hold.canceled += lmdaex => Holdfalse();
plantercontroller.BaseMovementPlanter.Move.performed += lmdaex => moveDir = lmdaex.ReadValue<Vector2>();
plantercontroller.BaseMovementPlanter.Move.canceled += lmdaex => moveDir = Vector2.zero;
plantercontroller.BaseMovementPlanter.RightStickMove.performed += lmdaex => rsmoveDir = lmdaex.ReadValue<Vector2>();
plantercontroller.BaseMovementPlanter.Move.canceled += lmdaex => rsmoveDir = Vector2.zero;
}
void OnEnable()
{
plantercontroller.BaseMovementPlanter.Enable();
}
void FixedUpdate()
{
if (jumpbutton) { playjumpani = true; }
if (!jumpbutton || OnGround()) { playjumpani = false; }
if (downbutton) { playduckani = true; }
if (!downbutton) { playduckani = false; }
animator.SetBool("Runing", playrunani);
animator.SetBool("Jumping", playjumpani);
animator.SetBool("Duck", playduckani);
animator.SetBool("BaseSlash", playbaseslashani);
animator.SetBool("DuckSlash", playduckslashani);
animator.SetBool("AirSlash", playairslashani);
animator.SetBool("Falling", falling);
animator.SetBool("Dashing", dashing);
//StaminaRegen
if (!attackbuttondown && (currentStamina < maxStamina) == true && !attackbutton && !sprintbutton)
{ GainStam((50f * Time.deltaTime)); }
ComforttimeCalc();
Wallslide();
Climb();
if (IsGroundAbove() == true && holdbutton && !dashbutton && !jumpbutton && !OnGround() && !ContactLeft() && !ContactRight())
{
rigidbody2D.velocity = Vector2.up * 10f;
friction = 0.5f;
climbingUpsideDown = true;
}
else climbingUpsideDown = false;
JumptimeCalc();
Jumpstate();
SpeedCalc();
FloattimeCalc();
Slashs();
Jump();
GroundSmash();
DogeDash();
Horizontal();
Cameraoffset();
jumpbuttondown = false;
jumpbuttonup = false;
attackbuttondown = false;
attackbuttonup = false;
dashbuttonup = false;
if (lastDirectionPressed != 0) { transform.localScale = new Vector3(normallocalscale * lastDirectionPressed, transform.localScale.y, transform.localScale.z); }
GameObject.Find("Staminabar").GetComponent<PlantersStaminaBar>().SetStamina(currentStamina);
OnGround();
IsGroundAbove();
OnBall();
ContactLeft();
ContactLeftBall();
ContactRight();
ContactRightBall();
GenerousWalljumpleft();
GenerousBalljumpleft();
GenerousWalljumpright();
GenerousBalljumpright();
Drag();
if (moveDir.x > 0)
{
rightbutton = true;
leftbutton = false;
}
if (moveDir.x < 0)
{
leftbutton = true;
rightbutton = false;
}
if (moveDir.x == 0)
{
rightbutton = false;
leftbutton = false;
}
if (rsmoveDir.x > 0)
{
rsrightbutton = true;
rsleftbutton = false;
}
if (rsmoveDir.x < 0)
{
rsleftbutton = true;
rsrightbutton = false;
}
if (rsmoveDir.x == 0)
{
rsrightbutton = false;
rsleftbutton = false;
}
if (moveDir.y > 0)
{
upbutton = true;
}
if (moveDir.y < -0.5)
{
downbutton = true;
}
if (moveDir.y == 0)
{
downbutton = false;
upbutton = false;
}
if (rsmoveDir.y > 0)
{
rsupbutton = true;
}
if (rsmoveDir.y < -0.5)
{
rsdownbutton = true;
}
if (rsmoveDir.y == 0)
{
rsdownbutton = false;
rsupbutton = false;
}
}
void Update()
{
}
void Cameraoffset()
{
if (rsrightbutton == true)
{
float activateRightCamera = 100.0f;
if (RightCameraMoveTimer < activateRightCamera)
{
RightCameraMoveTimer = RightCameraMoveTimer + 1f;
}
}
if (rsrightbutton == false)
{
RightCameraMoveTimer = 0;
}
if (rsleftbutton == true)
{
float activateLeftCamera = 100.0f;
if (LeftCameraMoveTimer < activateLeftCamera)
{
LeftCameraMoveTimer = LeftCameraMoveTimer + 1f;
}
}
if (rsleftbutton == false)
{
LeftCameraMoveTimer = 0;
}
if (rsdownbutton == true)
{
float activateDuckcamera = 150.0f;
if (Ducktime < activateDuckcamera)
{
Ducktime = Ducktime + 1f;
}
}
if (rsdownbutton == false)
{
Ducktime = 0;
}
}
void SpeedCalc()
{
weretomove = 0;
if (jumpbuttonup)
{
easyWalljumpwasgranted = true;
easywalljumptimer = maxeasywalljumptime;
}
if (rightbutton || leftbutton || jumpbutton)
{
playerSpeed = defaultplayerspeed;
if (sprintbutton && currentStamina > 10)
{
speedmultiplier = maxplayerspeed;
DrainStamina(0.5f);
}
else speedmultiplier = 1;
}
if (!ContactLeft() && !ContactRight())
{
walljumpedalready = false;
}
if (downbutton && !climbingUpsideDown && OnGround())
{
friction = 0.25f;
Duck();
if (ducking == false)
{
transform.position = new Vector2(transform.position.x, transform.position.y - boxCollider2D.bounds.extents.y);
}
ducking = true;
}
if (!downbutton)
{
boxCollider2D.size = new Vector2(boxCollider2D.size.x, usualHeight.y);
friction = 1f;
ducking = false;
}
if (rightbutton && acceleration < maxacceleration || leftbutton && acceleration < maxacceleration)
{
acceleration = acceleration + 2f;
}
if (!rightbutton && acceleration > 0 && !leftbutton)
{
acceleration = acceleration - (1f * airlag);
}
if (!OnGround() && !OnBall() && !climbingUpsideDown)
{
airlag = maxairlag;
friction = 1f;
}
else airlag = 1;
if (!OnGround() && !OnBall() && leftbutton && !climbingUpsideDown || !OnGround() && !OnBall() && rightbutton && !climbingUpsideDown)
{
acceleration = maxacceleration;
}
if ((ContactRight() == true && !leftbutton) && (!OnGround()) || (ContactLeft() == true && !rightbutton) && (!OnGround()) || attackbuttondown && OnGround() || (baseslashexposedtime <= 0) == false && OnGround())
{ playerSpeed = 0; }
if (GenerousWalljumpright() && leftbuttondown && (jumpedAlready == false) && (walljumpedalready == false) || GenerousBalljumpright() && leftbuttondown && (jumpedAlready == false) && (walljumpedalready == false))
{
jumptime = maxjumpheight * walljumpdegrader;
walljumpedalready = true;
}
if (GenerousWalljumpleft() && rightbuttondown && (jumpedAlready == false) && (walljumpedalready == false) || GenerousBalljumpleft() && rightbuttondown && (jumpedAlready == false) && (walljumpedalready == false))
{
jumptime = maxjumpheight * walljumpdegrader;
walljumpedalready = true;
}
if (GenerousWalljumpleft() && leftbuttondown && (jumpedAlready == false) && (walljumpedalready == false) || GenerousBalljumpleft() && leftbuttondown && (jumpedAlready == false) && (walljumpedalready == false))
{
jumptime = maxjumpheight * walljumpdegrader;
walljumpedalready = true;
}
if (GenerousWalljumpright() && rightbuttondown && (jumpedAlready == false) && (walljumpedalready == false) || GenerousBalljumpright() && rightbuttondown && (jumpedAlready == false) && (walljumpedalready == false))
{
jumptime = maxjumpheight * walljumpdegrader;
walljumpedalready = true;
}
if (rightbutton && (OnGround() == true) || leftbutton && (OnGround() == true)) { playrunani = true; }
if (!rightbutton && !leftbutton || (OnGround() == false)) { playrunani = false; }
}
void Jump()
{
if (jumpbutton && !performingGroundSmash && jumpedAlready == false && jumptime > 0 )
{
rigidbody2D.velocity = Vector2.up * (jumpspeed * jumptime);
}
if (jumpbuttonup && hasSpacebeenpressedMidair < 1)
{
canifloat = true;
}
else if (jumpbuttonup && hasSpacebeenpressedMidair <= 10)
{
canifloat = false;
}
if (canifloat == true && (jumpbutton) && (floattime > 0))
{
hasSpacebeenpressedMidair = hasSpacebeenpressedMidair + (1f );
rigidbody2D.velocity = Vector2.up * (floatspeed );
// Debug.Log(hasSpacebeenpressedMidair);
}
if (jumpbuttonup || jumptime <= 0)
{
jumpedAlready = true;
}
//Jumpanimations
if (jumpbutton) { playjumpani = true; }
if (!jumpbutton || OnGround()) { playjumpani = false; }
if (downbutton && !attackbutton) { playduckani = true; }
if (!downbutton || attackbuttondown) { playduckani = false; }
}
void GroundSmash()
{
if (dashbutton && !OnGround() && downbutton && !jumpbutton)
{
rigidbody2D.velocity = Vector2.down * (smashspeed );
performingGroundSmash = true;
friction = 0.3f;
}
if (OnGround() && !dashbutton || OnBall() && !dashbutton) { performingGroundSmash = false; }
}
void DogeDash()
{
if ((!downbutton))
{
if ((dashbutton) && (dogetime < maxDogeTime))
{
if ((currentStamina > 50) == true)
{
dogetime = dogetime + (1f );
dogeSpeed = defaultplayerspeed *dogeSpeedMultiplier * ((maxDogeTime - dogetime) * 0.1f);
DrainStamina(20*dogetime);
dashing = true;
acceleration = maxacceleration;
}
}
else dashing = false;
if (dashbuttonup)
{
dashing = false;
}
if (!dashbutton && (currentStamina > 10) == true)
{
dogetime = 0;
}
}
if (downbutton && OnGround())
{
dogetime = maxDogeTime;
}
if (currentStamina < 50) { dogeSpeed = 0; }
}
void Wallslide()
{
if ((ContactRight() == true && rightbutton && !holdbutton && !attackbutton) && !jumpbutton || (ContactLeft() == true && leftbutton && !holdbutton & !attackbutton) && !jumpbutton)
{
if (!downbutton)
{
slideValue = 0.25f;
}
if (downbutton)
{
slideValue = 0.75f;
}
}
else slideValue = 1f;
}
void Climb()
{
if (ContactRight() == true && holdbutton && !jumpbutton || ContactLeft() == true && holdbutton && !jumpbutton)
{
rigidbody2D.velocity = Vector2.up * 1.9f;
if (upbutton)
{
rigidbody2D.velocity = Vector2.up * 5f;
}
if (downbutton)
{
rigidbody2D.velocity = Vector2.down * 10f;
}
}
}
void Duck()
{
boxCollider2D.size = new Vector2(boxCollider2D.size.x, halfHeight.y);
dashing = false;
}
void Horizontal()
{
if (leftbutton && weretomove == 0 || rightbutton && weretomove == 0)
{
if (leftbutton) { weretomove = -1; lastDirectionPressed = -1; }
if (rightbutton) { weretomove = +1; lastDirectionPressed = +1; }
}
if (leftbutton && rightbutton)
{
weretomove = 0;
playerSpeed = 0;
acceleration = 0;
}
//if(!leftbutton&&!rightbutton){ playerSpeed = 0;}
rigidbody2D.velocity = new Vector3((playerSpeed*speedmultiplier* lastDirectionPressed * (acceleration / maxacceleration)) * friction, rigidbody2D.velocity.y * slideValue , 0);
if (dashbutton) { rigidbody2D.velocity = new Vector3(dogeSpeed * lastDirectionPressed , rigidbody2D.velocity.y, 0); }
if (dashbutton && (maxDogeTime - dogetime) == 0 || dashbutton && dogeSpeed == 0) { rigidbody2D.velocity = new Vector3(playerSpeed*speedmultiplier * weretomove * (acceleration / maxacceleration)*friction, rigidbody2D.velocity.y * slideValue , 0); }
// transform.position = transform.position + new Vector3(playerSpeed * lastDirectionPressed*(acceleration/maxacceleration)*friction *Time.deltaTime , rigidbody2D.velocity.y * slideValue*Time.deltaTime,0 );
//if (dashbutton) {transform.position = transform.position + new Vector3(dogeSpeed * lastDirectionPressed*Time.deltaTime, rigidbody2D.velocity.y*Time.deltaTime,0); }
//if (dashbutton && (maxDogeTime - dogetime) == 0||dashbutton && dogeSpeed ==0) {transform.position = transform.position + new Vector3(playerSpeed * weretomove , rigidbody2D.velocity.y * slideValue ,0); }
}
bool OnGround()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0f, Vector2.down, 0.1f, groundlayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.green;
}
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.down * (boxCollider2D.bounds.extents.y + 0.1f));
return raycastHit.collider != null;
}
bool IsGroundAbove()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 1f, Vector2.up, 1f, groundlayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.green;
}
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.up * (boxCollider2D.bounds.extents.y + 0.1f));
return raycastHit.collider != null;
}
bool OnBall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0f, Vector2.down, 0.1f, balllayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.green;
}
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.down * (boxCollider2D.bounds.extents.y + 0.1f));
return raycastHit.collider != null;
}
bool ContactLeft()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0.1f, Vector2.left, 0.1f, groundlayerMask);
Color rayColor;
if (raycastHit.collider != null)
{ rayColor = Color.green; }
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.left * (boxCollider2D.bounds.extents.x + 0.1f));
return raycastHit.collider != null;
}
bool ContactRight()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0.1f, Vector2.right, 0.1f, groundlayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.green;
}
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.right * (boxCollider2D.bounds.extents.x + 0.1f));
return raycastHit.collider != null;
}
bool ContactRightBall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0.1f, Vector2.right, 0.1f, balllayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.green;
}
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.right * (boxCollider2D.bounds.extents.x + 0.1f));
return raycastHit.collider != null;
}
bool ContactLeftBall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0.1f, Vector2.left, 0.1f, balllayerMask);
Color rayColor;
if (raycastHit.collider != null)
{ rayColor = Color.green; }
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.left * (boxCollider2D.bounds.extents.x + 0.1f));
return raycastHit.collider != null;
}
bool GenerousWalljumpright()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 1.5f, Vector2.right, 1.5f, groundlayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.blue;
}
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.right * (boxCollider2D.bounds.extents.x + 1.5f));
return raycastHit.collider != null;
}
bool GenerousWalljumpleft()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 1.5f, Vector2.left, 1.5f, groundlayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.blue;
}
else rayColor = Color.red;
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.left * (boxCollider2D.bounds.extents.x + 1.5f));
return raycastHit.collider != null;
}
bool GenerousBalljumpleft()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0.1f, Vector2.left, 0.1f, balllayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.blue; //Debug.Log("theres something right");
}
else rayColor = Color.red;
// Debug.Log("theres nothing right");
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.left * (boxCollider2D.bounds.extents.x + 0.1f));
return raycastHit.collider != null;
}
bool GenerousBalljumpright()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0.1f, Vector2.right, 0.1f, balllayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.blue; //Debug.Log("theres something right");
}
else rayColor = Color.red;
// Debug.Log("theres nothing right");
Debug.DrawRay(boxCollider2D.bounds.center, Vector2.right * (boxCollider2D.bounds.extents.x + 0.1f));
return raycastHit.collider != null;
}
// used to get if Player is rising,falling,or landing
void Jumpstate()
{
//rising
if (OnGround() == true && (jumpbuttondown == true))
{
leavingGround = true;
}
//jumpclimax
if (jumptime <= 0 || jumpbuttonup)
{
leavingGround = false;
playjumpani = false;
}
//falling
if ((!OnGround()) && (!jumpbutton))
{
falling = true;
}
else falling = false;
//flapping/gliding
if (canifloat == true && (jumpbutton) && (floattime > 0))
{
flapping = true;
}
else flapping = false;
}
void JumptimeCalc()
{
if (OnGround() == false && jumptime > 0)
{
jumptime = jumptime - (1f );
//Debug.Log(jumptime);
}
if (OnGround() == true && !jumpbutton || OnBall() && !jumpbutton || comforttime > 0 && comforttime <= maxComforttime && !jumpbutton)
{
jumptime = maxjumpheight;
jumpedAlready = false;
}
if (comforttime<=0&&!jumpbutton&&!OnGround())
{
jumptime = 0;
}
}
void FloattimeCalc()
{
if (canifloat == true)
{
floattime = maxFloattime;
}
else if (floattime > 0 && jumpbutton && canifloat == true)
{
floattime = floattime - 1f;
}
if (OnGround() == true && !jumpbutton || ContactLeft() == true || ContactRight() == true || OnBall() && !jumpbutton)
{
floattime = 0f;
}
if (floattime == 0 || hasSpacebeenpressedMidair > hasSpacebeenPressedmidairMax)
{
canifloat = false;
hasSpacebeenpressedMidair = 0;
}
}
void ComforttimeCalc()
{
if (jumpbutton) { comforttime = 0; }
if (!jumpbutton)
{
if (ContactLeft() == true || ContactRight() == true || ContactLeftBall() == true || ContactRightBall() == true)
{
comforttime = maxComforttime;
}
}
if (OnGround() == true && (!jumpbutton))
{
comforttime = maxComforttime / 2;
}
if (ContactLeft() == false || ContactRight() == false || ContactLeftBall() == false || ContactRightBall() == false || OnGround() == false)
{
comforttime = comforttime - (1f);
}
}
void AttackOutput()
{
if (attackbutton)
{ Slash1(hitboxes[0]); }
}
void Slash1(Collider2D col)
{
Collider2D[] col1 = Physics2D.OverlapBoxAll(col.bounds.center, col.bounds.extents, 0f, LayerMask.GetMask("Hitboxslash1"));
foreach (Collider2D c in col1)
{
if (c.transform.root == transform)
{
continue;
Debug.Log(c.name);
}
}
}
void Slashs()
{
//BaseSlash
if (OnGround())
{
if (attackbuttondown && !jumpbutton && currentStamina >= 5 && !downbutton && slashanispeed != maxslashanispeed)
{
slashanispeed = maxslashanispeed;
DrainStamina(1);
baseslashexposedtime = 20f;
}
if (attackbutton)
{
slashanispeed = slashanispeed - 1f;
if (baseslashexposedtime > 0) { baseslashexposedtime -= 1f; }
}
if (attackbuttonup)
{
slashanispeed = 0f;
}
if (slashanispeed > 0)
{
playbaseslashani = true;
}
if (slashanispeed <= 0)
{
playbaseslashani = false;
}
if (baseslashexposedtime > 0) { baseslashexposedtime -= 1f; }
}
//Duckslash
if (attackbuttondown && currentStamina >= 5 && downbutton && duckslashanispeed != maxduckslashanispeed)
{
duckslashanispeed = maxduckslashanispeed;
DrainStamina(10);
baseslashexposedtime = 20f;
}
if (attackbutton)
{
duckslashanispeed = duckslashanispeed - 1f;
if (baseslashexposedtime > 0) { baseslashexposedtime -= 1f; }
}
if (attackbuttonup)
{
duckslashanispeed = 0f;
}
if (duckslashanispeed > 0)
{
playduckslashani = true;
}
if (duckslashanispeed <= 0)
{
playduckslashani = false;
}
if (baseslashexposedtime > 0) { baseslashexposedtime -= 1f; }
//AirSlash
if (!OnGround())
{
if (attackbuttondown && currentStamina >= 7 && !jumpbutton && !downbutton && airslashanispeed != maxairslashanispeed)
{
airslashanispeed = maxairslashanispeed;
DrainStamina(0.5f);
baseslashexposedtime = 35f;
}
if (attackbutton)
{
airslashanispeed = airslashanispeed - 1f;
if (baseslashexposedtime > 0) { baseslashexposedtime -= 1f; }
}
if (attackbuttonup)
{
airslashanispeed = 0f;
rigidbody2D.constraints = RigidbodyConstraints2D.None;
rigidbody2D.rotation = 0f;
rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
}
if (airslashanispeed > 0)
{
playairslashani = true;
rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionY;
}
if (airslashanispeed <= 0)
{
playairslashani = false;
rigidbody2D.constraints = RigidbodyConstraints2D.None;
rigidbody2D.rotation = 0f;
rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
}
if (baseslashexposedtime > 0) { baseslashexposedtime -= 1f; }
if (baseslashexposedtime <= 0)
{
rigidbody2D.constraints = RigidbodyConstraints2D.None;
rigidbody2D.rotation = 0f;
rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
}
}
}
void Drag()
{
if (holdbutton && ContactLeftBall() || holdbutton && ContactRightBall())
{
draging = true;
}
if (!holdbutton || !holdbutton)
{
draging = false;
}
}
void DrainStamina(float drainstam)
{
currentStamina -= drainstam;
}
void GainStam(float gainstam)
{
currentStamina += gainstam;
}
}][1]
[1]: /storage/temp/183417-playermovement.txt
This is the script im having Problems with, sorry that its the full script, im new to this and dont know what the problem might be, my guess is that it calculates the speed variables not as i am expecting, wich is why the actions can be performed as long as they are supposed to, but have to high numbers given out to work with.
Link what V-sink does: https://www.google.com/amp/s/www.digitaltrends.com/computing/what-is-vsync/%3famp
Your link is emty. Pleas paste your code inside the textfield whit the little code icon on top of it. I will watch your code and look what the problem could be. But be aweared im also not an expert. ;)
Thanks for the link and help, for better orientation the Methods i believe cause issues are Jump in l.625, Horizontal in l.770, and jumpspeedcalc in l.1000
It seems you working alot whit booleans its not bad but for such a komplex script you trying to achive it will be bether to write abstrakt "good readable" code who is less error prone.
For egsample [SerializeField]float moveSpeed = 10
Private void FixedUpdate()
{
// Set here all your action Methodes that have to do whit physiks (Rigidbody)
Movement();
Jump();
//more methodes
}
Private void Movement()
{
if (Input.GetKey(KeyCode. W))
{
rigidBody.AddLocalForce(Vector 3.forward * movespeed)
}
// checks every FixedUpdate if
you holding the key "S"
if (Input.GetKey(KeyCode.S)) // S
key ispesset = true
{
// moves your rigidbody
backwords * movespeed
rigidBody.AddLocalForce(Vector3.back * movespeed)
}
}
Note: like the most time i write comments on my phone and have no intelisenser that corrigate my code it maybe not 100% correct written.
I hardly recoment you to gain more experience befor u go and script such a complex game. But no worry i got the right course for u. Its free and it does really fun, why i now this you might ask ? Because i fall in the same kind of problems (i think like everybody who learning gamedev ^^) and absolve the same cours and this was one thing that really give me a big push.
Link to Unity's Junior Programmer Cours https://learn.unity.com/pathway/junior-programmer
Thanks ill definitely go through the course, and try out if Addlocal Force solves the issues.
Answer by dero273 · Jul 13, 2021 at 01:48 PM
As i now you dont need to set time.delta time in fixed update because it should already be frame independent. Maybe this fix your problem.
I deleted the deltatime multiplier out of all functions that used it, now at least the walkspeed works again, the Jumpfunktion still behaves odd and elevates the player way to fast and way to high, i think the problem is a little deeper but thanks, ill post something again if i figure out what the problem is exactly.
Still strange, that it seems to behave frameindependent in the Editor but not on the actual build, or am i misunderstanding Vsynch?
Your answer
Follow this Question
Related Questions
FixedUpdate and Time.deltaTime slowe 2 Answers
Problem with Fixed Timestep 0 Answers
Slow update best practice 0 Answers
Can FixedUpdate be synhcronized across devices 1 Answer
How to reset attack animations using time.deltatime 2 Answers