Need help in the stone throwing mechanic with aiming for mobile in Unity3d

I really need to find a way to create a better throwing mechanic for mobile in unity. For aiming seems to be hard with the current code that I am using atm. I need to find a way to increase power by pulling back (which determines the distance of travel) while aiming left and right, then if I release my finger, then the ball will launch and land on the area that I aimed on.

How do I do this in C# in unity? (Note: I have tried 6 different codes as far as I found in the internet, and the code they wrote is of the same variant as the one I got)

The code that I have wrote so far does the following:

-I press the object, then I can apply power by moving only a little away
from the object to launch powerfully,
or moving my finger far to the edge
of the screen to have the lowest
launch power.

  • When I release it, the ball will launch accordingly. Though I must
    attest, the mechanic I wrote was only
    used for flicking objects, not
    necessarily aiming them.

Here’s the pic of the play area and the code that I’m using for the throwing mechanic:

Link to code

pic of play area "Hopscotch"

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ThrowSimulation2 : MonoBehaviour
{
Vector2 startPos, endPos, dir;
       float touchStartT, touchEndT, timeInt;

       [SerializeField]
       public float touchForceXY = 2f;
       [SerializeField]
       public float touchForceZ = 50f;

       Rigidbody rb;

       private void Start()
       {
           rb = GetComponent<Rigidbody>();
       }
       private void Update()
       {
           if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
           {
               touchStartT = Time.time;
               startPos = Input.GetTouch(0).position;
           }
           if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
           {
               touchEndT = Time.time;

               timeInt = touchEndT - touchStartT;
               endPos = Input.GetTouch(0).position;
               dir = startPos - endPos;
               rb.isKinematic = false;
               rb.AddForce(-dir.x * touchForceXY, -dir.y * touchForceXY, touchForceZ / timeInt);
           }
   }
}

Bump question…

So it seems like you have most of the bits in place, at the moment your forward force I’m guessing is in the Z axis.
Based on your description you want this based on the distance that you pull back, at the moment your code is using the inverse of the time that you pull back for. So if you fire quickly it will have more force than if you fire slowly.

then your y axis is upward force, it seems correct to leave this using the drag back distance but maybe make another scalar since it doesn’t seem like you’d want to scale it the same as direction.

I’d think it might be helpful to normalise the values of dir that you calculate by divinding things by Screen.width/height. This should mean on different devices you can reach the same maximum power despite how many pixels they might have, it also makes working out the scaling values easier as you are working with numbers closer to 1.

so I’d suggest your force is something like:

rb.AddForce(-dir.x * directionScale / Screen.width, -dir.y * upwardScale / Screen.height, -dir.y*forwardScale / Screen.height);

I might suggest starting with some scales of 1 for everything and increasing forwardScale first until you are able to hit as far back as you want.

EDIT, misunderstood the question

So there’s basically two ways to go about drawing a trajectory:

  1. Implement some physics equations yourself and calculate for a given force the arc of motion. This normally involves pretty simple equations of motion and is definitely something worth learning more generally in order to do game development.

some existing answers

or 2.

Make use of the maths already in unity by creating a secondary scene with the objects you want to simulate in, and then running Simulate() on the PhysicsScene object like in they have done here.