自学Unity的第一个游戏Roll-a-Ball

该文章介绍了如何使用Unity创建Roll-a-Ball游戏。内容包括设置游戏场景、移动玩家对象、添加物理行为、实现相机跟随、检测与收集品的碰撞以及显示分数和结束消息。重点涉及Rigidbody组件、Input系统以及Collider的使用。
摘要由CSDN通过智能技术生成

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是不存在的)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值