Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kevinspawner · Nov 26, 2014 at 09:16 AM · movementphysicsplayertouchscreen

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;
 }

 
 

} alt text`

The slingshot mechanism same as ZomBirds Game style. Ref: http://www.youtube.com/watch?v=0CcqsOY0Ikk

logic_01.png (241.3 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wisearn · Nov 26, 2014 at 10:39 AM 0
Share

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

avatar image kevinspawner · Nov 26, 2014 at 10:51 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kevinspawner · Nov 27, 2014 at 06:27 AM 0
Share

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 .

avatar image fafase · Nov 27, 2014 at 09:10 AM 0
Share

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.

avatar image kevinspawner · Nov 28, 2014 at 02:51 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges