- Home /
How to touch anywhere in the screen and the point touched should alwaysstart from a particular point
Am creating a slingshot game in which the object/player will be moving up and down. The user can click anywhere in this part of screen [if(Input.GetTouch(0).position.x< Screen.width/3)] and always the touch should start from the game object position. Since the game object will be moving up and down am not sure how to do this. I have pasted the code below so far what I have done.
{
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 && currentTouchPosition.x < slinngshotMiddleVector.x)
{
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 = FruitPrefab.transform.position - slinngshotMiddleVector;
FruitPrefab.GetComponent<Rigidbody2D>().velocity = new Vector2(velocity.x, velocity.y) *2.0f* distance;
Debug.Log("Thrown Bird");
}
}
}
private void initializeFruit()
{
FruitPrefab.transform.position = fruitWaitPosition.position;
}
}
Answer by yashpal · Dec 02, 2014 at 12:32 PM
@kevinspawner, I did't understand you question properly but if you want to make your touchable area move with your object than You can use collider in trigger mode and use raycast to detect touch.
Edited:
Here is a package I make for you. I never make single shot game. I just move platform up down and drag and hold gameObject. as per I understand. And Code is for single touch.
Steps to import unitypackage.
1) download package form Here.
2) create new project.
3) import package by double click OR from Assets -> import Package -> custom Package.
4) And open scene and build and test it.
Let me know it is what you want.(It is not throw gameObject like angry bird. you need to implement by yourself.)
Did you check out the learning material? Like, creating a sling shot style game: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/making-angry-birds-style-game
Thanks for your replies.
I currently want the user to touch anywhere in the left side 70% of the screen if(Input.GetTouch(0).position.x< Screen.width/3)
Once the user touch anywhere the touch should always begin from the public Transform fruitWaitPosition;
In normal angry birds style game, user need to click on the bird to drag and shoot. The bird will be stationed in one place and doesn't move. Whereas in my case the user will use the right finger to move the ship up and down [30% of the right screen] and 70% of the left screen will be used to touch and drag the slingshot to shoot. So anywhere they place the finger in 70% left screen it should always start from the public Transform fruitWaitPosition;
position. Game slingshot mechanism is same as this game link text
In the link provided, the user will click and drag in the pumpkin to shoot arrows, Actually you can click and drag anywhere in the screen and it always shoot arrows from the pumpkin. Hope this is quiet clear. Let me know if you need more details.
@saraCecillia, Thanks. I did checked the video, In my case it is quiet different and am bit struck with this. Am not quiet sure as how to proceed with this error.
Your answer
Follow this Question
Related Questions
How to set velocity to new direction after rotation? 0 Answers
AddForce vs Velocity issues with Rigidbody2D 2 Answers
Drag and throw Rigidbody2d (Angry Birds style) 0 Answers
Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers
(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer