Throwing the ball into the hoop with unity

First of all hello. my ball is constantly bouncing in my project(with physical material).I can do right left back and forth with joystick but the game will be android, I need to throw the ball into the hoop. How can I adjust to throw the ball into the basketball hoop according to the finger drag speed?how can I also ensure that the bouncing ball physical is disabled as soon as my finger touches the screen? I’m waiting yours answer. Thx.

8512778–1134452–kontroljoystick.cs (579 Bytes)

Welcome to the forum!
Please post code using code tags. People wont just download a couple douzen files a day :slight_smile:

Sounds like you should be looking into kinematic equations:

Merhaba, mouse kontrolü ile yaptım fakat dokunmatik ekranda çalıştırmak için ne yapmalıyım?

UnityEngine kullanarak;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Çarpıştırıcı))]
public class DragAndShoot : MonoBehaviour
{
    özel Vector3 farePressDownPos;
    özel Vector3 mouseReleasePos;

    özel Rigidbody rb;

    özel bool isShoot;

    geçersiz Başlat()
    {
        rb = GetComponent<Rigidbody>();
    }

    özel geçersiz OnMouseDown()
    {
        mousePressDownPos = Input.mousePosition;
    }

    özel geçersiz OnMouseUp()
    {
        mouseReleasePos = Input.mousePosition;
        Vur(mouseReleasePos - mousePressDownPos);
    }

    özel kayan nokta guc = 1f;
    özel kayan nokta gökyüzü = 1.2f;
    void Shoot(Vector3 Force)
    {
        eğer (isShoot)
            dönüş;

        rb.AddForce(new Vector3(Force.x* guc, Force.y*sky, Force.y*guc));
        isShoot = doğru;
    }

}[/kod]

Your browser might or might not have auto translated what i wrote, but i dont speak that language. You also appear to have somehow auto translated code sniplet, because how it looks on the forum, it wouldnt compile.

1 Like

Sorry…
Script:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Collider))]
public class DragAndShoot : MonoBehaviour
{
    private Vector3 mousePressDownPos;
    private Vector3 mouseReleasePos;

    private Rigidbody rb;

    private bool isShoot;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void OnMouseDown()
    {
        mousePressDownPos = Input.mousePosition;
    }

    private void OnMouseUp()
    {
        mouseReleasePos = Input.mousePosition;
        Shoot(mouseReleasePos - mousePressDownPos);
    }

    private float guc = 1f;
    private float sky = 1.2f;
    void Shoot(Vector3 Force)
    {
        if (isShoot)
            return;

        rb.AddForce(new Vector3(Force.x* guc, Force.y*sky, Force.y* guc));
        isShoot = true;
    }

}

Haha, no problem.

Touches can be handled similarly to mouse inputs. Just that there can be more than one.
You get a specific “touch” by providing an index to GetTouch:

If there is just one finger, index 0 will provide the corresponding touch.
But there might be 5 or more fingers touching the screen. This is how you differentiate between them.

Also relevant: Unity - Scripting API: Touch.fingerId

Touches do have phases. The equivalent to pressing a mouse down would be the touchphase Began, and the equivalent to a mouse up would be the touchphase Ended.

The main thing that’s more complicated here is that there are no “OnTouchDown” events, or things like that. Instead you have to check for touches manually in your Update function. You should find plenty of examples online for how to handle touches. Here are some decent examples to give an overview:

As for how to disable your bounce animation, it depends on how you do it. Effectively you want to detect the situation in which you want to cancel it. For example a finger touching the screen, or letting go of it, to shoot the ball. Then you either disable the animation, or script thats responsible, or you communicate to it, that it should now enter some “shooting mode” or whatever, in which it doesnt animate this bounce.

I couldn’t do it :frowning:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Collider))]
public class DragAndShoot : MonoBehaviour
{
    private Vector3 touch;
    private Vector3 dontTouch;

    private Rigidbody rb;

    private bool isShoot;
    private float power= 1f;
    private float fly = 1.2f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void touchh()
    {
        Touch touch= Input.GetTouch(1);
    }

    private void dontTouchh()
    {
        Touch dontTouch= Input.GetTouch(0);
        Shoot(dontTouch- touch);
    }
    void Shoot(Vector3 Force)
    {
        if (isShoot)
            return;

        rb.AddForce(new Vector3(Force.x* power, Force.y*fly, Force.y* power));
        isShoot = true;
    }
}