(Sorry for mistakes because I`m not native English speaker)
I`m writing physycal movement script(using RigidBody). My character seemed very slippery, as if he was walking on ice and slipping after stopping. So, I added a few lines to the code to wrap the acceleration if the moving keys(WASD) are not pressed and stop moving. Here yhey are:
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded)
{
rigidbody.velocity = myVector;
myVector.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeaccelerationVolx, walkDeacceleration);
myVector.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeaccelerationVolz, walkDeacceleration);
}
I think that my “grounded” does not have time to change the value to “false” and my “walkDeacceleraion” applied to jump too.
But if my character is moving there are no problems with the jump.
I want to add that if you make many attempts to jump, then with some attempt it will work, but it’s all by random.
Help me make a normal jump that will work as it should!)
And yes, here is the whole code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour
{
Rigidbody rigidbody;
public GameObject camera;
public float walkAccelAirRatio=0.1f;
public float walkAcceleration = 5;
public float walkDeacceleration = 5;
float walkDeaccelerationVolx;
float walkDeaccelerationVolz;
public float maxWalkSpeed = 20;
float velocityX ;
float velocityZ ;
Vector2 horizontalMovement;
Vector3 myVector;
public float jumpVelocity=20;
public bool grounded=false;
public float maxSlope = 60;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
void Update()
{
horizontalMovement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalMovement.magnitude>maxWalkSpeed)
{
horizontalMovement=horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity = new Vector3(horizontalMovement.x, rigidbody.velocity.y, horizontalMovement.y);
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded)
{
rigidbody.velocity = myVector;
myVector.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeaccelerationVolx, walkDeacceleration);
myVector.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeaccelerationVolz, walkDeacceleration);
}
transform.rotation = Quaternion.Euler(0, camera.GetComponent<MouseLookScript>().currentYRotation, 0);
if(grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio * Time.deltaTime);
if (Input.GetButtonDown("Jump")&&grounded)
{
rigidbody.AddForce(0, jumpVelocity, 0);
}
}
private void OnCollisionStay(Collision collision)
{
foreach(ContactPoint contact in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
private void OnCollisionExit(Collision collision)
{
grounded = false;
}
}