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 heyylateef · Jun 27, 2018 at 03:21 AM · rigidbodyaddforceballrigidbody.addforce

Rigidbody addforce not working

Hi, I'm having an issue with the following code. I'm basically trying to make an AI, using 3 different actions chosen by random. The problem is that inside AIStriker(), within each case, only the animation will play, there isn't any force applied to the ball rigidbody.

Note: goalTech.turn is a reference to an if statement in another script, it is set to 1 for testing purposes.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class kickTheBall : MonoBehaviour {
 
     public Rigidbody ballRB;            //reference to rigidbody of soccer ball
     public Animator strikerAnim;        //reference to animator of striker (ybot)
     public goalLineTechnology goalTech; //reference to goallineTechnology script
 
     //public SwipeDetector swipe;       // reference to SwipeDector script (computes screen swipe and determines direction)
     
     public float shotPower;             // amount of force put on the soccer ball
     private Vector3 strikerPos;         // saves the position of the striker
 
 
 
     private void Awake()
     {
         SwipeDetector.OnSwipe += SwipeDetector_OnSwipe;         // call to the SwipeDetector script
     }
 
 
     // Use this for initialization
     void Start () {
         ballRB = GetComponent<Rigidbody>();
         strikerAnim = GetComponent<Animator>();
         strikerPos = transform.position;  //store the initial position of the striker
         if (goalTech.turn == 1)
             AIStriker();
 
 
     }
 
 
 
     private void SwipeDetector_OnSwipe(SwipeData data)         //reference the direction of the swipe determined by the SwipeDetector script
     {
         //x and y repesent force to be added in the x, y axes.
         float x = (data.EndPosition.x - data.StartPosition.x) /  34f;
         float y = (data.EndPosition.y - data.StartPosition.y) /  34f;
 
 
         {
 
             Debug.Log("Swipe in Direction: " + data.Direction);
             if (data.Direction == SwipeDirection.Up)
             {
                 strikerAnim.Play("kick soccerball");
                 ballRB.AddForce((new Vector3(x, y, 15)) * shotPower);
             }
             if (data.Direction == SwipeDirection.Right)
             {
                 strikerAnim.Play("soccer penalty kick");
                 ballRB.AddForce((new Vector3(x, 10, 15)) * shotPower);
             }
             if (data.Direction == SwipeDirection.Left)
             {
                 strikerAnim.Play("soccer penalty kick 0");
                 ballRB.AddForce((new Vector3(x, 10, 15)) * shotPower);
             }
             else
             {
                 //Down swipe    
             }
 
         }
     }
 
     
     
      void AIStriker()
     {
         //check if the ball is kicked so that we can play the animation
             
             int num = (int)Random.Range(1f, 3f); //generate a random number within 1 to 3
         switch (num)
         {
             case 1:
                 strikerAnim.Play("soccer penalty kick");    // AI controlled player takes a strike, aims right
                 ballRB.AddForce((new Vector3(15, 10, 15)) * shotPower);
                 break;
             case 2:
                 strikerAnim.Play("soccer penalty kick 0");  // AI controlled player takes a strike, aims left
                 ballRB.AddForce((new Vector3(-15, 10, 15)) * shotPower);
                 break;
             case 3:
                 strikerAnim.Play("kick soccerball");        // AI controlled player takes a strike, aims up
                 ballRB.AddForce((new Vector3(10, 12, 15)) * shotPower);
                 break;
         }
     }
 
     
 }
 
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

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

Bounce the 3d ball opposite to the touched point 0 Answers

How to prevent AddForce from stacking up ? 1 Answer

Problems with firing a bullet 0 Answers

Rigidbody doesn't seem to apply gravity 1 Answer

Bullet shooting not working 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