- Home /
Detecting Touch on moving Object - Help Needed!
Hi there,
Am creating a 2D-Game in which the player moves up and down the ship, when the user touch and drags in up and down direction. In the right side [30%] of the screen is used to drag the object up and down.
The Left Side [70%] is used to aim and shoot like slingshot. Problem is, I want the user to drag anywhere in the 70% left screen to pull the slingshot and shoot fruits.
Currently in my code, Am not sure how to append this idea, I want the user when he touch anywhere in the 70% screen and drag it should begin the dragging of slingshot right from the player point. Not from the finger touch point.
Also, in my code am not sure what am doing wrong, it is not projecting the fruits. Meaning fruits is not fired. Please help me to resolve the code and also explain it, so I can understand better. Thanks for your time. I have attached the image for better understanding`using UnityEngine; using System.Collections; using System;
public class slingShot_Mechanism : MonoBehaviour {
private Vector3 slinngshotMiddleVector;
public Transform leftSlingshotOrigin;
public Transform RightSlingshotOrigin;
public GameObject FruitPrefab;
public float throwSpeed;
public float timeSinceThrown;
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()
{
initializeFruit();
if(Input.GetMouseButtonDown(0))
{
Vector3 location = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(FruitPrefab.GetComponent<CircleCollider2D>()== Physics2D.OverlapPoint(location))
{
Debug.Log("User is Pulling the slinghsot");
}
}
if(Input.GetMouseButton(0))
{
Vector3 location = Camera.main.ScreenToWorldPoint(Input.mousePosition);
location.z = 0;
if (Vector3.Distance(location,slinngshotMiddleVector)> 0.3f)
{
var maxPos = (location - slinngshotMiddleVector).normalized * 0.3f + slinngshotMiddleVector;
FruitPrefab.transform.position = maxPos;
}
else
{
FruitPrefab.transform.position = location;
}
float distance = Vector3.Distance(slinngshotMiddleVector,FruitPrefab.transform.position);
}
else // User Removed the Finger
{
timeSinceThrown = Time.time;
float distance = Vector3.Distance(slinngshotMiddleVector,FruitPrefab.transform.position);
if (distance > 0.2f)
{
ThrowFruit(distance);
}
else //not pulled long enough, so reinitiate it
{
}
}
}
private void ThrowFruit(float distance)
{
Vector3 velocity = slinngshotMiddleVector - FruitPrefab.transform.position;
FruitPrefab.GetComponent<Rigidbody2D>().velocity = new Vector2(velocity.x, velocity.y) * throwSpeed * distance;
}
private void initializeFruit()
{
FruitPrefab.transform.position = fruitWaitPosition.position;
}
} `
The slingshot mechanism same as ZomBirds Game style. Ref: http://www.youtube.com/watch?v=0CcqsOY0Ikk
You want to check if the mouseposition.x is over or under 70% (0.7) of your Screen.width.
Secondly you want to save the position on$$anonymous$$ouseDown and then compare current on$$anonymous$$ouseButton to the saved position
Thanks. So which is the best way to control the 70% screen and also in my existing code the touch drag area is in complete circle. I want it only half, like in the attached image. Can you provide a ref code or pseudo code? So I can understand better. Thanks
Answer by fafase · Nov 26, 2014 at 10:53 AM
public Action<Vector3> OnTap = (Vector3 v) = >{};
void Update(){
if(Input.touchCount > 0)
{
foreach(Touch t in Input.touches)
{
OnTap(t.position);
}
}
}
then somewhere else you have a class that listens to this:
float thirdScreen;
void Start(){
// This means your InputSystem has a singleton pattern
// Else just do a GetComponent or drag and drop
InputSystem.Instance.OnTap += this.CheckInput;
thirdScreen = Screen.width / 3f * 2f;
// or thirdScreen = Screen.width * 0.7f;
}
private void CheckInput(Vector3 position)
{
if(position.x > thirdScreen)
{
Move();
}
else
{
Shoot();
}
}
I would actually put move and shoot to their own classes but at least you get the idea.
Thanks fafase. It I will try out this as a separate class. Also, I have mentioned about the slingshot problem. am not sure what exactly I have done, it is not throwing the fruit prefab. I also added velocity. It would be great if you can find the mistake. Thanks for your input .
Are you moving the fruit in its FixedUpdate?
public Vector2 Velocity{
get; set;
}
void FixedUpdate()
{
rigidbody2D.velocity = Velocity;
}
You might as well set Velocity from your slingShot_$$anonymous$$echanism and leave the movement to a specific class on the fruit.
Nope, I don't have a separate class. Also am using Update not fixed update. You can check my code above, I don't have a separate class for fruit. I just applied the above code to a empty game object and added fruit as a prefab. Am not sure, why even when the force is applied the fruit is not moving as it suppose to move. Thanks
Your answer
Follow this Question
Related Questions
Player movement for iOS? 1 Answer
Quake like movement 1 Answer
Movement script breaks colliders 1 Answer
How to make a character move towards a side of the screen that's pressed at a constant rate? 2 Answers
Non slippery movement 1 Answer