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 · Dec 30, 2014 at 10:03 AM · cameramovementslingshot

Moving Slingshot touch Help Needed

 using UnityEngine;
 using System.Collections; 
 
 public class slingShot_Mechanics : MonoBehaviour 
 {
 
     public Transform fruitPos;
     public Transform player;
 
     public  GameObject orangeFruitPrefab;
     private GameObject orangeFruitClone;
 
     private Vector3 throwStartPos;
     private Vector3 throwMovPos;
 
 
 
 
     void Update()
     {
 
         if(Input.touchCount==1)
         {
             Touch touch = Input.GetTouch(0);
             
             if(touch.position.x< Screen.width/3)
             {
                 if(touch.phase == TouchPhase.Began)
                 {                    
                     orangeFruitClone = Instantiate(orangeFruitPrefab,fruitPos.position,fruitPos.transform.rotation)as GameObject;
                     orangeFruitClone.transform.parent = player.transform;
                     throwStartPos = Camera.main.ScreenToWorldPoint(touch.position);                    
                 }
 
                 else if(touch.phase == TouchPhase.Moved)
                 {                
                     throwMovPos =  Camera.main.ScreenToWorldPoint(touch.position);        
 
                     float temp = Mathf.Clamp( (Vector3.Distance(throwMovPos , throwStartPos )),-0.3f,0.3f );
                     Vector3 differenceVector = (Vector3.Normalize(throwStartPos - throwMovPos))*temp;
 
                     orangeFruitClone.transform.position = fruitPos.transform.position - differenceVector;
                     orangeFruitClone.GetComponent<Rigidbody2D>().isKinematic = true;
                 }
 
 
                 else if(touch.phase == TouchPhase.Ended ||touch.phase == TouchPhase.Canceled)
                 {
                     orangeFruitClone.GetComponent<Rigidbody2D>().isKinematic = false;
                     orangeFruitClone.GetComponent<Rigidbody2D>().AddForce(-throwMovPos*800.0f);
                 }
             }
         }   
 
 
     }  
 
 
 
 }
 


This is a slingshot script, It is a 2D Game in which the object will be moving up and down [Y Axis]. Currently am able to shoot the fruits prefab clone. However it is not working the way I want.

Am not sure how to update the position correctly, currently am able to pull back the slingshot and throw fruits. However, it is not correctly updating according to the moved position. I have another script which will make the object move up and down, So once the object moved in Y axis to a new touch position the fruit should be thrown from that position. Currently the velocity is not accurate it is not updating from the touch position correctly.

Any help would be appreciated. Thanks for your time. Let me know if you need more explanation.

Comment
Add comment · Show 4
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 jakovd · Dec 30, 2014 at 10:46 AM 0
Share

Please explain your objects hierarchy. Is this fruit a child of the player? Your gameplay is not clear, too. Do you shoot your fruits from the players character? In your description you are using a term 'object' for something that is moving up or down. What is this object? Player? Fruit? A screenshot of the unwanted behavior would be nice too.

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

Thanks for your quick response.

1.fruit is the not the child of the Player. It is a seperate prefab. 2. I will shoot the fruits by touching and dragging from the public Transform fruitPos; It is nothing to do with player. 3. The object I meant is the player. [I have referenced its position as public Transform player;] 4. Fruits will be instantiated as public GameObject orangeFruitPrefab;

The player is moved by a simple script up and down. If that something that is related to this explanation.

alt text

screenshot.png (478.1 kB)
avatar image jakovd · Dec 30, 2014 at 02:33 PM 0
Share

Also, you might want to avoid setting the is$$anonymous$$inematic value every time your are in processing $$anonymous$$oved touch event. Set is$$anonymous$$inematic to true in prefab and remove that line 43. For performance reasons.

avatar image kevinspawner · Dec 31, 2014 at 02:14 AM 0
Share

Thanks. Still it is not accurate. Am not sure what am doing wrong.

         if(Input.touchCount==1)
         {
             Touch touch = Input.GetTouch(0);
             
             if(touch.position.x< Screen.width/3)
             {
                 if(touch.phase == TouchPhase.Began)
                 {                    
                     orangeFruitClone = Instantiate(orangeFruitPrefab,fruitPos.position,fruitPos.transform.rotation)as GameObject;
                     orangeFruitClone.transform.parent = player.transform;
                     throwStartPos = Camera.main.ScreenToWorldPoint(touch.position);                    
                 }
 
                 else if(touch.phase == TouchPhase.$$anonymous$$oved)
                 {                
                     throw$$anonymous$$ovPos =  Camera.main.ScreenToWorldPoint(touch.position);        
 
                     float temp = $$anonymous$$athf.Clamp( (Vector3.Distance(throw$$anonymous$$ovPos , throwStartPos )),-0.3f,0.3f );
                     Vector3 differenceVector = (Vector3.Normalize(throwStartPos - throw$$anonymous$$ovPos))*temp;
 
                     orangeFruitClone.transform.position = fruitPos.transform.position - differenceVector;
                     orangeFruitClone.GetComponent<Rigidbody2D>().is$$anonymous$$inematic = true;
                 }
 
 
                 else if(touch.phase == TouchPhase.Ended ||touch.phase == TouchPhase.Canceled)
                 {
 
                     touchReleasePos =  Camera.main.ScreenToWorldPoint(touch.position);
                     newReleasePos = fruitPos.transform.position - touchReleasePos;
 
                     orangeFruitClone.GetComponent<Rigidbody2D>().is$$anonymous$$inematic = false;
                     orangeFruitClone.GetComponent<Rigidbody2D>().AddForce(newReleasePos*1000.0f);
                 }
             }
         }
 

1 Reply

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

Answer by jakovd · Dec 30, 2014 at 02:29 PM

Okay, now I have a clearer picture of what is the problem. On touch release, you are launching the fruit in the direction opposite to throwMovPos. That value is not to be used. That is just a representation of your touch position in 3d world. That has nothing to do with the direction you would like to launch your projectile to. Your direction should be a vector from your touch release position (A) toward the position where you spawn your fruits, fruitPos (B). And you want to use the current position of (B), not the position calculated when the touch began. You get that vector by subtracting (B-A). Make sure you are using the values from the same coordinate system. So, before you calculate the vector, either project your fruitPos in Screen coordinates or project your touch.position into World. You have methods for that on your Camera.main object.

Comment
Add comment · Show 2 · 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 · Feb 04, 2015 at 10:32 AM 0
Share

Thanks. I figured out another way to do it. Anyway, thanks for your answer.

avatar image Geneve-Goh · Jul 23, 2016 at 11:52 AM 0
Share

hi, @jakovd may I ask you a question? I'm doing a game similar to angry birds. but, why isn't my catapult working in unity? It cannot be stretch or pulled. I've checked the code and it is correct. Before I proceed to designing of the game, everything seems okay. It works well but sometimes, it cannot be stretch or whatsoever too. But after I improved on the design wise, it cannot be stretch. Do you know why?

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 walks to the right? 1 Answer

How to add Pinch to Zoom? 0 Answers

Set Max Rotation On Weapon Sway 0 Answers

Camera Movement and angles 2 Answers

how to stop making script constantly want to look at the vector 2 Answers


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