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
3
Question by SrBilyon · Sep 25, 2011 at 03:21 AM · physicsvectorreflect

Walljumping and Vector.Reflect

/Free Cake for anyone who can solve this/

I'm trying to write a Walljumping function using Vector.Reflect, and I managed to getting to work somewhat, but the reflection only works when the character is facing the forward direction (if the camera is facing behind him).

 //=====================================================================
 // ApplyWalljump
 // -Not Working all the way yet........................................
 //=====================================================================
 function ApplyWallJump ()
 {
     var jumpButtonPressed = Input.GetButtonDown("Jump"); 
 
     //Checks to see if the player is against a wall and trips a flag
     if ((controller.collisionFlags & CollisionFlags.CollidedSides) != 0 )
     {isAgainstWall = true;}
     else
     {isAgainstWall = false;}
     
     // We must actually jump against a wall for this to work
     if (!isJumping)
     return;
     
     if (isAgainstWall && jumpButtonPressed && !controller.isGrounded )
     {
         //isWalljumping = true;
         var reflectedMoveDirection : Vector3 = Vector3.Reflect (moveDirection, transform.forward);
         
         reflectedMoveDirection.y = y;
         reflectedMoveDirection.y -= gravity * Time.deltaTime;        
         
         transform.rotation = Quaternion.LookRotation(Vector3(reflectedMoveDirection.x, 0, reflectedMoveDirection.z)); 
         controller.Move(reflectedMoveDirection * Time.deltaTime);
     }    
 }





How can I adjust the Reflection Direction so that the character will go into the opposite direction when he jumps against the wall, no matter where he jumps from?

If you need to see how the movement is set up, check it below:


 //=====================================================================
 // CalculateMovement
 //=====================================================================
 function CalculateMovement()
 {
     //Get the Inputs
     x  = Input.GetAxis("Horizontal");
     z  = Input.GetAxis("Vertical"); 
     y  = moveDirection.y;
 
     var jumpButton = Input.GetButton("Jump");
     var jumpButtonPressed = Input.GetButtonDown("Jump"); //SMH
     controller = GetComponent(CharacterController);
 
     var cameraTransform : Transform  = Camera.main.transform;
 
     //Get the forward vector from the camera
     var forward : Vector3 = cameraTransform.TransformDirection(Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;
 
     // Right vector relative to the camera 
     var right = Vector3(forward.z, 0, -forward.x);
 
     //Make sure that the movement is relative to the camera
     moveDirection = x * right + z * forward;
 
     //Speed the Character up
     moveDirection *= speed;
     
     //Orient the Player Accordingly
     if (moveDirection != Vector3.zero  && !onLedge)
     {
        transform.rotation = Quaternion.LookRotation(Vector3(moveDirection.x, 0, moveDirection.z));   
     }
     //This is needed, otherwise the character can't move while jumping
     moveDirection.y = y;
     
     //MAKE SURE JUMP ALWAYS COME AFTER ROTATION!!!!
     if (jumpButtonPressed && controller.isGrounded && canJump) 
     {
          ApplyJump(jumpHeight);
          isJumping = true;
     }
     else if (jumpButtonPressed && !controller.isGrounded && canJump && canDoubleJump) 
     {
         ApplyJump(jumpBoost);
         canJump = false;
     }
     
     //This Area is For Gliding
     if (jumpButton && !controller.isGrounded && moveDirection.y <= 0.0 && canGlide)
     {isGliding = true;}
     else
     {isGliding = false;}
     
     //If not on the ground
     if (!controller.isGrounded)
     {
         isAirborne = true;
     }
     //This checks to see if the player is on the ground, or if the player is on the ground and decides to walk off the side of something
     else if (controller.isGrounded && moveDirection.y <= 0.0)
     {
         isAirborne = false;        
         isJumping = false;
         canJump = true;
         isWalljumping=false;
         moveDirection.y = 0;
     }
 }
 
 
 //=====================================================================
 // ApplyMovement
 //=====================================================================
 function ApplyMovement()
 {
     if (!isGliding && (!onLedge && !onWall))
     {
         // Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
     }
     else
     {
         // Apply gravity at a reduced rate
         moveDirection.y -= glideGravity * Time.deltaTime;
     }
          
     // Move the controller
     if (!onLedge && !onWall && !isWalljumping)
     {
         controller.Move(moveDirection * Time.deltaTime);
     }

 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SirGive · Sep 27, 2011 at 01:09 AM

Like Bunny said, you want to use hit.normal. However, you don't want to reflect it at all. What you would actually want to do is to apply the normal + Vector3.up when the character is using the wall jump. You also might want to apply a wall jump variable. Like so:

 if(isWalljumping)
     {
         controller.Move(hit.normal*Time.deltaTime+Vector3.up*wallJump);
     }

Also be aware that you might need to change your character's forward to match the new direction.

This is much different than a true reflection of the character's velocity. As you may have noticed, most games make the player jump between two walls to get to the top. They take the perpendicular direction (the normal) and send the character that way.

Now just make sure you trip your flag off once the total force is applied.

Comment
Add comment · Show 2 · 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 SrBilyon · Sep 27, 2011 at 01:17 AM 0
Share

Thanks, kinda got it working for the most part, the tripping flag tripping however needs to be tweaked, because the player will just stop and free fall...

avatar image Bunny83 · Sep 27, 2011 at 03:13 AM 0
Share

Of course that's another way but it's an unnatural movement. If you jump at the wall in an narrow angle you would suggest to jump from the wall in the same angle and not 90° off the wall. Adding additional up-speed is self-evident since you perform a jump.

avatar image
3

Answer by Bunny83 · Sep 25, 2011 at 11:52 AM

You have to reflect the movedirection at the surface normal and not at Vector3.forward. You have to save the normal-vector when you hit the wall so you can use it when you want to walljump.

You have to implement a OnControllerColliderHit function and there check for a side-collision and save the hit.normal in a variable.

Comment
Add comment · Show 3 · 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 shane.rachel · Nov 30, 2012 at 03:49 AM 0
Share

how do you do this? could you give an example?

avatar image Bunny83 · Nov 30, 2012 at 09:30 AM 1
Share

Just use Vector3.Reflect like in the question above but use the hit normal as surface normal ;)

avatar image SrBilyon · Nov 30, 2012 at 09:33 AM 0
Share

It's funny. A year later, and this is also seems elementary to me :D

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody physics - equalizing force 1 Answer

[Help] Hovering Ball Not Reflecting Off Walls 1 Answer

rotation, vector bounce problem 1 Answer

Making an object bounce off a wall the same way light bounces off of a mirror. 4 Answers

Trying to move an object towards another's axis. 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