❔ slide bug in csharp script in unity

im making a game in unity and i have a bug in my script where my character glides over the ground when i hold Space + left or right + and then let go of left or right
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Smoovement : MonoBehaviour
{
public float walkSpeed;
public float moveInput;
public bool isGrounded;
private Rigidbody2D rb;
public LayerMask groundmask;
public float jumplength;
public PhysicsMaterial2D bounceMat, normalMat;
public bool canJump = true;
public float jumpValue = 0.0f;


public float distance = 0.6f;
public LayerMask wallLayer;
public bool isTouchingWallL = false;
public bool isTouchingWallR = false;




// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");

if(jumpValue == 0.0f && isGrounded){
rb.velocity = new Vector2(moveInput * walkSpeed, rb.velocity.y);
}
isGrounded = Physics2D.OverlapBox(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f),
new Vector2(0.9f, 0.4f), 0f, groundmask);

isTouchingWallR = Physics2D.Raycast(transform.position, Vector2.right, distance, wallLayer);
isTouchingWallL = Physics2D.Raycast(transform.position, Vector2.left, distance, wallLayer);


if(jumpValue >= 0 && !isGrounded && (isTouchingWallL || isTouchingWallR))
{
rb.sharedMaterial = bounceMat;
}else
{
rb.sharedMaterial = normalMat;
}



if(Input.GetKey("space") && isGrounded && canJump)
{
jumpValue += 0.3f;

}

if(Input.GetKeyDown("space") && isGrounded && canJump)
{
rb.velocity = new Vector2(0.0f, rb.velocity.y);

}

if(jumpValue >= jumplength && isGrounded)
{
float tempx = moveInput * walkSpeed;
float tempy = jumpValue;
rb.velocity = new Vector2(tempx, tempy);
Invoke("ResetJump", 0.3f);

}

if(Input.GetKeyUp("space")){
if(isGrounded){
rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue);
jumpValue = 0.0f;
}
canJump = true;
}
if(jumpValue == 0){
canJump = true;
}


}

void ResetJump()
{
canJump = false;
jumpValue = 0;


}

void OndrawnGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawCube(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f), new Vector2(0.9f, 0.2f));

}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Smoovement : MonoBehaviour
{
public float walkSpeed;
public float moveInput;
public bool isGrounded;
private Rigidbody2D rb;
public LayerMask groundmask;
public float jumplength;
public PhysicsMaterial2D bounceMat, normalMat;
public bool canJump = true;
public float jumpValue = 0.0f;


public float distance = 0.6f;
public LayerMask wallLayer;
public bool isTouchingWallL = false;
public bool isTouchingWallR = false;




// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");

if(jumpValue == 0.0f && isGrounded){
rb.velocity = new Vector2(moveInput * walkSpeed, rb.velocity.y);
}
isGrounded = Physics2D.OverlapBox(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f),
new Vector2(0.9f, 0.4f), 0f, groundmask);

isTouchingWallR = Physics2D.Raycast(transform.position, Vector2.right, distance, wallLayer);
isTouchingWallL = Physics2D.Raycast(transform.position, Vector2.left, distance, wallLayer);


if(jumpValue >= 0 && !isGrounded && (isTouchingWallL || isTouchingWallR))
{
rb.sharedMaterial = bounceMat;
}else
{
rb.sharedMaterial = normalMat;
}



if(Input.GetKey("space") && isGrounded && canJump)
{
jumpValue += 0.3f;

}

if(Input.GetKeyDown("space") && isGrounded && canJump)
{
rb.velocity = new Vector2(0.0f, rb.velocity.y);

}

if(jumpValue >= jumplength && isGrounded)
{
float tempx = moveInput * walkSpeed;
float tempy = jumpValue;
rb.velocity = new Vector2(tempx, tempy);
Invoke("ResetJump", 0.3f);

}

if(Input.GetKeyUp("space")){
if(isGrounded){
rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue);
jumpValue = 0.0f;
}
canJump = true;
}
if(jumpValue == 0){
canJump = true;
}


}

void ResetJump()
{
canJump = false;
jumpValue = 0;


}

void OndrawnGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawCube(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f), new Vector2(0.9f, 0.2f));

}
}
22 Replies
FusedQyou
FusedQyou2y ago
Maybe it has to do with your code not being consistently formatted and there being loose spaces, brackets and all that 🥹 In all seriousness though, this could very much cause mistakes Sound slike movement input is not being read if the player jumped
Smikkelbakje
SmikkelbakjeOP2y ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Smoovement : MonoBehaviour
{
public float walkSpeed;
public float moveInput;
public bool isGrounded;
private Rigidbody2D rb;
public LayerMask groundmask;
public float jumplength;
public PhysicsMaterial2D bounceMat, normalMat;
public bool canJump = true;
public float jumpValue = 0.0f;
public float distance = 0.6f;
public LayerMask wallLayer;
public bool isTouchingWallL = false;
public bool isTouchingWallR = false;

// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");

if(jumpValue == 0.0f && isGrounded)
{
rb.velocity = new Vector2(moveInput * walkSpeed, rb.velocity.y);
}

isGrounded = Physics2D.OverlapBox(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f),
new Vector2(0.9f, 0.4f), 0f, groundmask);

isTouchingWallR = Physics2D.Raycast(transform.position, Vector2.right, distance, wallLayer);
isTouchingWallL = Physics2D.Raycast(transform.position, Vector2.left, distance, wallLayer);

if(jumpValue >= 0 && !isGrounded && (isTouchingWallL || isTouchingWallR))
{
rb.sharedMaterial = bounceMat;
}
else
{
rb.sharedMaterial = normalMat;
}

if(Input.GetKey("space") && isGrounded && canJump)
{
jumpValue += 0.3f;

}

if(Input.GetKeyDown("space") && isGrounded && canJump)
{
rb.velocity = new Vector2(0.0f, rb.velocity.y);

}

if(jumpValue >= jumplength && isGrounded)
{
float tempx = moveInput * walkSpeed;
float tempy = jumpValue;
rb.velocity = new Vector2(tempx, tempy);
Invoke("ResetJump", 0.3f);
}

if(Input.GetKeyUp("space")){
if(isGrounded){
rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue);
jumpValue = 0.0f;
}
canJump = true;
}

if(jumpValue == 0){
canJump = true;
}
}

void ResetJump()
{
canJump = false;
jumpValue = 0;
}

void OndrawnGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawCube(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f), new Vector2(0.9f, 0.2f));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Smoovement : MonoBehaviour
{
public float walkSpeed;
public float moveInput;
public bool isGrounded;
private Rigidbody2D rb;
public LayerMask groundmask;
public float jumplength;
public PhysicsMaterial2D bounceMat, normalMat;
public bool canJump = true;
public float jumpValue = 0.0f;
public float distance = 0.6f;
public LayerMask wallLayer;
public bool isTouchingWallL = false;
public bool isTouchingWallR = false;

// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");

if(jumpValue == 0.0f && isGrounded)
{
rb.velocity = new Vector2(moveInput * walkSpeed, rb.velocity.y);
}

isGrounded = Physics2D.OverlapBox(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f),
new Vector2(0.9f, 0.4f), 0f, groundmask);

isTouchingWallR = Physics2D.Raycast(transform.position, Vector2.right, distance, wallLayer);
isTouchingWallL = Physics2D.Raycast(transform.position, Vector2.left, distance, wallLayer);

if(jumpValue >= 0 && !isGrounded && (isTouchingWallL || isTouchingWallR))
{
rb.sharedMaterial = bounceMat;
}
else
{
rb.sharedMaterial = normalMat;
}

if(Input.GetKey("space") && isGrounded && canJump)
{
jumpValue += 0.3f;

}

if(Input.GetKeyDown("space") && isGrounded && canJump)
{
rb.velocity = new Vector2(0.0f, rb.velocity.y);

}

if(jumpValue >= jumplength && isGrounded)
{
float tempx = moveInput * walkSpeed;
float tempy = jumpValue;
rb.velocity = new Vector2(tempx, tempy);
Invoke("ResetJump", 0.3f);
}

if(Input.GetKeyUp("space")){
if(isGrounded){
rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue);
jumpValue = 0.0f;
}
canJump = true;
}

if(jumpValue == 0){
canJump = true;
}
}

void ResetJump()
{
canJump = false;
jumpValue = 0;
}

void OndrawnGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawCube(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f), new Vector2(0.9f, 0.2f));
}
}
i tried cleaning it up i have no idea how to fix this i have sat here a couple of hours trying to find solutions
lemminglion
lemminglion2y ago
rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue); is possibly wrong in your use case here. When you do this its immediately overriding the horizontal (sliding) momentum by setting a new velocity directly. Its not ideal to set velocity directly unless that's exactly what you need to do. Instead, use AddForce https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html The line you want is likely: rb.AddForce(rb.transform.up * jumpValue);
Smikkelbakje
SmikkelbakjeOP2y ago
if i replace rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue); with rb.AddForce(rb.transform.up * jumpValue); it still does the same thing
lemminglion
lemminglion2y ago
I would try replacing all the rb.velocity = with AddForce calls. You will need to adjust the multipliers (in this case WalkSpeed) to possibly much bigger numbers as AddForce imparts energy to the rigidbody as though something hit it in X direction
Smikkelbakje
SmikkelbakjeOP2y ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Smoovement : MonoBehaviour
{
public float walkSpeed;
public float moveInput;
public bool isGrounded;
private Rigidbody2D rb;
public LayerMask groundmask;
public float jumplength;
public PhysicsMaterial2D bounceMat, normalMat;
public bool canJump = true;
public float jumpValue = 0.0f;
public float distance = 0.6f;
public LayerMask wallLayer;
public bool isTouchingWallL = false;
public bool isTouchingWallR = false;

// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");

if(jumpValue == 0.0f && isGrounded)
{
rb.AddForce(new Vector2(moveInput * walkSpeed, 0), ForceMode2D.Impulse);
}

isGrounded = Physics2D.OverlapBox(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f),
new Vector2(0.9f, 0.4f), 0f, groundmask);

isTouchingWallR = Physics2D.Raycast(transform.position, Vector2.right, distance, wallLayer);
isTouchingWallL = Physics2D.Raycast(transform.position, Vector2.left, distance, wallLayer);

if(jumpValue >= 0 && !isGrounded && (isTouchingWallL || isTouchingWallR))
{
rb.sharedMaterial = bounceMat;
}
else
{
rb.sharedMaterial = normalMat;
}

if(Input.GetKey("space") && isGrounded && canJump)
{
jumpValue += 2.3f;

}

if(Input.GetKeyDown("space") && isGrounded && canJump)
{
rb.AddForce(new Vector2(0, 0), ForceMode2D.Impulse);

}

if(jumpValue >= jumplength && isGrounded)
{
float tempx = moveInput * walkSpeed;
float tempy = jumpValue;
rb.AddForce(new Vector2(tempx, tempy), ForceMode2D.Impulse);
Invoke("ResetJump", 0.3f);
}

if(Input.GetKeyUp("space")){
if(isGrounded){
rb.AddForce(new Vector2(moveInput * walkSpeed, jumpValue), ForceMode2D.Impulse);
jumpValue = 0.0f;
}
canJump = true;
}

if(jumpValue == 0){
canJump = true;
}
}

void ResetJump()
{
canJump = false;
jumpValue = 0;
}

void OndrawnGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawCube(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f), new Vector2(0.9f, 0.2f));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Smoovement : MonoBehaviour
{
public float walkSpeed;
public float moveInput;
public bool isGrounded;
private Rigidbody2D rb;
public LayerMask groundmask;
public float jumplength;
public PhysicsMaterial2D bounceMat, normalMat;
public bool canJump = true;
public float jumpValue = 0.0f;
public float distance = 0.6f;
public LayerMask wallLayer;
public bool isTouchingWallL = false;
public bool isTouchingWallR = false;

// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");

if(jumpValue == 0.0f && isGrounded)
{
rb.AddForce(new Vector2(moveInput * walkSpeed, 0), ForceMode2D.Impulse);
}

isGrounded = Physics2D.OverlapBox(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f),
new Vector2(0.9f, 0.4f), 0f, groundmask);

isTouchingWallR = Physics2D.Raycast(transform.position, Vector2.right, distance, wallLayer);
isTouchingWallL = Physics2D.Raycast(transform.position, Vector2.left, distance, wallLayer);

if(jumpValue >= 0 && !isGrounded && (isTouchingWallL || isTouchingWallR))
{
rb.sharedMaterial = bounceMat;
}
else
{
rb.sharedMaterial = normalMat;
}

if(Input.GetKey("space") && isGrounded && canJump)
{
jumpValue += 2.3f;

}

if(Input.GetKeyDown("space") && isGrounded && canJump)
{
rb.AddForce(new Vector2(0, 0), ForceMode2D.Impulse);

}

if(jumpValue >= jumplength && isGrounded)
{
float tempx = moveInput * walkSpeed;
float tempy = jumpValue;
rb.AddForce(new Vector2(tempx, tempy), ForceMode2D.Impulse);
Invoke("ResetJump", 0.3f);
}

if(Input.GetKeyUp("space")){
if(isGrounded){
rb.AddForce(new Vector2(moveInput * walkSpeed, jumpValue), ForceMode2D.Impulse);
jumpValue = 0.0f;
}
canJump = true;
}

if(jumpValue == 0){
canJump = true;
}
}

void ResetJump()
{
canJump = false;
jumpValue = 0;
}

void OndrawnGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawCube(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.5f), new Vector2(0.9f, 0.2f));
}
}
changed them all to rb.AddForce the player is now on iceskates
lemminglion
lemminglion2y ago
Yep thats more 'normal' In the Rigidbody in Unity's inspector, increase the drag
lemminglion
lemminglion2y ago
That's probably 0 if you've never changed it
Smikkelbakje
SmikkelbakjeOP2y ago
yea
lemminglion
lemminglion2y ago
So depending on how you want your character to move, you have to balance the amount of force being added vs the amount of drag. There's quite a few ways to do it so its more about trying different solutions until you get one that feels right
Smikkelbakje
SmikkelbakjeOP2y ago
the jump has became very inconsistand and it doesnt move left or right anymore
Smikkelbakje
SmikkelbakjeOP2y ago
im trying to figure out why but cant
lemminglion
lemminglion2y ago
So there are a lot of things going on in that script, some of which from what I can see of your game isn't required for what you're wanting to do. Give me a few mins I'll write up some example code
Smikkelbakje
SmikkelbakjeOP2y ago
ok thanks! i will be back in ~1 hour
lemminglion
lemminglion2y ago
Not as feature complete as yours, it lacks the wall finding stuff. However I think that's doing what you wanted 🙂 You may have to adjust the values in the inspector as I was just doing it with a 1x1 cube
Smikkelbakje
SmikkelbakjeOP2y ago
it works but it doesnt have the jump charge things im gonna try to implement that in your script and see how it works im having a lot of trouble trying to combine them the player shouldnt be able to move in the air its a jump king style script
lemminglion
lemminglion2y ago
Add a grounded check where it starts looking at the velocity sign, that will give you immediate control when grounded but not in the air
lemminglion
lemminglion2y ago
lemminglion
lemminglion2y ago
@Smikkelbakje If you're still stuck with this, I can confirm this works kinda like how Jump King does. Its not 1 to 1 but despite how simple it may look its actually a fairly hard jump routine. You will need to configure some gameobjects in the scene to act as Left/Right Walls or Floors via tags. I've sent the sample scene to use it too (it may require updating to whatever version of Unity you have installed)
lemminglion
lemminglion2y ago
Its unrefined but implements various things from JumpKing: 1. walk left/right when not jumping 2. Jump up 3. Jump and leap in a direction (assuming the player did it fast enough) 4. Don't allow air control, once the player is leaping they're stuck leaping in that direction 5. Kill all momentum when the player touches the ground 6. Bounce off walls if you hit them
lemminglion
lemminglion2y ago
Apologies, fixed some issues with jump handling, it needs some tuning of variables but this acts basically the same as Jump King
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server