2
\$\begingroup\$

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))
            rb.AddForce (transform.forward * thrust);
    }
}

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?

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Have you looked at the MoveTowards method? \$\endgroup\$
    – Matt
    Commented Aug 2, 2015 at 23:30
  • \$\begingroup\$ @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". \$\endgroup\$
    – Sie
    Commented Aug 3, 2015 at 1:13
  • \$\begingroup\$ Try using braces instead of indentation. C# is like that sometimes. \$\endgroup\$
    – Matt
    Commented Aug 3, 2015 at 1:25
  • \$\begingroup\$ It doesn't seem to care. Here is my code. \$\endgroup\$
    – Sie
    Commented Aug 3, 2015 at 2:32
  • \$\begingroup\$ is the player creating the ball or is it always there? \$\endgroup\$ Commented Aug 3, 2015 at 6:28

4 Answers 4

2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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")){
       ball.GetComponent<Rigidbody> ().AddForce(Vector3.forward * speed);
    }

    if(Input.GetButtonDown("Fire 2")){
        ball.GetComponent<Rigidbody> ().AddForce(-Vector3.forward * speed);
    }
}
\$\endgroup\$
0
\$\begingroup\$

Instead of adding force based on the ball's direction, add it based of the players forward-vector.

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))
    rb.AddForce (player.transform.forward * thrust);

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") {
                    hit.gameObject.GetComponent<Rigidbody>().AddForce(player.transform.forward * thrust);
                }
            }
        }
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .