• 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
Question by kevinspawner · Dec 02, 2014 at 08:51 AM · 2dphysicsvector3velocityslingshot

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

People who like this

0 Show 1
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 · Dec 02, 2014 at 10:17 AM 0
Share

Any help please.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.)

Comment
kevinspawner
SandipVala

People who like this

2 Show 10 · 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 SaraCecilia · Dec 02, 2014 at 04:25 PM 0
Share

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

avatar image kevinspawner · Dec 03, 2014 at 03:13 AM 0
Share

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

avatar image kevinspawner · Dec 03, 2014 at 03:16 AM 0
Share

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.

avatar image kevinspawner · Dec 03, 2014 at 07:59 AM 0
Share

@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.

avatar image kevinspawner · Dec 05, 2014 at 02:56 AM 0
Share

Any one can give a hand?

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

2D Geometry dash-like ship physics 0 Answers

Ignore collision at high velocity. 1 Answer

Imparting Physics in only certain planes of motion 1 Answer

Physics in 2d arkanoid. Tangential and normal velocity 1 Answer

How do I get velocity from a collision? 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