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

Strafing with Swipe Funtionality to a limited distance

Discussion in 'Scripting' started by Reshad, Mar 23, 2013.

  1. Reshad

    Reshad

    Joined:
    Mar 23, 2013
    Posts:
    6
    Hi,
    I am working on a game development project where the character movement is like the Subway Surfer. But I can't understand how can I strafe my character for a limited distance with character controller in both ground and in air. Can anyone help me about this because I am not good in scripting. I give you my swipe script for understand. Thanks.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SwipeFinal : MonoBehaviour {
    6.  
    7. /*Swipe Variables*/
    8. public float comfortZone = 70.0f;
    9. public float minSwipeDistance = 14.0f;
    10. public float maxSwipeTime = 0.5f;
    11. private float startTime;
    12. private Vector2 startPosition; 
    13. private bool couldBeSwipe;
    14.  
    15. /*Character Variables*/
    16. public float jumpSpeed = 8.0f;
    17. public float gravity = 20.0f;  
    18. public float speed = 6.0f;
    19. public float strafeSpeed = 8.0f;   
    20. private Vector3 moveDirection;
    21. public float dist = 20.0f;
    22. private RaycastHit hit;
    23. private float rayDistance; 
    24. private CharacterController controller;
    25. private int jumpTimer;
    26. private bool grounded = false;
    27.    
    28. // Player must be grounded for at least this many physics frames before being able to jump again; set to 0 to allow bunny hopping
    29.   public int antiBunnyHopFactor = 1;   
    30.    
    31. // If true, diagonal speed (when strafing + moving forward or back) can't exceed normal move speed; otherwise it's about 1.4 times faster
    32.     public bool limitDiagonalSpeed = true;
    33.    
    34. // If checked, then the player can change direction while in the air
    35.     public bool airControl = false;
    36.     private bool playerControl = false;
    37.     private Vector3 contactPoint;
    38.  
    39. // Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast
    40.     public float antiBumpFactor = .75f;
    41.    
    42.        
    43. public float strafeTime = 1.5f;
    44.    
    45.    
    46. public enum SwipeDirection {
    47.         Left,
    48.         Right,
    49.         Up,
    50.         Down,
    51.         None
    52.     }
    53.    
    54. public SwipeDirection lastSwipe;
    55.    
    56. public float lastSwipeTime;
    57.    
    58.  
    59.     void Start() {
    60.        
    61.         controller = GetComponent<CharacterController>();
    62.         rayDistance = controller.height * .5f + controller.radius;
    63.                 jumpTimer = antiBunnyHopFactor;
    64.     }
    65.    
    66.    
    67.    
    68.     // Update is called once per frame
    69.     void Update () {
    70.        
    71.     float inputX = Input.GetAxis("Horizontal");
    72.         float inputY = Input.GetAxis("Vertical");
    73.         // If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
    74.         float inputModifyFactor = (inputX != 0.0f  inputY != 0.0f  limitDiagonalSpeed)? .7071f : 1.0f;
    75.        
    76.        
    77.         if (grounded) {
    78.         if(Input.touchCount > 0) {
    79.            
    80.             Touch touch = Input.touches[0];
    81.                    
    82.             switch(touch.phase) {
    83.                
    84.             case TouchPhase.Began:
    85.                
    86.                 lastSwipe = SwipeFinal.SwipeDirection.None;
    87.                 lastSwipeTime = 0;
    88.                 couldBeSwipe = true;
    89.                 startPosition = touch.position;
    90.                 startTime = Time.time;
    91.  
    92.                 break;
    93.            
    94.             case TouchPhase.Ended:
    95.                
    96.                 if(couldBeSwipe) {
    97.                    
    98.                     float swipeTime = Time.time - startTime;
    99.                    
    100.                     float verticalDistance = (new Vector2(0, touch.position.y) - new Vector2(0, startPosition.y)).magnitude;
    101.                     float horizontalDistance = (new Vector2(touch.position.x, 0) - new Vector2(startPosition.x, 0)).magnitude;
    102.                    
    103.                     if(swipeTime < maxSwipeTime) {
    104.                        
    105.                         if(verticalDistance > minSwipeDistance) {
    106.                        
    107.                             float verticalSwipeValue = Mathf.Sign(touch.position.y - startPosition.y);  
    108.                        
    109.                         /* If the swipe direction is positive, it was an upward swipe */
    110.                             if(verticalSwipeValue > 0) {
    111.                            
    112.                                 lastSwipe = SwipeFinal.SwipeDirection.Up;
    113.  
    114.                         /* Upward Direction Action */          
    115.                                
    116.                             moveDirection = new Vector3(0, -antiBumpFactor, inputY * inputModifyFactor);
    117.                                     playerControl = true;
    118.                                     jumpTimer++;
    119.                                  if (jumpTimer >= antiBunnyHopFactor) {
    120.                                     moveDirection.y = jumpSpeed;
    121.                                     jumpTimer = 0;
    122.                                 }
    123.                                
    124.                         }
    125.                                
    126.                                
    127.                         /* If the swipe direction is negative, it was an downward swipe */ 
    128.                             else if(verticalSwipeValue < 0) {// !up  !right  !left) {
    129.  
    130.                             lastSwipe = SwipeFinal.SwipeDirection.Down;
    131.                         /* Downward Direction Action */    
    132.  
    133.                             }
    134.                         }
    135.                        
    136.                         else if(horizontalDistance > minSwipeDistance) {
    137.                                                        
    138.                         float horizontalSwipeValue = Mathf.Sign(touch.position.x - startPosition.x);
    139.                            
    140.                         /* If the swipe direction is positive, it was an right swipe */
    141.                             if(horizontalSwipeValue > 0) {
    142.                            
    143.                                 lastSwipe = SwipeFinal.SwipeDirection.Right;   
    144.                                     Vector3 distance = new Vector3(2, 0, 0);
    145.                                     Vector3 startpos = transform.position;
    146.                                     Vector3 endpos = startpos + distance;
    147.                                     StartCoroutine(Right(startpos, endpos, strafeTime));
    148.  
    149.                         /* Right Direction Action */
    150.  
    151.                             }
    152.                        
    153.                         /* If the swipe direction is negative, it was an left swipe */ 
    154.                             else if(horizontalSwipeValue < 0) {// !up  !right  !down) {
    155.                            
    156.                                 lastSwipe = SwipeFinal.SwipeDirection.Left;
    157.                                 Vector3 distance = new Vector3(-2, 0, 0);
    158.                                 Vector3 startpos = transform.position;
    159.                                 Vector3 endpos = startpos + distance;
    160.                                 StartCoroutine(Left(startpos, endpos, strafeTime));
    161.                        
    162.                         /* Left Direction Action */
    163.  
    164.                             }
    165.                         }                      
    166.                     }
    167.                 }
    168.             lastSwipeTime = Time.time; // Set the time the last swipe occured, useful for other scripts to check:
    169.             Debug.Log("Direction: " + lastSwipe);
    170.             break;
    171.         }
    172.     }      
    173.    
    174.         else if(Input.touchCount <= 0) {   
    175.             if(startPosition.x != -1  startPosition.y != -1) {
    176.                 startPosition.x = -1;
    177.                 startPosition.y = -1;
    178.             }
    179.         } }
    180.        
    181.     else {
    182.             if (airControl  playerControl) {
    183.                 moveDirection.x = inputX * speed * inputModifyFactor;
    184.                 moveDirection.z = inputY * speed * inputModifyFactor;
    185.                 moveDirection = myTransform.TransformDirection(moveDirection);
    186.             }
    187.         }
    188.            
    189. // Apply gravity
    190.         moveDirection.y -= gravity * Time.deltaTime;
    191.        
    192.         // Move the controller, and set grounded true or false depending on whether we're standing on something
    193.         grounded = (controller.Move(moveDirection * Time.deltaTime)  CollisionFlags.Below) != 0;
    194. }
    195.  
    196.    
    197. IEnumerator Right(Vector3 startpos, Vector3 endpos, float seconds) {
    198.     float t = 0.0f;
    199.     if(endpos.magnitude >= 22  endpos.magnitude < 23)
    200.         while (t <= 1.0) {
    201.             t += Time.deltaTime/seconds;
    202.             rightDirection.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0.0f, 1.0f, t));
    203.             controller.Move(rightDirection.position * Time.deltaTime);
    204.             yield return 0;
    205.         }  
    206.     }
    207.    
    208. IEnumerator Left(Vector3 startpos, Vector3 endpos, float seconds) {
    209.     float t = 0.0f;
    210.     if(endpos.magnitude >= 22  endpos.magnitude < 23)
    211.         while (t <= 1.0) {
    212.             t += Time.deltaTime/seconds;
    213.             transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0.0f, 1.0f, t));
    214.             yield return 0;
    215.         }
    216.     }  
    217.  
    218.  
    219. void OnControllerColliderHit (ControllerColliderHit hit) {
    220.         contactPoint = hit.point;
    221.     }
    222.  
    223. }
    224.