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 KaiserDE · Jun 19, 2012 at 05:12 PM · iphonetouchbounce

iPhone: Bounce on touch

I am currently developing a game for the iPhone and I have a question. First of all, the game is a soccer ball juggling game. The ball starts in the air And is pushed down by gravity and the game ends if it touches the ground. I want to make a script so every time someone touches the ball it bounces up but like a 30 degree range of direction. Any help would be greatly appreciated.

Comment
Add comment · 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 KaiserDE · Jun 19, 2012 at 08:58 PM 0
Share

Give me a few $$anonymous$$utes. Probably within 15 $$anonymous$$utes I will need help. Thank you so much though!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Jun 19, 2012 at 05:17 PM

  • From an Update() function use Physics.RayCast to see if the touch position intersects with the ball. Use Camera.main.ScreenPointToRay() passing the touch positions (if you are using more than one).

  • Decide on whether to do the ray case when the touch begins, or all the time and only do the ray cast if the touch phase fits your expected phase(s).

  • When you detect a hit you need to AddForce to the rigidbody attached to the ball. You can create a Vector3 of the lowest possible angle and the multiple a Quaternion that you make from a random rotate around whichever axis you need (maybe Z).

        var q = Quaternion.Euler(0,0,Random.Range(0,30));
          var forceDirection = q * startingVector;
    
Comment
Add comment · Show 1 · 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 KaiserDE · Jun 19, 2012 at 06:59 PM 0
Share

Im still a bit confused on how to go about doing this. I understand generally what you are saying but I just need help on the base. So I would add a script on the ball saying if it is touched it will add force to the rigid body.

avatar image
0

Answer by KaiserDE · Jun 19, 2012 at 10:12 PM

Okay, I have almost got this working. Raycast now works and it successfully recognizes when I touch the ball. Now all I need to know is how to input the addforce.

Here is the code I'm using for the ray cast.

 var particle : GameObject;
 function Update () {
     for (var i = 0; i < Input.touchCount; ++i) {
         if (Input.GetTouch(i).phase == TouchPhase.Began) {
             // Construct a ray from the current touch coordinates
             var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
             if (Physics.Raycast (ray)) {
                 // Create a particle if hit
                 Instantiate (particle, transform.position, transform.rotation);
             }
         }
     }
 }


How would I add the addforce to that code? Here is the code I have for the addforce.

 function FixedUpdate () {
     rigidbody.AddForce (Vector3.up * 10);
 }

How would I input that code into the previous one?

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity iPhone: AddForce to Object after Touch 0 Answers

Unity iPhone 2D Left Right movement 0 Answers

translating a touchDeltaPosition from the y axis to z axis 1 Answer

Holding a Touch down on the iphone 2 Answers

Game running on android but not in iphone 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