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

Help with Dash Ability

Discussion in 'Scripting' started by inferno46n2, Sep 21, 2021.

  1. inferno46n2

    inferno46n2

    Joined:
    Oct 29, 2020
    Posts:
    11
    Hello everyone,

    I am in the very early stages of learning Unity and ultimately C# code. I have two quick questions I was hoping this community could answer.
    • How do I get my visual studio to display similar colors and suggestions as nearly all of the youtube tutorials I watch? I've installed the proper unity add ons through visual studio installer but mine still isn't coloring the same as all of the tutorials I watch (please see photo example of what my compiler looks like). For example, why is Vector3 not colored, or CharacterController in orange etc.. I also do not get the proper suggested auto commands and stuff.

    • I'm currently making a top down twin stick shooter. I've managed to setup my movement but I am trying to add a dash through a IEnumerator command. So far I have it working, but it dashes in the direction I am facing and I would like it to dash in the direction I am travelling (as the direction I am facing will be where my bullets are aiming at the enemies). The idea is to eventually have a twin stick setup where stick left is movement, stick right is direction of bullet fire.

    Please see below for my Character movement script so far. Thank you all in advance!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TopDownCharacterMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.  
    9.     public float speed = 6f;
    10.  
    11.     public float dashSpeed;
    12.     public float dashTime;
    13.     public float dashCD = 1;
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         float horizontal = Input.GetAxisRaw("Horizontal");
    19.         float vertical = Input.GetAxisRaw("Vertical");
    20.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    21.  
    22.         if (direction.magnitude >= 0.1f)
    23.         {
    24.             controller.Move(direction * speed * Time.deltaTime);
    25.  
    26.         }
    27.  
    28.         // Dash Stuff
    29.         dashCD -= Time.deltaTime;
    30.  
    31.         if (Input.GetMouseButtonDown(0))
    32.         {
    33.             if (dashCD <= 0)
    34.             {
    35.                 StartCoroutine(Dash());
    36.             }
    37.         }
    38.  
    39.         // Dash stuff
    40.         IEnumerator Dash()
    41.         {
    42.             float startTime = Time.time;
    43.  
    44.             while (Time.time < startTime + dashTime)
    45.             {
    46.                 transform.Translate(Vector3.forward * dashSpeed * Time.deltaTime);
    47.                 dashCD = 1;
    48.  
    49.                 yield return null;
    50.             }
    51.         }
    52.  
    53.     }
    54. }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Welcome!

    This can be infuriatingly flaky at times. Other times it just works and angels sing on high.

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874

    Line 46 above transforms the Vector.forward using the object this script is on (the shortcut
    transform
    ). You could probably achieve the dash you want by giving it instead the transform of whatever thing controlling your facing is.

    You might want to actually capture its rotation at the start of the dash though, otherwise perhaps course changes mid-dash would be reflected in dash direction, which may or may not be desired.
     
  3. inferno46n2

    inferno46n2

    Joined:
    Oct 29, 2020
    Posts:
    11
    Thank you for the welcome and the reply! I've actually since switched over to "Visual Studio Code" and it appears to be working much better.

    The thing that is controlling my facing is a second script titled, " PlayerDirection" and it is technically the direction my character is dashing with the script as is. You are correct in stating that I would like to snapshot the direction I am moving. I just honestly minimal idea how to achieve this.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerDirection : MonoBehaviour
    6. {
    7.  
    8.     void Update()
    9.     {
    10.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    11.         RaycastHit hit;
    12.  
    13.         Debug.DrawRay(ray.origin, ray.direction * 1000, Color.white);
    14.  
    15.         if (Physics.Raycast(ray, out hit))
    16.         {
    17.             Vector3 targetPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
    18.  
    19.             Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
    20.  
    21.             transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 10.0f);
    22.         }
    23.     }
    24. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    From your first script, line 24 should be setting the CharacterController to face the way you are moving.

    Is PlayerDirection perhaps overwriting that? That might not be reliable unless you ensure execution order.

    Traditionally one would control two separate transforms, one the lower body and one the upper body, and thus you could choose to use either one for the dash.

    By "snapshot" the rotation, I mean make a copy of the rotation from the transform in question (Whatever that ends up being), and use that rotation to rotate the Vector3.forward for the duration of the dash.

    Code (csharp):
    1. Quaternion savedRotation = movingTransform.rotation;  // whatever movingTransform turns out to be
    and then for the motion, you would move it by this quantity:

    Code (csharp):
    1. Vector3 desiredDashDirection = savedRotation * Vector3.forward * DashSpeed;
    NOTE:
    Translate()
    takes an optional second argument, which in this case you would want to set to
    Space.World
    .

    https://docs.unity3d.com/ScriptReference/Transform.Translate.html
     
  5. inferno46n2

    inferno46n2

    Joined:
    Oct 29, 2020
    Posts:
    11
    A reddit user that had posted a similar game type prototype helped me with the issue. I will post my solution here for anyone that may come across this thread. Essentially I had to first hold a public variable for my movement direction which I did not have. Secondly I changed the IEnumerate function to transform.position += rather than transform.Translate. The reddit user pointed out world space versus local space conversions. Here's the finalized code that is working now:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TopDownCharacterMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.    
    9.     public float speed = 6f;
    10.  
    11.     public float dashSpeed;
    12.     public float dashTime;
    13.     public float dashCD = 1;
    14.     Vector3 direction;
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         float horizontal = Input.GetAxisRaw("Horizontal");
    19.         float vertical = Input.GetAxisRaw("Vertical");
    20.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    21.  
    22.         if (direction.magnitude >= 0.1f)
    23.         {
    24.             controller.Move(direction * speed * Time.deltaTime);
    25.  
    26.         }
    27.  
    28.         // Dash Stuff
    29.         dashCD -= Time.deltaTime;
    30.  
    31.         if (Input.GetMouseButtonDown(0))
    32.         {
    33.             if (dashCD <= 0)
    34.             {
    35.                 StartCoroutine(Dash());
    36.             }
    37.         }
    38.  
    39.         // Dash stuff
    40.        
    41.         direction = new Vector3(horizontal, 0f, vertical).normalized;
    42.        
    43.         IEnumerator Dash()
    44.         {
    45.             float startTime = Time.time;
    46.  
    47.             while (Time.time < startTime + dashTime)
    48.             {
    49.                 transform.position += direction * dashSpeed *Time.deltaTime;
    50.                 dashCD = 1;
    51.  
    52.                 yield return null;
    53.             }
    54.         }
    55.  
    56.     }
    57. }
     
    Kurt-Dekker likes this.