#2D top down dash problem
1 messages · Page 1 of 1 (latest)
[]cb
Use codeblocks to send code in a message!
To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.
For example:
```cs
Console.WriteLine("Hello World");
```
Produces:
Console.WriteLine("Hello World");
To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
float speedX, speedY;
Rigidbody2D rb;
public Animator animator;
Vector2 movement;
[Header("Dash Settings")]
[SerializeField] float dashSpeed = 10f;
[SerializeField] float dashDuration = 1f;
[SerializeField] float dashCooldown = 1f;
bool isDashing;
bool canDash = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
canDash = true;
}
}
void Update()
{
if (isDashing)
{
return;
}
speedX = Input.GetAxisRaw("Horizontal") * moveSpeed;
speedY = Input.GetAxisRaw("Vertical") * moveSpeed;
rb.velocity = new Vector2(speedX, speedY);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
if(Input.GetKeyDown(KeyCode.Space) && canDash)
{
StartCorutine(Dash());
}
}
public IE Dash()
{
canDash = false;
isDashing = true;
rb.velocity = new Vector2(speedX * dashSpeed, speedY * dashSpeed);
yield return new WaitforSeconds (dashDuration);
isDashing = false;
yield return new WaitforSeconds(dashCooldown);
canDash = true;
}
}
@ebon zodiac
I see that you've added an extra "}" before void update method.
And not sure if IE would work, i dont know if it does work like that though. Try using "IEnumerator" instead of IE.
Let me know if it works.
@warped geode
Coroutine spelling error.
oh
thx
but the other errors are still there
I am sorry
I am very noob at programming
@sturdy blade
?
Add this in the beginning
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
float speedX, speedY;
Rigidbody2D rb;
public Animator animator;
Vector2 movement;
[Header("Dash Settings")]
[SerializeField] float dashSpeed = 10f;
[SerializeField] float dashDuration = 1f;
[SerializeField] float dashCooldown = 1f;
bool isDashing;
bool canDash = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
canDash = true;
}
void Update()
{
if (isDashing)
{
return;
}
speedX = Input.GetAxisRaw("Horizontal") * moveSpeed;
speedY = Input.GetAxisRaw("Vertical") * moveSpeed;
rb.velocity = new Vector2(speedX, speedY);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
if(Input.GetKeyDown(KeyCode.Space) && canDash)
{
StartCoroutine(Dash());
}
}
public IEnumerator Dash()
{
canDash = false;
isDashing = true;
rb.velocity = new Vector2(speedX * dashSpeed, speedY * dashSpeed);
yield return new WaitforSeconds (dashDuration);
isDashing = false;
yield return new WaitforSeconds(dashCooldown);
canDash = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
float speedX, speedY;
Rigidbody2D rb;
public Animator animator;
Vector2 movement;
[Header("Dash Settings")]
[SerializeField] float dashSpeed = 10f;
[SerializeField] float dashDuration = 1f;
[SerializeField] float dashCooldown = 1f;
bool isDashing;
bool canDash = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
canDash = true;
}
void Update()
{
if (isDashing)
{
return;
}
speedX = Input.GetAxisRaw("Horizontal") * moveSpeed;
speedY = Input.GetAxisRaw("Vertical") * moveSpeed;
rb.velocity = new Vector2(speedX, speedY);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
if(Input.GetKeyDown(KeyCode.Space) && canDash)
{
StartCoroutine(Dash());
}
}
public IEnumerator Dash()
{
canDash = false;
isDashing = true;
rb.velocity = new Vector2(speedX * dashSpeed, speedY * dashSpeed);
yield return new WaitforSeconds (dashDuration);
isDashing = false;
yield return new WaitforSeconds(dashCooldown);
canDash = true;
}
}
Remove "cs".
cs is to copy script for this discord server
Oh, my bad. Whats the final error?
It should solve the issues
Happens with best of us. Make sure to use intellisense..
Makes the job easier.
whats that?
It helps correct and complete the code. Google it. How to enable intellisense for unity in visual studio.
thx
Bruh! Still written wrong spelling. Copy and paste this WaitForSeconds()
Yes!
Glad i colud help, Now quit visual studio and enable intellisense and reload the script.
Most of the issues will be solved
Yes, thanks. Check out my game i made for Gamejam: https://incredibro.itch.io/10-seconds-to-win
You too..
I am sorry
but when i press play
the screen goes black
It should work.. try reloading. Works on most browsers.