Unity Learning Week Two

Write the game Rall A Ball2.0

create model

1. Create the ball
inHierarchyCreate a small ball in (right-click3D ObjectSphere in ), inInspectorClick Add Component (Add Component) to add a small ball collider (Box Collider) and rigid body (Rigidbody).
2. Create the ground
Add the ground to the 3D Object (Plane) and add an upper collider to the ground, and
adjust the ground to a suitable size (the default is fine).
3. Create food
Add cubes to 3D Object (Cube)namedFoodAnd add the corresponding collision body for the cube. Create a cube prefab in Project (Profabs) and placed in a folder.
insert image description here

Copy multiple cubes in the Inspector (ctrl+D or select the target and right-click Paste) as the food of the ball to create a prefab.
insert image description here
Creating walls
The method of creating walls is the same as that of creating food, the difference is that you need to adjust the size of the walls
in each objectTransformAmong them, Scale can adjust the length, width and height (x, y, z) of the object.
insert image description here

Write code

ball code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class script : MonoBehaviour
{
    
    
    public int score = 0;
    public Text text;
    public GameObject winText;
    public GameObject lostText;
    public Wallscript wallscript;
    public Rigidbody body;/// <summary>
    /// 声明一个组件
    /// </summary>
    // Start is called before the first frame update
    void Start()
    {
    
    

        //Debug.Log("开始游戏了!");
        body = GetComponent<Rigidbody>();
        //得到一个组件,<>内为组件名
    }

    // Update is called once per frame
    void Update()
    {
    
    
        Debug.Log("游戏正在运行!");
       
        float i = Input.GetAxis("Horizontal");  //Horizontal左右键
        float j = Input.GetAxis("Vertical");//Vertical前后
        body.AddForce(new Vector3(i,0,j)*3);
     
    }
    private void OnCollisionEnter(Collision collision)  //判断是否发生碰撞OnCollisionEnter是碰撞的瞬间
    {
    
    
        if (collision.gameObject.tag == "Cube")   //collision.gameObject获得标签
        {
    
    
            //Destroy(collision.gameObject);  //Destroy毁坏物体
            lostText.SetActive(true);
        }

    }
    private void OnTriggerEnter(Collider other)//触发区域检测
    {
    
    
        if (other.tag == "Food")
        {
    
     
            Destroy(other.gameObject);
            score++;
            text.text = "分数:"+score; //显示分数
            if(score==7)
            {
    
    
                winText.SetActive(true);  //激活对象
                
            }
        }
        
    }
}

food code (cube)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class foodscript : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        transform.Rotate(Vector3.up);
       // transform.Rotate(Vector3.back);
        //将物体旋转Rotate是旋转()内是旋转方向
    }
}

The camera follows the ball

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class follwtarge : MonoBehaviour
{
    
    
    public Transform tf;   //transform位置
    private Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
    
    
        offset = transform.position - tf.position;
     //两物体的位置之差
    }

    // Update is called once per frame
    void Update()
    {
    
    
        transform.position = tf.position + offset;
    }
}

game packaging

Click on the upper leftFile,turn upBuilding Settings,Add Open Scene (Add Open Scene) can choose PC end, iOS end, Android end.
insert image description here
Finally click Build and select the packaging location.

Guess you like

Origin blog.csdn.net/AD_GOD/article/details/122726379