Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Taureus2 · Jul 26, 2017 at 12:59 AM · movementcharactercontrollervelocityjumpwalljump

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;
     }

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daemonhahn · Jul 26, 2017 at 08:52 AM 0
Share

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.

avatar image wyatts Daemonhahn · Jul 26, 2017 at 10:19 PM 0
Share

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.

avatar image Daemonhahn wyatts · Jul 27, 2017 at 07:57 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges