# Move ball based on direction of force?

I'm trying to make a ball that will move in the direction I push it from, and later pull it to me based on where the player is standing.

using UnityEngine;
using System.Collections;

public class BallForce : MonoBehaviour {

public float thrust;
public Rigidbody rb;

// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {

if (Input.GetMouseButton (0))
}
}


This seems to work, but I'm not sure how I get it to move based on the position of the player. How would I do this?

• Have you looked at the MoveTowards method?
– Matt
Commented Aug 2, 2015 at 23:30
• @N1ghtshade3 I tried to do that, but I'm not sure how to get it t work. I tried doing "if (Input.GetMouseButton (0))", but the compiler keeps saying "An embedded statement may not be a declaration or labeled statement".
– Sie
Commented Aug 3, 2015 at 1:13
• Try using braces instead of indentation. C# is like that sometimes.
– Matt
Commented Aug 3, 2015 at 1:25
• It doesn't seem to care. Here is my code.
– Sie
Commented Aug 3, 2015 at 2:32
• is the player creating the ball or is it always there? Commented Aug 3, 2015 at 6:28

To find a vector from point A to point B, you do this: B - A = C. The C vector is the vector that starts from A and ends at B.

In this case your vector A is the ball's position...
...and the vector B is player position...
...so the resulting vector C will give you a vector from ball to player.

Then you can use C.normalized * thrust for your force.

You could use Quaternion.LookRotation( transform.position - PLAYER POSITION ) which can be used to make the ball turn in the direction of the player and then you could add forward force to the ball for it to be pulled.

You could create a variable to reference the ball in the player script and just push forward or pull backwards in this script as the direction will be depending on the player, something like this:

public GameObject ball;
void Update(){
if(Input.GetButtonDown("Fire 1")){
}

if(Input.GetButtonDown("Fire 2")){
}
}


You will have to tag the playerobject in the Inspector as "Player" (without quotations)

Try this:

private GameObject player;
void Start() {
player = GameObject.FindGameObjectWithTag("Player");
}
…
if (Input.GetMouseButton (0))


Since you probably will have more or even many balls in the scene, a script for every ball would be an overkill, and would decrease the performance of your game. (except it's a game similar to football)

The better solution would be a BallManager. Tag every ball with the tag "ball" and check when the mouseButton is down, if the clicked object is a "Ball".

public class BallManager : MonoBehaviour {
private GameObject player;

void Start() {
player = GameObject.FindGameObjectWithTag("Player");
}

void Update() {
if (Input.GetMouseButton (0)) {
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Raycast hit;
if (Physics.Raycast(ray, out hit, 100.0f)) {
if (hit.gameObject.tag == "Ball") {