Hi there,
I have a problem in slingshot script that I have created to shoot prefabs. Evryting is working fine except the prefab is not shot in the way I needed. Once I remove the finger it should shoot the fruit in the direction specified in the velocity, instead it shoot the prefab inside the circular area which is specified for touch and drag.
Please help me to figure out what am doing wrong here. Am i missing something? So far no clue yet. Thanks in advance.
{
private Vector3 slinngshotMiddleVector;
public Transform leftSlingshotOrigin;
public Transform RightSlingshotOrigin;
public GameObject FruitPrefab;
public Transform fruitWaitPosition;
void Start()
{
slinngshotMiddleVector = new Vector3((leftSlingshotOrigin.position.x+ RightSlingshotOrigin.position.x)/2,(leftSlingshotOrigin.position.y+RightSlingshotOrigin.position.y)/2,0);
initializeFruit();
}
void Update()
{
if(Input.touchCount>0)
{
if(Input.GetTouch(0).position.x< Screen.width/3)
{
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved)
{
Vector3 currentTouchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
currentTouchPosition.z=0;
if (Vector3.Distance(currentTouchPosition,slinngshotMiddleVector)> 0.3f)
{
var maxPos = (currentTouchPosition - slinngshotMiddleVector).normalized * 0.3f + slinngshotMiddleVector;
FruitPrefab.transform.position = maxPos;
}
else
{
FruitPrefab.transform.position = currentTouchPosition;
}
float distance = Vector3.Distance(slinngshotMiddleVector,FruitPrefab.transform.position);
}
}
}
else
{
float distance = Vector3.Distance(slinngshotMiddleVector, FruitPrefab.transform.position);
if(distance>0.2f)
{
Vector3 velocity = slinngshotMiddleVector - FruitPrefab.transform.position;
FruitPrefab.GetComponent<Rigidbody2D>().velocity = new Vector2(velocity.x, velocity.y) *2.0f;
Debug.Log("Thrown Bird");
}
}
}
private void initializeFruit()
{
FruitPrefab.transform.position = fruitWaitPosition.position;
}
}