- Home /
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:
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:
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...
Bump once more. It looks like scribe isn't co$$anonymous$$g back.
Answer by Scribe · Apr 12, 2020 at 11:38 PM
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:
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.
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.
But how am I going to add an ai$$anonymous$$g system, where I can see where the stone would land while I'm ai$$anonymous$$g.
Updated my answer a little, still not exactly sure if you want to draw out a trajectory or just calculate the landing position, make you could add a screenshot of how you want the game to look, editing it in paint or something.
I just needed to draw a trajectory as I am ai$$anonymous$$g, similar to this., except the path ends when it reaches to the ground, and it's for ai$$anonymous$$g in 3d. https://www.youtube.com/watch?v=txP2rC9wKyQ