Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Hello Guys, I need help making a code for a button.

Discussion in 'Getting Started' started by andresescobart06, Apr 26, 2024.

  1. andresescobart06

    andresescobart06

    Joined:
    Apr 12, 2024
    Posts:
    2
    I was making a code for a button when he get pressed he turn off the "IsTrigger" from a "boxCollider2D" from the player, I have this code from the ball, is a object who get stuck to player.
    the Player have already the "IsTrigger" on.

    public class ballscript : MonoBehaviour
    {
    private bool stickToPlayer;
    [SerializeField] private Transform transformPlayer;
    [SerializeField] private Transform playerBallPosition;
    float speed;
    Vector2 previouslocation;
    public Collider2D ballcol;

    void Start()
    {


    }

    public void SetStickToPlayer(bool value)
    {
    stickToPlayer = value;
    }

    // Update is called once per frame
    void Update()
    {
    if (!stickToPlayer)
    {
    float distanceToPlayer = Vector3.Distance(transformPlayer.position, transform.position);
    if (distanceToPlayer < 0.5)
    {
    stickToPlayer = true;
    }
    }
    else
    {
    Vector2 currentlocation = new Vector2(transform.position.z, transform.position.x);
    speed = Vector2.Distance(currentlocation, previouslocation) / Time.deltaTime;
    transform.position = playerBallPosition.position;
    transform.Rotate(new Vector3(transformPlayer.right.z, 0, transformPlayer.right.x), speed, Space.World);
    previouslocation = currentlocation;
    }
    }

    }

    If u can made a force when player click on button to make shoot thats good.
    thanks and good day for u.
     
  2. andresescobart06

    andresescobart06

    Joined:
    Apr 12, 2024
    Posts:
    2
    i forgot to say, i wanna do this in touchscreen button