Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class BallScript : MonoBehaviour {
- private bool ballIsActive;
- private Vector3 ballPosition;
- private Vector2 ballInitialForce;
- // GameObject
- public GameObject playerObject;
- // Use this for initialization
- void Start () {
- // create the force
- ballInitialForce = new Vector2 (100.0f,300.0f);
- // set to inactive
- ballIsActive = false;
- // ball position
- ballPosition = transform.position;
- }
- // Update is called once per frame
- void Update () {
- // check for user input
- if (Input.GetButtonDown ("Jump") == true) {
- // check if is the first play
- if (!ballIsActive){
- // reset the force
- GetComponent<Rigidbody2D>().isKinematic = false;
- // add a force
- GetComponent<Rigidbody2D>().AddForce(ballInitialForce);
- // set ball active
- ballIsActive = !ballIsActive;
- }
- }
- if (!ballIsActive && playerObject != null){
- // get and use the player position
- ballPosition.x = playerObject.transform.position.x;
- // apply player X position to the ball
- transform.position = ballPosition;
- }
- // Check if ball falls
- if (ballIsActive && transform.position.y < -6) {
- ballIsActive = !ballIsActive;
- ballPosition.x = playerObject.transform.position.x;
- ballPosition.y = -4.2f;
- transform.position = ballPosition;
- GetComponent<Rigidbody2D>().isKinematic = true;
- }
- }
- }
- sing UnityEngine;
- using System.Collections;
- public class PlayerScript : MonoBehaviour {
- public float playerVelocity;
- private Vector3 playerPosition;
- public float boundary;
- // Use this for initialization
- void Start () {
- // get the initial position of the game object
- playerPosition = gameObject.transform.position;
- }
- // Update is called once per frame
- void Update () {
- // horizontal movement
- playerPosition.x += Input.GetAxis("Horizontal") * playerVelocity;
- // leave the game
- if (Input.GetKeyDown(KeyCode.Escape)){
- Application.Quit();
- }
- // update the game object transform
- transform.position = playerPosition;
- // boundaries
- if (playerPosition.x < -boundary) {
- transform.position = new Vector3 (-boundary, playerPosition.y, playerPosition.z);
- }
- if (playerPosition.x > boundary) {
- transform.position = new Vector3(boundary, playerPosition.y, playerPosition.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement