Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Bocksdin · Feb 16, 2018 at 03:20 AM · object move

Player and AI throw ball - C#

I'm sure this is already answered correctly somewhere, but I can't seem to find it. I have tried a number of remedies already. I have a "Player" game object made from a primitive capsule, a few AI capsules moving randomly on two different teams (red and blue), and several primitive spheres for throwing at each other. When the player is holding a ball and clicks I need the ball to move to the click position over time. When the AI is holding a ball, it waits a random amount of time, chooses a random enemy target and throws the ball at the enemy target's position at the time the ball was thrown. However, when the player or AI try to throw the ball it doesn't go anywhere.

     private bool canHold = true;
     public GameObject ball;
     public Transform guide;
     private float speed = 10; //Speed multiplier for the ball throw
     private GameObject[] targetList;
     private Vector3 mouseClick;
 
     void Update()
     {
         AIThrow();
 
         if (Input.GetMouseButtonUp(0))
         {
             mouseClick = Input.mousePosition;
             PlayerThrow();
         }
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "OnGround")
         {
             if (!ball)
             {
                 ball = other.gameObject;
                 Pickup();
             }
         }
     }
 
     private void Pickup()
     {
         if (!ball)
         {
             return;
         }
 
         ball.transform.SetParent(guide);
 
         ball.transform.position = guide.position;
 
         canHold = false;
 
         ball.tag = this.tag + "Ball";
 
     }
 
     private void PlayerThrow()
     {
         //On click, throw the ball the "Player" object is holding...
         //...to the click position
         if (!ball)
         {
             return;
         }
 
         ball.transform.position = Vector3.MoveTowards(ball.transform.position, mouseClick, speed * Time.deltaTime);
         ball.tag = this.tag + "InAir";
         ball = null;
         
     }
 
     private void AIThrow()
     {
         float waitTime = Random.Range(1,5); //Wait for 1-4 seconds to throw a ball after picking one up
         float waitTimePassed = 0;
         int index;  
         GameObject randomTarget;  //Choose random enemy "Red" tag or "Blue" tag target for ball throw
 
         if (!ball)
         {
             return;
         }
 
         waitTimePassed += Time.deltaTime;
 
         if (this.name == "RedPlayer")
         {
             targetList = GameObject.FindGameObjectsWithTag("Blue");
         } else if (this.name == "BluePlayer")
         {
             targetList = GameObject.FindGameObjectsWithTag("Red");
         }
 
         index = Random.Range(0, targetList.Length);
         randomTarget = targetList[index];
 
         if (waitTimePassed >= waitTime)
         {
             ball.transform.position = Vector3.MoveTowards(ball.transform.position, randomTarget.transform.position, speed * Time.deltaTime);
             ball.tag = this.tag + "InAir";
             ball = null;
         }
     }
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

0 Replies

· Add your reply
  • Sort: 

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

125 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

Related Questions

moving train 1 Answer

How to move an object left and right by dragging mouse after clicking left mouse button. 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