Search Unity

Walk on Walls (check) Jump off wall... (complete and utter uncheck)

Discussion in 'Scripting' started by MuffinMyst, Nov 28, 2014.

  1. MuffinMyst

    MuffinMyst

    Joined:
    Nov 17, 2014
    Posts:
    13
    I'm working with "Basic Movement Walk On Walls" script (added below) that i found on unity answers with a few tweaks of my own. The script currently sets it up so when you jump, you land back on the surface you jumped off of... my goal however is to have the character jump off and then re-orient to the ground and fall straight down (eventually i would like him to glide... he is a flying squirrel)

    But... something isn't quite working. I think the update is over riding my turning, though I can't figure out why... I have a bool that goes true while he is turning (or at least it is suppose to) and then when he is done turning, he will fall in the negative direction of his "new Normal" (thats all great for when he's walking on the wall)
    But... the only thing I can think of that is causing him to fall back to the tree/wall is that the script isn't waiting... so... i hit "jump" jumping = true... start turning... the next frame (so player has only turned a smidge... and basically down is still the surface he jumped off of) so gravity starts pulling him toward the surface of the wall... and the update aborts the turning and re-orients player to the tree....

    I've tweaked it in everyway my little brain can imagine to make it not do that... to wait until he is finished turning to continue with the update... now... he doesn't jump upwards at all... just... does a little bob (which I think is the player beginning to turn towards the ground... but then he falls and turns back to the wall)

    and even though i have it set up to only be able to hit the jump button once at a time ( if hit the jump button and jumping is = false.... do this... and the first step of "this" is jumping = true... i can still hit the jump button once more but... then everything goes wonky.. he doesn't turn to land on the ground feet first... but he does fall to the ground... and he stays in the state of not Grounded.... (a few tweaks later... he now can jump indefinitely...
    So... basically what i want to do... is use the climb button (and/or a collision) to "grab" onto a tree, orient on the faces (this part works beautifully) but... then when I jump... i want to jump off (up in the current "normal" direction, turn to orient so my normal is equal to world normal... and then gravity can take its course and he will fall down to the ground) eventually I will also have a glide button, that will be... if falling and hit glide, he will spread his arms and glide... (gravity will be reduced, and forward will have a constant force... the horizontal and vertical buttons will steer... that I will work on once he can jump off the tree...)
    Please please help me... I don't know how to make this work.... (I'm shareing the C# one.. if anyone can help and prefers Jscript I was also trying that one too to similar effect....

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. /// <summary>
    6.     /// C# translation from http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html
    7.     /// Author: UA @aldonaletto
    8.     /// </summary>
    9.    
    10. // Prequisites: create an empty GameObject, attach to it a Rigidbody w/ UseGravity unchecked
    11.         // To empty GO also add BoxCollider and this script. Makes this the parent of the Player
    12.         // Size BoxCollider to fit around Player model.
    13.        
    14. public class QM_CharController : MonoBehaviour {
    15.         private float moveSpeed = 6; // move speed
    16.     private float turnSpeed = 90; // turning speed (degrees/second)
    17.     private float lerpSpeed = 10; // smoothing speed
    18.     private float gravity = 10; // gravity acceleration
    19.     private bool isGrounded = true;
    20.     private float deltaGround = 1.2f; // character is grounded up to this distance
    21.     private float jumpSpeed = 10; // vertical jump initial speed
    22.     private float climbRange = 2; // range to detect target wall
    23.     private Vector3 surfaceNormal; // current surface normal
    24.     private Vector3 myNormal; // character normal
    25.     private float distGround; // distance from character position to ground
    26.     private bool jumping = false; // flag &quot;I'm jumping to wall&quot;
    27.     private float vertSpeed = 10; // vertical jump current speed
    28.     private bool climbing = false; //reorienting to new normal
    29.     private Vector3 worldNormal;
    30.     private bool falling;
    31.  
    32.  
    33.  
    34.     private Transform myTransform;
    35.     public BoxCollider boxCollider; // drag BoxCollider ref in editor
    36.    
    37.     private void Start(){
    38.         myNormal = transform.up; // normal starts as character up direction
    39.          myTransform = transform;
    40.         worldNormal = transform.up;
    41.         rigidbody.freezeRotation = true; // disable physics rotation
    42.         // distance from transform.position to ground
    43.         distGround = boxCollider.extents.y - boxCollider.center.y;
    44.        
    45.      }
    46.    
    47.     private void FixedUpdate(){
    48.         // apply constant weight force according to character normal:
    49.         rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
    50.         if (isGrounded) falling = false;
    51. }
    52.    
    53.     private void Update(){
    54.  
    55.         myTransform.Rotate(0, Input.GetAxis("Turn")*turnSpeed*Time.deltaTime, 0);
    56.         myTransform.Translate(0, 0, Input.GetAxis("Forward")*moveSpeed*Time.deltaTime);
    57.         Debug.Log (" grounded? " + isGrounded);
    58.         Debug.Log (" jumpping? " + jumping);
    59.         Debug.Log (" climbing? " + climbing);
    60.         // climb code - jump off wall or simple jump
    61.         if (climbing) return; // abort Update while orienting to a wall
    62.         if (jumping) return;
    63.  
    64.             Ray ray;
    65.         RaycastHit hit;
    66.        
    67.         if (Input.GetButtonDown("Climb")){ // climb pressed:
    68.          ray = new Ray(myTransform.position, myTransform.forward);
    69.             if (Physics.Raycast(ray, out hit, climbRange)){ // wall ahead?
    70.                 climbing = true;
    71.                 ClimbToWall(hit.point, hit.normal); // yes: climb on the wall
    72.  
    73.             }
    74.         }
    75.         if (Input.GetButtonDown ("Jump")&& !falling) { // jumping while on ground
    76.                     rigidbody.velocity += jumpSpeed * myNormal;
    77.                     falling = true;
    78.                     jumping = true;
    79.                     ClimbToWall (myTransform.position, Vector3.up);
    80.         }
    81.  
    82.  
    83.  
    84.                
    85.             Clinging ();
    86.  
    87.        
    88.    
    89.    
    90.     }
    91.  
    92.     private void Clinging(){
    93.        
    94.         Ray ray;
    95.         RaycastHit hit;
    96.         // movement code - turn left/right with Horizontal axis:
    97.         // update surface normal and isGrounded:
    98.         ray = new Ray(myTransform.position, -myNormal); // cast ray downwards
    99.         if (Physics.Raycast(ray, out hit)){ // use it to update myNormal and isGrounded
    100.             isGrounded = hit.distance <= distGround + deltaGround;
    101.             surfaceNormal = hit.normal;
    102.         }
    103.         else {
    104.             isGrounded = false;
    105.             // assume usual ground normal to avoid "falling forever"
    106.             surfaceNormal = Vector3.up;
    107.         }
    108.         myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
    109.         // find forward direction with new myNormal:
    110.         Vector3 myForward = Vector3.Cross(myTransform.right, myNormal);
    111.         // align character to the new myNormal while keeping the forward direction:
    112.         Quaternion targetRot = Quaternion.LookRotation(myForward, myNormal);
    113.         myTransform.rotation = Quaternion.Lerp(myTransform.rotation, targetRot, lerpSpeed*Time.deltaTime);
    114.         // move the character forth/back with Vertical axis:
    115.     }
    116.    
    117.     private void ClimbToWall(Vector3 point, Vector3 normal){
    118.         // climb to wall
    119.          // signal it's jumping to wall
    120.          rigidbody.isKinematic = true; // disable physics while climbing
    121.         Vector3 orgPos = myTransform.position;
    122.         Quaternion orgRot = myTransform.rotation;
    123.         Vector3 dstPos = point + normal * (distGround + 0.5f); // will jump to 0.5 above wall
    124.         Vector3 myForward = Vector3.Cross(myTransform.right, normal);
    125.           Quaternion dstRot = Quaternion.LookRotation(myForward, normal);
    126.        
    127.         StartCoroutine (jumpTime (orgPos, orgRot, dstPos, dstRot, normal, 0.4f));
    128.         //jumptime
    129.     }
    130.  
    131.  
    132.     private void JumpOffWall(){
    133.         // jump off wall
    134.  
    135.          Vector3 orgPos = myTransform.position;
    136.          Quaternion orgRot = myTransform.rotation;
    137.          Vector3 myForward = Vector3.Cross(myTransform.right, Vector3.up);
    138.          Quaternion dstRot = Quaternion.LookRotation(myForward, Vector3.up);
    139.          StartCoroutine (jumpTime (orgPos, orgRot, orgPos, dstRot, Vector3.up, 0.05f));
    140.         //jumptime
    141.     }
    142.    
    143.     private IEnumerator jumpTime(Vector3 orgPos, Quaternion orgRot, Vector3 dstPos, Quaternion dstRot, Vector3 normal, float time) {
    144.  
    145.         //rigidbody.velocity += jumpSpeed * myNormal;
    146.    
    147.         for (float t = 0.0f; t < time; ){
    148.             t += Time.deltaTime;
    149.             myTransform.position = Vector3.Lerp(orgPos, dstPos, t);
    150.             myTransform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
    151.             yield return null; // return here next frame
    152.  
    153.         }
    154.  
    155.         myNormal = normal; // update myNormal
    156.         rigidbody.isKinematic = false; // enable physics
    157.         climbing = false; // climbing is accomplished
    158.         jumping = false;
    159.  
    160.     }
    161.  
    162.     void OnTriggerEnter(Collider other){
    163.         Ray ray;
    164.         RaycastHit hit;
    165.                 if (other.gameObject.tag == "tree") {
    166.                         ray = new Ray (myTransform.position, myTransform.forward);
    167.                         if (Physics.Raycast (ray, out hit, climbRange)) { // wall ahead?
    168.                                 ClimbToWall (hit.point, hit.normal); // yes: climb on the wall
    169.                        
    170.                         }
    171.                 }
    172.         }
    173.    
    174. }
    175.  
    176.  
     
  2. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    I'm not familiar with C# so I had to look up a few terms, but from what I can roughly deduce by reading your script it seems the problem is here
    Code (CSharp):
    1.        if (Input.GetButtonDown ("Jump")&& !falling) { // jumping while on ground
    2.                     rigidbody.velocity += jumpSpeed * myNormal;
    3.                     falling = true;
    4.                     jumping = true;
    5.                     ClimbToWall (myTransform.position, Vector3.up);
    6.         }
    7.  
    8.  
    You can only jump when not falling,
    if you jump you are falling,
    hence you can only jump when you are not in (mid-air).

    correct me if I'm wrong but I did not see any deactivations/exceptions for this statement to allow additional jumping even if falling = true.

    The only off switch I saw for falling was here
    Code (CSharp):
    1.    private void FixedUpdate(){
    2.         // apply constant weight force according to character normal:
    3.         rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
    4.         if (isGrounded) falling = false;
    again correct me if I'm wrong as I'm not familiar with C# , but is falling ever deactivated in this script other than if grounded = true?

    if you can provide the javascript I should be able to help
     
    Last edited: Nov 28, 2014
  3. MuffinMyst

    MuffinMyst

    Joined:
    Nov 17, 2014
    Posts:
    13
    yea. I was trying to make it so when mid-air he couldn't jump again. (otherwise he could just keep jumping up and up and up) I eventually solved the problem I was having by using "Invoke" (C# is differnt from Java by not being able to us delay/waitforsec within a regular function, i needs an IEnumerator function... and even then I have issues with it... but the Invoke (in C#) allows you to select a function you want to call, and specify a float "time" for how long before it moves on in the code after "invokeing" the named function. The only issue I have with Invoke though... is you can't use it to invoke a function that requires input fields. I was able to make a function that didn't require and inputs, so this time it worked...) this gave the script time to do it's thing (rotating player)before moving on to applying gravity. It still has a few bugs, but... he can at least jump off the tree and land or fly... that's still a bit weird... sometime he'll go straight into flying (as my current code says) other times he sits in idle pose until I hit jump again... which... again.. is weird because mid-air he shouldn't be able to use the jump button again. I'm going to start a different thread though, as that's a different question entirely. Thank you for looking at it

    *Edit: If you still want to see the Java code I can post it later. Currently at work, so don't have access to my code
     
  4. Raf95

    Raf95

    Joined:
    Nov 8, 2017
    Posts:
    23
    Hey man could you share your code if you fix it thanks!