Hi, i’m trying to create a walljump, and i’m just at the point where I need to add force to the character for the actual jump, but when I walljump nothing happens, even though all the conditions are met. How do I add velocity to the character? I tried to add velocity to the charactercontroller component earlier, but it said the velocity was read only.
public class wallriding : MonoBehaviour {
private Rigidbody rbody;
void Start () {
rbody = GetComponent<Rigidbody>();
}
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if (Input.GetKeyDown(KeyCode.Space))
{
var inAir = CheckIfMidAir();
if (inAir) {
if (Input.GetKey(KeyCode.A))
{
if (CheckWall(1f))
{
print("wall on left");
rbody.velocity = new Vector3(10f, 10f, 0);
}
}
if (Input.GetKey(KeyCode.D))
{
if (CheckWall(2f))
{
print("wall on right");
rbody.velocity = new Vector3(0, 10f, 10f);
}
}
}
}
}
private bool CheckIfMidAir()
{
Vector3 downward = transform.TransformDirection(Vector3.down) * 10;
///Debug.DrawRay(transform.position, downward, Color.green, 500f);
Ray ray = new Ray(transform.position, transform.up * -1);
RaycastHit hit;
float distance = 18f;
Physics.Raycast(ray, out hit, distance);
return hit.distance > 1f;
}
public bool CheckWall(float direction)
{
///Debug.DrawRay(transform.position, transform.right * -1, Color.blue, 500f);
RaycastHit hit;
float howfar = 5f;
float aord = direction;
if (aord == 1f)
{
Ray ray = new Ray(transform.position, transform.right * -1);
Physics.Raycast(ray, out hit, howfar);
///Debug.DrawRay(transform.position, transform.right * -1, Color.blue, 500f);
if (hit.distance > 0.2f)
{
return true;
}
else
{
return false;
}
}
if (aord == 2f)
{
Ray ray = new Ray(transform.position, transform.right);
Physics.Raycast(ray, out hit, howfar);
Debug.DrawRay(transform.position, transform.right, Color.red, 0.01f);
if (hit.distance > 0.2f) {
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}