Roll-a-Ball
2023-07-25,记录下主要内容,以便后续回顾
1.Setting up the Game
1.make some profile package to make your work more organization.
(ag. Scence, Template , Materials, Scripts…)
2.create a new thing and reset by the transform.
3.add with some beautiful materials.
(Warning:when you put some gameobject to a null parents, make sure it is reset)
2.Moving the Player
1.add some components to the player like Rigidbody and Player Input
(pls. the meaning of the Rigidbody is to make sure the script can find player and add some physical force to it.)
(pls. the Input system should be inputed by package manager)
(pls. when adding the player input, we need creating a file (Input) to link the player)
private Rigidbody rb; //the private is to make sure anyone can't find in UI
void Start()
{
rb = GetComponent<Rigidbody>();
}
2.add some script to write the OnMove function declaration.
(pls. make sure using the UnityEngine.InputSystem)
private float movementx;
private float movementy;
public float speed = 0;
void OnMove(InputValue movementValue)
{
//获取用户输入的x和y,保存为二维向量
Vector2 movementActor = movementValue.Get<Vector2>();
//赋值到全局变量x和y
movementx = movementActor.x;
movementy = movementActor.y;
}
void FixedUpdate()
{
//物体移动是三维的,因此需要指定Vector3
Vector3 movement = new Vector3(movementx, 0.0f, movementy);
//添加作用力
rb.AddForce(movement * speed);
}
3.Moving the Camera
In order to make sure the camera can move with the ball rolling, we need to cal the offset bewteen the ball and the camera and using lateupdate function to change the position of the Camera.
(pls. the scprit should be linked to Camera, so transform can be used.)
private Vector3 offset;
public GameObject player;
void Start()
{
//cal the distance between the player and the camera
offset = transform.position - player.transform.position;
}
// LateUpdate is after the update
void LateUpdate()
{
//keep the distance by adding the offset to player
transform.position = player.transform.position + offset;
}
4.Detecting Collisions with Collectibles
1.Rotate the PickUp
void Update()
{
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
2.make the PickUp a Prefab and Duplicating it
3.use C# to detect the collider
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
}
}
(Warning: 1.Add the tag to find the compareTag ;
2.In the prefab, turn on the trigger ;
3.Because the prefab is Rotating every frame , we can use RidgeBoyd and turn off the Gravity and turn on the kinematic, which will not react to physics’ forces)
5.Displaying Score and Text
1.Store the value of collected PickUps
1.定义private int count
2.Start()中初始化count = 0
3.OnTriggerEnter判断中 count += 1
2.Create a UI text and display the count value
(pls. 在Text TMP中,需要使用 TextMeshProUGUI去调用text)
1.using TMPro
2.定义 public TextMeshProUGUI counttext;
3.定义方法
void OnCountWin()
{
counttext.text = "Count:" + count.ToString();
}
4.调用函数在Start()和OnTriggerEnter()
3.Create game end message
1.定义变量 public GameObject winner;
2.在Start()中,winner.SetActive(false);
3.在OnTriggerEnter判断中
if (count >= 4)
{
winner.SetActive(true);
}
(pls. SetActive只有在GameObject中才有,TextMeshProUGUI是不存在的)