- Home /
slingshot script - velocity help needed
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;
}
}
Answer by taxvi · Dec 01, 2014 at 08:55 AM
how about changing line 59 like this:
Vector3 velocity = FruitPrefab.transform.position - slinngshotMiddleVector;
Thanks. It is moving at least. But it is going in a wrong direction. If I pull the fruit back it shoot throw it in a forward direction. But as per your suggestion it is moving in an opposite direction.
Also. I need to know how to change the selection angle. In my start code it is slinngshot$$anonymous$$iddleVector = new Vector3((leftSlingshotOrigin.position.x+ RightSlingshotOrigin.position.x)/2,(leftSlingshotOrigin.position.y+RightSlingshotOrigin.position.y)/2,0);
and I would like to make the drag selection only on the half side. Currently it is in a circle. How to make it only half Circle? Thanks in advance.
sorry I guess I understood your question wrong, where is the
circular area which is specified for touch and drag
Hope I have explained it clearly. Sorry, I should have attached the image earlier.
I have created the $$anonymous$$ and max points as left slingshot origin and right slingshot origin in my code. But it is moving in a circle. I need only the half circle Drag area [as per image].
Also the fruit prefab should shoot in the forward direction as marked in the image. Currently after changing the code as per your suggestion it is shooting in the wrong direction.
Your answer
Follow this Question
Related Questions
2d shooting. 0 Answers
2d shooting. 1 Answer
Shooting 2D - how to fix 1 Answer
prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers