Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI button being held

Discussion in 'Scripting' started by danielzivharel, Apr 29, 2020.

  1. danielzivharel

    danielzivharel

    Joined:
    Apr 17, 2020
    Posts:
    12
    Hello,
    i want to add force to player while UI button being held.
    the add force thing i got but i can do only on click wich mean i cant hold the button for it to work you need to release and click agin wich is not good for me.
    now my code is that:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class leftRight : MonoBehaviour
    6. {
    7.     public void left()
    8.     {
    9.         FindObjectOfType<playerMove>().moveLeft();
    10.     }
    11.     public void right()
    12.     {
    13.         FindObjectOfType<playerMove>().moveRight();
    14.     }
    15. }
    the moveLeft and moveRight are functions in other script that add force to player.
    inside uinty on the onclick i added the right() and left() for each button;
    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
  3. danielzivharel

    danielzivharel

    Joined:
    Apr 17, 2020
    Posts:
    12
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    No, there is one in the link above.
     
  5. danielzivharel

    danielzivharel

    Joined:
    Apr 17, 2020
    Posts:
    12
    is there a way to do it or none that you know
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    If you described your problem accurately, EventTrigger might help you.

    If you look in the link there is example code.

    Run the code. Does it exhibit the button behavior that you want?

    If so, integrate it into your game. If not, try the code in the Standard Assets package that I also suggested in my first reply.

    This forum is NOT about us writing your code. It's about us helping you understand how to write YOUR CODE, as long as you can coherently describe your engineering problem.
     
    russellnadin likes this.
  7. danielzivharel

    danielzivharel

    Joined:
    Apr 17, 2020
    Posts:
    12
    which forum can i use?
     
  8. Sophophilia_Studios

    Sophophilia_Studios

    Joined:
    Jul 28, 2015
    Posts:
    21
    Hahahaha, I know this is annoying, but you have to admit this comment chain is pretty hilarious. I am not sure if it was satire or not.
     
    Kurt-Dekker likes this.