using UnityEngine;
using System.Collections;
public class slingShot_Mechanics : MonoBehaviour
{
public Transform fruitPos;
public Transform player;
public GameObject orangeFruitPrefab;
private GameObject orangeFruitClone;
private Vector3 throwStartPos;
private Vector3 throwMovPos;
void Update()
{
if(Input.touchCount==1)
{
Touch touch = Input.GetTouch(0);
if(touch.position.x< Screen.width/3)
{
if(touch.phase == TouchPhase.Began)
{
orangeFruitClone = Instantiate(orangeFruitPrefab,fruitPos.position,fruitPos.transform.rotation)as GameObject;
orangeFruitClone.transform.parent = player.transform;
throwStartPos = Camera.main.ScreenToWorldPoint(touch.position);
}
else if(touch.phase == TouchPhase.Moved)
{
throwMovPos = Camera.main.ScreenToWorldPoint(touch.position);
float temp = Mathf.Clamp( (Vector3.Distance(throwMovPos , throwStartPos )),-0.3f,0.3f );
Vector3 differenceVector = (Vector3.Normalize(throwStartPos - throwMovPos))*temp;
orangeFruitClone.transform.position = fruitPos.transform.position - differenceVector;
orangeFruitClone.GetComponent<Rigidbody2D>().isKinematic = true;
}
else if(touch.phase == TouchPhase.Ended ||touch.phase == TouchPhase.Canceled)
{
orangeFruitClone.GetComponent<Rigidbody2D>().isKinematic = false;
orangeFruitClone.GetComponent<Rigidbody2D>().AddForce(-throwMovPos*800.0f);
}
}
}
}
}
This is a slingshot script, It is a 2D Game in which the object will be moving up and down [Y Axis]. Currently am able to shoot the fruits prefab clone. However it is not working the way I want.
Am not sure how to update the position correctly, currently am able to pull back the slingshot and throw fruits. However, it is not correctly updating according to the moved position. I have another script which will make the object move up and down, So once the object moved in Y axis to a new touch position the fruit should be thrown from that position. Currently the velocity is not accurate it is not updating from the touch position correctly.
Any help would be appreciated. Thanks for your time. Let me know if you need more explanation.