hello, I managed to put this script on a button UI and I want when I press it to launch the script but in the Onclick function of the button I only have the Dash script which appears in the dropdown list and no function to be selected. Thank you in advance for your help !!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class Dash : MonoBehaviour
{
private Rigidbody2D rb;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void Update()
{
if (direction == 0)
{
if (Input.GetMouseButtonDown(1) )
{
GameObject.Find("turbo").GetComponent<AudioSource>().Play(0);
direction = 1;
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
}
}
Pass to a method and assign it editor or via code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class Dash : MonoBehaviour
{
private Rigidbody2D rb;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
public Button dashButton;
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
// Added by code
// dashButton.onClick.AddListener(Dash);
}
public void Dash()
{
if (direction == 0)
{
if (Input.GetMouseButtonDown(1) )
{
GameObject.Find("turbo").GetComponent<AudioSource>().Play(0);
direction = 1;
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
}
}
Edit: I saw only after post, you need to change how you’re doing some things because the conditional do right click will only work if press at the same time that you call the function
Thank you for your reply. What thing do I need to change? Put for example Spacebar in place of mousedown (1)?
Change part of my script?
ps: I tested the script you modified and I got an error with the line: public void Dash ().
Thank you again for your time.
My bad, I used the same that that you’re using in your class, only change the method name and everything will be fine
All your triggers and changes are happening in update, and if I understood right you want to dash when player press the UI button, so you need to change your trigger to the button too.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class Dash : MonoBehaviour
{
private Rigidbody2D rb;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
public Button dashButton;
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
// Added by code
// dashButton.onClick.AddListener(Dash);
}
public void DashMethod()
{
if (direction == 0)
{
GameObject.Find("turbo").GetComponent<AudioSource>().Play(0);
direction = 1;
}
}
private void Update()
{
if (direction == 0)
{
// Trigger part
if (Input.GetMouseButtonDown(1) )
{
GameObject.Find("turbo").GetComponent<AudioSource>().Play(0);
direction = 1;
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
}
}
This way you will trigger dash by UI button and mouse button
Thanks for your help this is exactly what I needed! Many thanks again. Ps: When I click on the button my player jumps once and only after that he dash.