Roll A Ball

GameObject的添加就不细说了,地面,小球和碰撞小物体。

刚体组件(Rigidbody):

使物体能够模拟物理效果,比如重力,碰撞,推力等;

控制小球移动的脚本(Script,Ball的脚本):

  Input.GetAxis("Horizontal"):控制横向运动键,有A,D键;

  Input.GetAxis("Vertical"):控制纵向运动键,有W,S;

  AddForce(Vector3):添加力,力是矢量,有方向;

控制相机跟随物体移动(第一视角):

  把相加直接拖到物体上,作为物体的子对象,但是这里行不通,因为小球是圆球,移动起来是旋转的,相机也会随着选装,导致物体在整个画面中一直在颠倒旋转。另一种方法是相机和物体的位移保持不变,也就是相机的中心和小球的中心始终保持着一定的距离,该距离用向量表示。

  通过Transform.position得到各个物体的位置,是个点坐标。定义一个公共变量,为主角Ball的Transform对象。通过小球的位置,和两者位置的偏移量获得相机的位置。

由于场地周围没有任何阻碍,小球会离开地面,然后受重力作用往下落。所以在场地周围增加墙,来限制小球的运动范围

创建小球要收集的食物(Foods):

  创建一个cube,调整角度,然后将其制成prefab,为了方便之后对所有食物的统一修改。多个食物的创建,目前是利用prefab重复拖动到Scene中,或者在在Scene中使用快捷键ctrl+D来创建(这里暂时不描述怎么动态随机创建物体,只是初级简单的创建)。prefab的好处是方便修改所有利用prefab创建的物体的属性的修改。控制食物动态旋转,利用transform.Rotate(new Vector3(0, 1, 0))。由于食物较多,所以创建一个空的GameObject,统一管理这些物体。食物的Box Collider组件中如果不勾选Is Trigger属性,小球,无法穿过食物,勾选后可以穿透食物。

检测是否发生碰撞:

  1.碰撞检测;2.触发检测;3.射线检测;

  碰撞首先物体得依赖于刚体

  1.OnCollisionEnter(Collision col);

  2.OnTriggerEnter(Collider col);

  顺便一提Resharper插件,以后介绍

分数显示和结束画面:

  这里UI使用的是UGUI,在Hierarchy面板中右键创建UI中的Text,然后设置Text位于UI的Canvas的位置:

  

  而使这些字在结束时显示,则使用SetActive(true)。

源代码:

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4 
 5 public class BallControl : MonoBehaviour {
 6     public int force = 5;
 7     private Rigidbody rd;
 8 
 9     private int score = 0;
10 
11     public Text text;
12     public GameObject winText;
13     // Use this for initialization
14     void Start () {
15         rd = gameObject.GetComponent<Rigidbody>();
16     }
17     
18     // Update is called once per frame
19     void Update () {
20         float h = Input.GetAxis("Horizontal");
21         float v = Input.GetAxis("Vertical");
22         print(h + "");
23         print(v + "");
24         rd.AddForce(new Vector3(h, 0, v) * force);
25     }
26 
27     void OnCollisionEnter(Collision col)
28     {
29         if (col.collider.tag == "Food")
30         {
31             Destroy(col.collider.gameObject);
32         }
33     }
34 
35     void OnTriggerEnter(Collider col)
36     {
37         if (col.tag == "Food")
38         {
39             score++;
40             text.text = score.ToString();
41             if (score == 12)
42             {
43                 winText.SetActive(true);
44             }
45             Destroy(col.gameObject);
46         }
47     }
48 }
using UnityEngine;
using System.Collections;

public class FoodsControl : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        transform.Rotate(new Vector3(0, 1, 0));
    }
}
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class FollowTarget : MonoBehaviour {
 5 
 6     public Transform playerTransform;//声明对象的位置
 7     private Vector3 offset;//位置偏移量
 8 
 9     // Use this for initialization
10     void Start () {
11         offset = this.transform.position - playerTransform.position;
12     }
13     
14     // Update is called once per frame
15     void Update () {
16         //根据小球的位置和偏移量得到相机的位置
17         transform.position = playerTransform.position + offset; 
18     }
19 }


 
原文地址:https://www.cnblogs.com/kesteler/p/5602178.html