Stick and ball
Stick and ball is a simple 3D game, The player must place the balls in a box with a stick.
The stick is moved up and down with a mouse click, and the balls are created by pressing R, B and C on the keyboard.
This is a simple 3D game tutorial made with Unity that uses first person character and in it you will see how to use terrain, post-processing, physics, static and dynamic sun, moving stick and creating balls with C# codes.
Sample pictures
C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class SunScript : MonoBehaviour { public float speed = 1; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { gameObject.transform.Rotate(speed * Time.deltaTime, 0, 0); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using UnityEngine; using System.Collections; public class StickScript : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKeyDown (KeyCode.Mouse0)) { gameObject.transform.Rotate (40, 0, 0); } if (Input.GetKeyUp (KeyCode.Mouse0)) { gameObject.transform.Rotate (-40, 0, 0); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using UnityEngine; using System.Collections; public class RotateScript : MonoBehaviour { public float rotateSpeed = 10; // Use this for initialization void Start () { } // Update is called once per frame void Update () { gameObject.transform.Rotate (new Vector3 (0, rotateSpeed * Time.deltaTime, 0)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using UnityEngine; using System.Collections; public class BallScript : MonoBehaviour { public GameObject redBall , blueBall , colorBall; public Transform redBallPos, blueBallPos, colorBallPos; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.R)) { Instantiate (redBall, redBallPos.position, redBallPos.rotation); } if (Input.GetKeyDown(KeyCode.B)) { Instantiate(blueBall, blueBallPos.position, blueBallPos.rotation); } if (Input.GetKeyDown(KeyCode.C)) { Instantiate(colorBall, colorBallPos.position, colorBallPos.rotation); } } } |