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 01, 2014 at 08:28 AM · 2dprefabshootingslingshot

slingshot script - velocity help needed

Hi there,

I have a problem in slingshot script that I have created to shoot prefabs. Evryting is working fine except the prefab is not shot in the way I needed. Once I remove the finger it should shoot the fruit in the direction specified in the velocity, instead it shoot the prefab inside the circular area which is specified for touch and drag.

Please help me to figure out what am doing wrong here. Am i missing something? So far no clue yet. Thanks in advance.

 {
 
     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)
                 {
                         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 = slinngshotMiddleVector - FruitPrefab.transform.position;
                 FruitPrefab.GetComponent<Rigidbody2D>().velocity = new Vector2(velocity.x, velocity.y) *2.0f; 
                 Debug.Log("Thrown Bird");
             }
         }
             
     }
 
 
     private void initializeFruit()
     {
         FruitPrefab.transform.position = fruitWaitPosition.position;
     }
 
         
         
     
 }    

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by taxvi · Dec 01, 2014 at 08:55 AM

how about changing line 59 like this:

 Vector3 velocity = FruitPrefab.transform.position - slinngshotMiddleVector;
Comment
Add comment · 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 kevinspawner · Dec 01, 2014 at 09:31 AM 0
Share

Thanks. It is moving at least. But it is going in a wrong direction. If I pull the fruit back it shoot throw it in a forward direction. But as per your suggestion it is moving in an opposite direction.

avatar image kevinspawner · Dec 01, 2014 at 09:38 AM 0
Share

Also. I need to know how to change the selection angle. In my start code it is slinngshot$$anonymous$$iddleVector = new Vector3((leftSlingshotOrigin.position.x+ RightSlingshotOrigin.position.x)/2,(leftSlingshotOrigin.position.y+RightSlingshotOrigin.position.y)/2,0); and I would like to make the drag selection only on the half side. Currently it is in a circle. How to make it only half Circle? Thanks in advance.

avatar image taxvi · Dec 01, 2014 at 09:40 AM 0
Share

sorry I guess I understood your question wrong, where is the

circular area which is specified for touch and drag

avatar image kevinspawner · Dec 01, 2014 at 09:53 AM 0
Share

alt text

logic_01.png (241.3 kB)
avatar image kevinspawner · Dec 01, 2014 at 09:59 AM 1
Share

Hope I have explained it clearly. Sorry, I should have attached the image earlier.

  1. I have created the $$anonymous$$ and max points as left slingshot origin and right slingshot origin in my code. But it is moving in a circle. I need only the half circle Drag area [as per image].

  2. Also the fruit prefab should shoot in the forward direction as marked in the image. Currently after changing the code as per your suggestion it is shooting in the wrong direction.

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.

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

2d shooting. 0 Answers

2d shooting. 1 Answer

Shooting 2D - how to fix 1 Answer

prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers

Drag and throw Rigidbody2d (Angry Birds style) 0 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