- Home /
Adding force to the charactercontroller for walljump
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;
}
}
}
Answer by wyatts · Jul 26, 2017 at 02:39 AM
Have you tried using Rigidbody.AddForce? It sounds like it might be more suited for your task.
Are you getting the "prints" in your console? I might use GetKeyUp instead of GetKey. And you have to do all this while holding down the spacebar?
All in all it would help to add some Debug.Log("something happened") into your code to see where it's breaking down.
Do not use debug logs to debug your code as has been advised here.
Research "breakpoints" and use them to properly debug your code so you can actually step through it and work out what is going on. Debug.Logging out stuff is a very bad habit that newer unity users do ins$$anonymous$$d of proper debugging.
Hey @Daemonhahn, as a newer unity user -- do you $$anonymous$$d sharing why Debug.Logging is a bad habit? I've found it extremely useful in debugging my code.
Debug.Logging is NOT debugging ,it is simply logging. It gets you into the habbit of not knowing how to actually debug the execution of a program.
Generally, just outputting values is not a good way to tell what is going on and will miss most problems found in proper debugging.
Also, it is very slow and slows down your program, and this topic has been spoken about at length on here before.
Look up breakpoints so you can learn to properly debug your code as it is a very basic skill that even the most new of programmers should learn, without it your not really program$$anonymous$$g to be honest.
Your answer
Follow this Question
Related Questions
Jet-Pack Script 1 Answer
Jump and move (CharacterController.velocity) C# 0 Answers
Problem with Character Controller movement left/right dash 0 Answers
My Player can't jump - coding 1 Answer
[C#] Jump on slopes 1 Answer