Search Unity

Need help adding a jump and dash to my game

Discussion in 'Scripting' started by Mercury122, Aug 27, 2021.

  1. Mercury122

    Mercury122

    Joined:
    Aug 27, 2021
    Posts:
    3
    Hello! I am a very new programmer trying to learn C#. My friend and I had a concept for a game and we just decided to go for it. Probably my stupidest decision diving in head first like that but I might as well try now. I was wondering how I could add a jump and a dash to my game. Keep in mind this is a third-person open-world type game. Any help would be appreciated.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ThirdPersonMovement : MonoBehaviour
    7. {
    8.     public CharacterController controller;
    9.     public Transform cam;
    10.     public float speed = 4f;
    11.     public float turnSmoothTime = 0.3f;
    12.  
    13.    
    14.    
    15.    
    16.     float turnSmoothVelocity;
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         float horizontal = Input.GetAxisRaw("Horizontal");
    21.         float vertical = Input.GetAxisRaw("Vertical") ;
    22.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    23.  
    24.        
    25.  
    26.  
    27.         if(direction.magnitude >= 0.1f)
    28.         {
    29.        
    30.  
    31.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    32.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    33.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    34.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    35.        
    36.  
    37.             controller.Move(moveDir.normalized * speed * Time.deltaTime);
    38.          }
    39.      }  
    40. }  
    41.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    There are a LOT of tutorials for jump/dash controllers... you probably want to start by working through three or four of these tutorials, get the hang of what's involved.

    Screen Shot 2021-08-26 at 8.50.11 PM.png
     
  3. Mercury122

    Mercury122

    Joined:
    Aug 27, 2021
    Posts:
    3
    Thank you very much, I will look at some of these :)
     
  4. Ningkamess

    Ningkamess

    Joined:
    Oct 9, 2020
    Posts:
    20
    I'm very new to Unity and scripting. I followed one of the tutorials listed above but it was for a 2D game with a static camera. How would you apply this script to dash based on the direction of a 3d camera following the player:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class DashMove : MonoBehaviour
    6. {
    7.     public Vector3 cameraPos;
    8.     private Rigidbody rb;
    9.     public float dashSpeed;
    10.     private float dashTime;
    11.     public float startDashTime;
    12.     private int direction;
    13.     public float distance;
    14.     public GameObject dashEffect;
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.         dashTime = startDashTime;
    19.     }
    20.     void Update()
    21.     {
    22.         if(direction == 0)
    23.         {
    24.             if (Input.GetKeyDown(KeyCode.A))
    25.             {
    26.                 Instantiate(dashEffect, transform.position, Quaternion.identity);
    27.                 direction = 1;
    28.             }
    29.             else if (Input.GetKeyDown(KeyCode.D))
    30.             {
    31.                 Instantiate(dashEffect, transform.position, Quaternion.identity);
    32.                 direction = 2;
    33.             }
    34.             else if (Input.GetKeyDown(KeyCode.W))
    35.             {
    36.                 Instantiate(dashEffect, transform.position, Quaternion.identity);
    37.                 direction = 3;
    38.             }
    39.             else if (Input.GetKeyDown(KeyCode.S))
    40.             {
    41.                 Instantiate(dashEffect, transform.position, Quaternion.identity);
    42.                 direction = 4;
    43.             }
    44.         } else
    45.         {
    46.             if(dashTime <= 0)
    47.             {
    48.                 direction = 0;
    49.                 dashTime = startDashTime;
    50.                 rb.velocity = Vector3.zero;
    51.             } else
    52.             {
    53.                 dashTime -= Time.deltaTime;
    54.                 if(direction == 1)
    55.                 {
    56.                     //rb.velocity = Vector3.left * dashSpeed;
    57.                     rb.velocity = Vector3.left * dashSpeed;
    58.                 } else if(direction == 2)
    59.                 {
    60.                     rb.velocity = Vector3.right * dashSpeed;
    61.                 } else if(direction == 3)
    62.                 {
    63.                     rb.velocity = Vector3.forward * dashSpeed;
    64.                 } else if(direction == 4)
    65.                 {
    66.                     rb.velocity = -Vector3.forward * dashSpeed;
    67.                 }
    68.             }
    69.         }
    70.     }
    71. }
    72.  
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Please don't post to old irrelevant threads. Start your own thread, it's FREE!

    You can see an example of "turning" control inputs based on camera heading here:

    https://github.com/kurtdekker/proxi...edControls/RotatedControlsPlayerController.cs

    Lines 124 through 130 do the magic you need.
     
  6. Ningkamess

    Ningkamess

    Joined:
    Oct 9, 2020
    Posts:
    20
    Awesome, good to know, thanks!