Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Joystick Movement Help

Discussion in 'Scripting' started by krillin1986, Jul 22, 2014.

  1. krillin1986

    krillin1986

    Joined:
    Oct 3, 2012
    Posts:
    36
    Ok, I have a spider game I am working on, I was originally messing with it with using the just Horizontal and Vertical to rotate the spider etc. Well, I decided to try to incorporate a joystick, that the spider will look and move in the direction the joystick is being held down (i probably should eventually change this relevant to the camera angle, but that's another day). Anyhow, I found a "Walk On Walls" script online, and modified it some so I can have my spider automatically start crawling on walls while I am going forward towards them. I won't post all the code, but, I will post a lot lol. I have NO problem when using Horizontal and Vertical keys, (simply rotate left and right, Vertical makes you move forward in the Z axis)My problem is, now that I have a joystick incorporated, when I try to crawl up walls, the spider keeps using my joystick direction it was using while on the ground. It doesn't "rotate" if you will, with the spider itself. Here is the code

    Code (JavaScript):
    1. ///// JOYSTICK KEYS!!! ////////
    2.          var h: float = Input.GetAxis ("Horizontal");
    3.         var v: float = Input.GetAxis ("Vertical");
    4.         var vk: float = Input.GetAxis ("VerticalKeyboard");
    5.         var hk: float = Input.GetAxis ("HorizontalKeyboard");
    6.        
    7.         //if (myNormal == uprightNormal)
    8.         //{
    9.         direction = new Vector3( h, 0, -v);
    10.         //}
    11.         //else if(myNormal == uprightNormalNegative)
    12.         //{
    13.         //direction = new Vector3( h, 0, v);
    14.         //}
    15.         //else
    16.         //{
    17.         //direction = new Vector3( h,-v,0);
    18.         //}      
    19.         var rotation = Quaternion.LookRotation(direction, myNormal);
    20.        
    21.         //Debug.Log(direction.magnitude);
    22.         if(direction.magnitude > 0.75f)
    23.         {
    24.         transform.rotation = rotation;
    25.         }
    26.        
    27. /////// KEYBOARD KEYS!!! //////////
    28.  
    29.     transform.Rotate(0, Input.GetAxis("HorizontalKeyboard")*turnSpeed*Time.deltaTime, 0);
    30.    
    31.    
    32.     // update surface normal and isGrounded:
    33.     ray = Ray(transform.position, -myNormal); // cast ray downwards
    34.     if (Physics.Raycast(ray, hit)){ // use it to update myNormal and isGrounded
    35.     isGrounded = hit.distance <= distGround + deltaGround;
    36.     surfaceNormal = hit.normal;
    37.     }
    38.     else {
    39.     isGrounded = false;
    40.     // assume usual ground normal to avoid "falling forever"
    41.     // Vector3.up
    42.     surfaceNormal = uprightNormal;
    43.     }
    44.     myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
    45.     // find forward direction with new myNormal:
    46.     var myForward = Vector3.Cross(transform.right, myNormal);
    47.     // align character to the new myNormal while keeping the forward direction:
    48.     var targetRot = Quaternion.LookRotation(myForward, myNormal);
    49.     transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed*Time.deltaTime);
    50.     // move the character forth/back with Vertical axis:
    51.     if (canMove)
    52.     {
    53.     /////// for KEYBOARD KEYS!!! /////////
    54.     if (vk || hk != 0)
    55.     {
    56.     transform.Translate(0, 0,vk*moveSpeed*Time.deltaTime);
    57.     }
    58.     if(direction.magnitude > 0.1f)
    59.     {
    60.     /////// FOR JOYSTICK!!! //////////
    61.     transform.Translate(0, 0,moveSpeed*Time.deltaTime*direction.magnitude);
    62.     }
    63.     }
    64.        }
    65.      
    66.        function HasJumpedFalse()
    67.        {
    68.        hasJumped = false;
    69.        }
    70.      
    71.      
    72.        function HasJumped()
    73.     {
    74.     canAttachToWall = false;
    75.     rigidbody.velocity += jumpSpeed * myNormal;
    76.     //my added code
    77.         if (myNormal != uprightNormal)
    78.         {
    79.                  if (myNormal == uprightNormalNegative)
    80.                   {
    81.                   myNormal = uprightNormal;
    82.                   transform.Rotate(Vector3(0,180,0));
    83.                     }
    84.                    else
    85.         {
    86.         myNormal = uprightNormal;
    87.         //transform.Rotate(Vector3(0,180,0));
    88.         }
    89.         }
    90.     }
    91.    
    92.     function CanMoveFunc()
    93.     {
    94.     canMove = true;
    95.     }
    96.     function CannotMoveFunc()
    97.     {
    98.     canMove = false;
    99.     }
    100.    
    101.     function JumpToWall(point: Vector3, normal: Vector3){
    102.     // jump to wall
    103.     jumping = true; // signal it's jumping to wall
    104.     rigidbody.isKinematic = true; // disable physics while jumping
    105.     var orgPos = transform.position;
    106.     var orgRot = transform.rotation;
    107.     var dstPos = point + normal * (distGround + 0.1); // will jump to 0.5 above wall
    108.     var myForward = Vector3.Cross(transform.right, normal);
    109.     var dstRot = Quaternion.LookRotation(myForward, normal);
    110.     for (var t: float = 0.0; t < 0.005f; ){
    111.     t += Time.deltaTime;
    112.     transform.position = Vector3.Lerp(orgPos, dstPos, t);
    113.     transform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
    114.     yield; // return here next frame
    115.     }
    116.     myNormal = normal; // update myNormal
    117.     rigidbody.isKinematic = false; // enable physics
    118.     jumping = false; // jumping to wall finished
    119.     }
    It is mostly only the beginning of this code that I need help with. Like I said, I found a javascript for a "walk on walls" script, modded it some and got what I got. As you can see, I am using a "direction" vector3 that takes into account the Joysticks Horizontal and Vertical positions. When I crawl on the wall, it is still trying to make LOOK FORWARD probably because of the "Quaternion.LookRotation" code. But, I am lost. Do you people get what I am trying to do? Lol, I am so dumbfounded right now that I probably am not making too much sense. If anyone has any ideas or suggestions, that'd be great! I'm thinking it's gotta be something to do with using the Spider's current transform rotation and somehow applying the controller direction vector3... i dunno -.- Thanks!
     
  2. krillin1986

    krillin1986

    Joined:
    Oct 3, 2012
    Posts:
    36