Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question brain.exe stuck on calculating Vectors

Discussion in 'Scripting' started by unity_rLrstvXScQEvow, Sep 8, 2020.

  1. unity_rLrstvXScQEvow

    unity_rLrstvXScQEvow

    Joined:
    Mar 4, 2019
    Posts:
    28
    Hello there,
    after a little bit of experimenting i figured out how to create a wall jump in opposite direction of the camera is facing. Now I added two more walls in my scene, to test it inside a room and realized that the walljumps at the other two walls are not working. Now i dont know how to fix it. :confused:

    Code (CSharp):
    1.             if(Input.GetButtonDown("Jump"))
    2.             {
    3.                 Debug.DrawRay(hit.point, hit.normal, Color.red, 1.25f);
    4.                 velocity.y = jumpForce;
    5.                 //currMove = hit.normal * speed_whileWalljump;
    6.  
    7.                 Debug.Log("Hit.Point = " + hit.point);
    8.                 Debug.Log("Hit.Normal = " + hit.normal);
    9.  
    10.                 Vector3 wallJumpDir = hit.normal - CameraDirection();
    11.  
    12.  
    13.                 Vector3 directionWalljump = wallJumpDir;
    14.  
    15.                 if (hit.normal.x > 0)
    16.                     directionWalljump = new Vector3(wallJumpDir.x, wallJumpDir.y, -wallJumpDir.z); //Wand 1
    17.                 else if (hit.normal.z < 0)
    18.                     directionWalljump = new Vector3(-wallJumpDir.x, wallJumpDir.y, wallJumpDir.z); //Wand 2
    19.  
    20.  
    21.                 currMove = directionWalljump * speed_whileWalljump;
    22.  
    23.                 Debug.DrawRay(transform.position, wallJumpDir, Color.red, Mathf.Infinity);
    24.                 Debug.DrawRay(transform.position, directionWalljump, Color.green, Mathf.Infinity);
    25.                 Debug.Log(wallJumpDir);
    26.                 Debug.Log(directionWalljump);
    27.                 Debug.Log("<color=yellow>________________________</color>");
    28.             }
    I found the debugs pretty useful while creating it, thought it would help to understand the process a little bit better.
    Here is how I imagined it should work... but as I said it don't work on every wall just on my first two created. The green direction on the other walls are most likely like the red one.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I question line 10 above... I don't think you get the wall jump direction by subtracting the hit normal with the way the camera is facing. I'm not even sure what that would do. If it is working for you I suggest you print the values out because it might just be happy coincidence with the way that the camera is pointed versus the wall that does work.

    Looking at your images I think you might have uploaded two identical images. I am also not fully able to grasp what you're trying to do there, as 2D kinda cannot capture 3D. Also, most wallrunner walljumper games I have played (indeed most games) only use the camera direction to actually bend the users intent (ie. so that control input forward goes "camera forward" in the game), all the while leaving the underlying physics of the wallrunner / walljumper (indeed all other game physics) alone so the world is consistent regardless of a later change in your camera angle.
     
  3. unity_rLrstvXScQEvow

    unity_rLrstvXScQEvow

    Joined:
    Mar 4, 2019
    Posts:
    28
    So I found Vector3.Reflect and reworked my script a little bit, but same scenario. Two walls works as intended and the other two not.
    Btw... here is my CameraDirection() method I forgot to put it in the script at the top:

    Code (CSharp):
    1. private Vector3 CameraDirection()
    2. {
    3.     var cameraForewardDirection = Camera.main.transform.forward;
    4.     Debug.DrawRay(Camera.main.transform.position, cameraForewardDirection * 10, Color.red);
    5.  
    6.     var directionToMoveIn = Vector3.Scale(cameraForewardDirection, (Vector3.right + Vector3.forward));
    7.     Debug.DrawRay(Camera.main.transform.position, directionToMoveIn * 10, Color.green);
    8.  
    9.     return directionToMoveIn;
    10. }
    11.  
    12.  
    13. private void OnControllerColliderHit(ControllerColliderHit hit)
    14. {
    15.     if (!controller.isGrounded && hit.normal.y < 0.1f)
    16.     {
    17.         if (Input.GetButtonDown("Jump"))
    18.         {
    19.             Debug.DrawRay(hit.point, hit.normal, Color.red, 1.25f);
    20.             velocity.y = jumpForce;
    21.  
    22.             Vector3 wallJumpDir = hit.normal - CameraDirection();
    23.  
    24.             Vector3 directionWalljump = Vector3.Reflect(wallJumpDir, Vector3.up);
    25.  
    26.             if (hit.normal.x != 0.0f)
    27.             {
    28.                 directionWalljump.z = -directionWalljump.z;
    29.                 Debug.Log("X");
    30.             }
    31.  
    32.             currMove = directionWalljump * speed_whileWalljump;
    33.         }
    34.     }
     
  4. unity_rLrstvXScQEvow

    unity_rLrstvXScQEvow

    Joined:
    Mar 4, 2019
    Posts:
    28
    You was right with subtracting the hit.normal. I never played or saw something from those games you mentioned.
    So I changed the script again and the direction is correct now. The only thing that gives me headache is that the player don't get any up velocity. After "jumping" the player is falling straight down. I applied the direction * speed like in my script before and only changed the method to calculate the reflection.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEngine;
    5.  
    6. public class PlayerControllerWalljump : MonoBehaviour
    7. {
    8.     public float jumpForce = 15f;
    9.     public float speed = 8f;
    10.     public float speed_whileWalljump = 10f;
    11.     private Vector3 velocity;
    12.     private Vector3 lastMove;
    13.     private Vector3 currMove;
    14.  
    15.     public float gravity = 25f;
    16.  
    17.     public CharacterController controller;
    18.     public Camera cam;
    19.  
    20.     Vector3 lastVelocity;
    21.  
    22.     private void Update()
    23.     {
    24.         float x = Input.GetAxis("Horizontal");
    25.         float z = Input.GetAxis("Vertical");
    26.  
    27.         currMove = transform.right * x + transform.forward * z;
    28.  
    29.         if (controller.isGrounded)
    30.         {
    31.             velocity.y = -2f; // -2 ~ -1
    32.  
    33.             if (Input.GetButtonDown("Jump"))
    34.                 velocity.y = jumpForce;
    35.         }
    36.         else
    37.         {
    38.             velocity.y -= gravity * Time.deltaTime;
    39.             currMove = lastMove;
    40.         }
    41.  
    42.         currMove.y = 0;
    43.         currMove.Normalize();
    44.         currMove *= speed;
    45.         currMove.y = velocity.y;
    46.  
    47.         controller.Move(currMove * Time.deltaTime);
    48.  
    49.         lastVelocity = controller.velocity;
    50.  
    51.         lastMove = currMove;
    52.  
    53.         //Debug.DrawRay(transform.position, transform.forward, Color.red);
    54.     }
    55.  
    56.     private void OnControllerColliderHit(ControllerColliderHit hit)
    57.     {
    58.         if (!controller.isGrounded && hit.normal.y < 0.1f)
    59.         {
    60.             if (Input.GetButtonDown("Jump"))
    61.             {
    62.                 Vector3 direction = transform.forward;
    63.                 direction = Vector3.Reflect(direction, hit.normal);
    64.  
    65.                 Debug.DrawRay(transform.position, direction, Color.green, 10f);
    66.  
    67.                 direction.y = jumpForce;
    68.  
    69.                 Debug.Log("Direction: " + direction);
    70.  
    71.                 currMove = direction * speed_whileWalljump;
    72.                 Debug.Log("CurrMove (x): "+currMove);
    73.             }
    74.         }
    75.     }
    76. }
    77.  
     

    Attached Files:

  5. unity_rLrstvXScQEvow

    unity_rLrstvXScQEvow

    Joined:
    Mar 4, 2019
    Posts:
    28
    Well I figured it out...
    Code (CSharp):
    1. direction.y = jumpForce;
    should be
    Code (CSharp):
    1. velocity.y = jumpForce;
    Thank you anyway :D
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Sweet! Just to think about the problem, here is what I see:

    Whatever the lateral (left/right if you were looking directly into the wall) relative speed of the player is, make a vector that is tangent to the wall and exactly horizontal in that left/right direction, and that would be the desired run-along for the player, regardless of camera position.