• 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 FurleyFries · Oct 04, 2017 at 12:58 AM · 2d2d gameobjectthrowthrowing

Can i have help writing a script to pick up and throw a ball (2D game)

there are 10 balls, and 3 players in play, and I am needing help fixing my script for one of the player objects to pick up and be able to throw a ball whenspace is pressed. I will put what I have so far, thank you for anyone who tries to help. Oh and t$$anonymous$$s is a 2D game by the way.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BallThrow : MonoBehaviour
 {
     private bool ballInHands = false;
     public float ballSpeed;
     private Rigidbody2D ballRB;
     public float offset;
 
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Ball")
         {
             if (ballInHands == false)
             {
                 ballInHands = true;
                 other.gameObject.transform.position = transform.position + new Vector3(transform.position.x, transform.position.y + offset, transform.position.z);
                 if (Input.GetKeyDown("space"))
                 {
                     ballRB = other.GetComponent<Rigidbody2D>();
                     ballRB.AddForce(transform.up);
                     ballInHands = false;
                 }
             }
         }
     }
 }//end of class
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 Cuttlas-U · Oct 04, 2017 at 03:58 AM

$$anonymous$$;

i t$$anonymous$$nk u did a good job and u have some mistakes;

first of all pay attention to t$$anonymous$$s code u wrote :

t$$anonymous$$s has an if statement that is waiting for the player to press a button ; but t$$anonymous$$s code is in trigger function and it will be checked just once , w$$anonymous$$le u need to check every fram for player input;

so u can make a bool and put t$$anonymous$$s in update function ; another change im doing is adding ,ForceMode2D.Impulse to your force code ; so check for what it does;

  private void OnTriggerEnter2D(Collider2D other)
      {
          if (other.gameObject.tag == "Ball")
          {
              if (ballInHands == false)
              {
                  ballInHands = true;
                  other.gameObject.transform.position = transform.position + new Vector3(transform.position.x, transform.position.y + offset, transform.position.z);
 
              }
          }
      }
 
 void Update()
 {
 if ( ballInHands  == true )
 {
                  if (Input.GetKeyDown("space"))
                  {
                      ballRB = other.GetComponent<Rigidbody2D>();
                      ballRB.AddForce(transform.up,ForceMode2D.Impulse);
                      ballInHands = false;
                  }
 }
 }


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 FurleyFries · Oct 06, 2017 at 02:33 PM 0
Share

The script you posted just does what mine did, send the ball left and up about 1 meter. Then nothing else, the player can still move but the ball just stays there, But I did fix an error in the code you posted when you try to reference other.gameObject I update() its out of reference since the variable is a constructor for OnTriggerEnter, the way I fixed this is by assigning ballRB to other.gameObject in the OnTriggerEnter function. But I still cannot solve my other error. Thank you for helping me though!

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

138 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

how to throwing/flick gameObject in 2D 1 Answer

Unity 2D - Use Digital Painting as Terrain 1 Answer

Unity 5 2d Thorw Object 1 Answer

Pixel Art with blurry outline in Game Window 1 Answer

OnTriggerEnter2D not working 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