Skip to main content
edited body
Source Link
plainerman
  • 331
  • 2
  • 10

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

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);
                }
            }
        }
    }
}

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);
                }
            }
        }
    }
}
added 17 characters in body
Source Link
plainerman
  • 331
  • 2
  • 10

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);
                }
            }
        }
    }
}

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);
                }
            }
        }
    }
}

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);
                }
            }
        }
    }
}
Improved Answer; Improved readability; spelling
Source Link
plainerman
  • 331
  • 2
  • 10

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);
                }
            }
        }
    }
}

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);

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);
                }
            }
        }
    }
}
Source Link
plainerman
  • 331
  • 2
  • 10
Loading