this is miìy script for character controller but
when hen he climbs a hill if he is more inclined than 10 ° he does not go forward, he gets stuck and falls under the ground, instead, when he has to go down the hill he does it in jerks how can I solve
thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Diagnostics;
using UnityEngine;
using UnityEngine.UIElements;
public class CharacterController : MonoBehaviour
{
[System.Serializable]
public class MoveSettings
{
public float rotateVel = 100;
public float forwardVel = 12;
public float jumpVel = 25;
public float distToGrounded = 0.1f;
public LayerMask ground;
}
[System.Serializable]
public class PhysSettings
{
public float downAccel = -0.75f;
}
[System.Serializable]
public class InputSettings
{
public float inputDelay = 0.1f;
public int jumpDelay = 1000;
public float tapSpeed = 0.5f;
public float slopeAngle;
}
public MoveSettings moveSetting = new MoveSettings();
public PhysSettings physSetting = new PhysSettings();
public InputSettings inputSetting = new InputSettings();
Vector3 velocity = Vector3.zero;
Quaternion targetRotation;
Rigidbody rBody;
Animator anim;
public float forwardInput, turnInput ,jumpInput, runInput;
public Quaternion TargetRotation
{
get { return targetRotation; }
}
/*bool Grounded()
{
return Physics.Raycast(transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
}*/
void Start()
{
//moveSetting.DisstanceToTheGround = GetComponent<Collider>().bounds.extents.y;
forwardInput = 0;
turnInput = 0;
jumpInput = 0;
runInput = 0;
targetRotation = transform.rotation;
if (GetComponent<Rigidbody>())
rBody = GetComponent<Rigidbody>();
else
{ Debug.LogError("the character needs rigidbody"); }
if(GetComponent<Animator>())
{
anim = GetComponent<Animator>();
}
else
{ Debug.LogError("the character needs animator"); }
}
void GetInput()
{
forwardInput = Input.GetAxis(/*inputSetting.FORWARD_AXIS*/"Vertical");
turnInput = Input.GetAxis(/*inputSetting.TURN_AXIS*/ "Horizontal");
jumpInput = Input.GetAxisRaw(/*inputSetting.JUMP_AXIS*/ "Jump");
runInput = Input.GetAxis("shiftrun");
}
void Update()
{
GetInput();
Turn();
}
void FixedUpdate()
{
Run();
Jump();
rBody.velocity = transform.TransformDirection(velocity);
attack();
Grounded();
}
void Run()
{
if (Mathf.Abs(forwardInput) > inputSetting.inputDelay)
{
//move rBody.velocity = transform.forward * forwardInput * moveSetting.forwardVel;
velocity.z = moveSetting.forwardVel * forwardInput;
if (Mathf.Abs(runInput) > inputSetting.inputDelay)
{
moveSetting.forwardVel = 30;
anim.SetFloat("velocity", 2f);
}
else
{
moveSetting.forwardVel = 12;
anim.SetFloat("velocity", 0f);
anim.SetInteger("condition", 1);
}
anim.SetInteger("condition", 1);
//rBody.velocity = transform.forward * forwardInput * moveSetting.forwardVel;
}
else
{
// rBody.velocity = Vector3.zero;
velocity.z = 0;
anim.SetInteger("condition", 0);
//rBody.velocity = Vector3.zero;
}
}
void Turn()
{
if (Mathf.Abs(turnInput) > inputSetting.inputDelay)
{
targetRotation *= Quaternion.AngleAxis(moveSetting.rotateVel * turnInput * Time.deltaTime, Vector3.up);
}
transform.rotation = targetRotation;
}
void Jump()
{
if (jumpInput>0 && isGrounded)
{
anim.SetBool("jumpon", true);
velocity.y = moveSetting.jumpVel;
//moveSetting.stopJump = true;
}
else if (jumpInput==0 && isGrounded)
{
velocity.y = 0;
anim.SetBool("jumpon", false);
}
else
{
velocity.y -= physSetting.downAccel;
}
}
void attack()
{
if (Input.GetKeyDown("f"))
anim.SetBool("attackon", true);
else
anim.SetBool("attackon", false);
}
public bool isGrounded = false;
public void Grounded()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, moveSetting.distToGrounded, moveSetting.ground))
{
inputSetting.slopeAngle = (Vector3.Angle(hit.normal, transform.forward) - 90);
isGrounded = true;
}
else
{
isGrounded = false;
}
}
}