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
Your answer
Follow this Question
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