hi i am trying to make a simple level but whe my charecter hit the wall it randomly fly or stick or can wallrun. idk how to fix it can somebody help?
New WinRAR ZIP archive.zip (18.3 MB)
here is a video of that
i uploded it in a zip becuse i could not uplode .mp4 here
here is my movement code
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
public class move : MonoBehaviour
{
public float wallrunF, wallrunT, wallrunS;
public LayerMask wall;
bool wallR, wallL;
bool wallruning;
public float maxcamtilt, camtilt = 0;
public Transform ori;
public float speed;
float horizantalI;
public float jump, jcolddown;
public float airm;
public LayerMask jumppadd;
bool redytojump;
float verticalI;
Vector3 moveD;
Rigidbody rb;
public float playerH;
public float dground;
public LayerMask ground;
bool grounded;
public Transform groundCheck;
public float groundd;
public KeyCode jumpkey = KeyCode.Space;
public KeyCode dashkey = KeyCode.LeftShift;
public float dashforce;
bool dashable, dashcold;
public float dashhcolddown;
public float jumppad;
public GameObject cam;
bool jumppaded = false;
bool jumppaddding = true;
private void Start()
{
dashable = true;
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
redytojump = true;
}
void Update()
{
grounded = Physics.Raycast(groundCheck.position, Vector3.down, groundd, ground );
jumppaded = Physics.Raycast(groundCheck.position, Vector3.down, groundd, jumppadd);
PInpute();
speedcotrole();
if (grounded)
{
rb.drag = dground;
}
else
{
rb.drag = 0;
}
if (grounded)
{
dashable = true;
}
wallinpute();
checkforwall();
Debug.Log(Physics.Raycast(groundCheck.position, Vector3.down));
}
private void FixedUpdate()
{
Pmove();
}
private void PInpute()
{
horizantalI = Input.GetAxisRaw("Horizontal");
verticalI = Input.GetAxisRaw("Vertical");
if (Input.GetKey(jumpkey) && redytojump && grounded)
{
redytojump = false;
jumping();
Invoke(nameof(ResetJ), jcolddown);
}
if (Input.GetKey(dashkey) && dashable && !dashcold)
{
dashcold = true;
dashable = false;
dash();
Invoke(nameof(dashColddown), dashhcolddown);
}
if(jumppaded && jumppaddding)
{
jumppading();
}
}
private void Pmove()
{
moveD = ori.forward * verticalI + ori.right * horizantalI;
if (grounded)
{
rb.AddForce(moveD.normalized * speed * 10f, ForceMode.Force);
}
else if (!grounded)
{
rb.AddForce(moveD.normalized * speed * 10f * airm, ForceMode.Force);
}
}
private void speedcotrole()
{
Vector3 flatvelocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (flatvelocity.magnitude > speed)
{
Vector3 limit = flatvelocity.normalized * speed;
rb.velocity = new Vector3(limit.x, rb.velocity.y, limit.z);
}
}
private void jumping()
{
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(transform.up * jump, ForceMode.Impulse);
if (wallruning) {
if (wallL && !Input.GetKey(KeyCode.D) || wallR && !Input.GetKey(KeyCode.A))
{
rb.AddForce(Vector2.up * jump * 1.5f);
}
if (wallR || wallL && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) rb.AddForce(-ori.up * jump * 1f);
if (wallR && Input.GetKey(KeyCode.A)) rb.AddForce(-ori.right * jump * 3.2f);
if (wallL && Input.GetKey(KeyCode.D)) rb.AddForce(ori.right * jump * 3.2f);
rb.AddForce(ori.forward * jump * 1f);
}
}
private void ResetJ()
{
redytojump = true;
}
private void dash()
{
rb.AddForce(moveD.normalized * dashforce, ForceMode.Impulse);
rb.velocity = new Vector3(0f, 0f, 0f);
}
private void dashColddown()
{
dashcold = false;
}
private void jumppading()
{
jumppaddding = false;
rb.AddForce(transform.up * (jumppad) * (rb.velocity.y + 1) , ForceMode.Impulse);
jumppaddding = true;
}
private void wallinpute()
{
if (Input.GetKey(KeyCode.D) && wallR) startwallrun();
if (Input.GetKey(KeyCode.A) && wallL) startwallrun();
}
private void startwallrun()
{
rb.useGravity = false;
wallruning = true;
if (rb.velocity.magnitude <= wallrunS)
{
rb.AddForce(ori.forward * wallrunF * Time.deltaTime);
}
if (wallR)
{
rb.AddForce(ori.right * wallrunF / 5 * Time.deltaTime);
}
if (wallL)
{
rb.AddForce(-ori.right * wallrunF / 5 * Time.deltaTime);
}
}
private void atopwallrun()
{
rb.useGravity = true;
wallruning = false;
}
private void checkforwall()
{
wallR = Physics.Raycast(transform.position, ori.right, wall);
wallL = Physics.Raycast(transform.position, -ori.right, wall);
if (!wallR && !wallL)
{
atopwallrun();
}
}
}