unity游戏设计与实现 --读书笔记(一)

1,  游戏入门的一些知识点,游戏对象GameObject(角色), 组件Compoent(角色的功能),资源Asset(美术素材呵呵音频等的数据),场景Scene(用以放置各个角色,负责展示画面),预制Prefab(事先做好的模型,游戏中代码生成)。

2,小demo。
脚本预览,Player.cs :用于控制小方块的运动。

Ball.cs:控制小球运动。

Launcher.cs:用于控制发射台和小球的发射。

注:观察unity标题栏,能发现在unity Personal(64bit)-Untitled ------这一文版右侧有“*”号,* 表示当前项目文件需要保存,保存之后该符号就会消失。之后做了操作之后需要重新保存的时候,符号会再次出现。

调整摄像机的角度,:按住Alt键的同时拖动鼠标左键,摄像机以地面为中心旋转。而若按住Alt键盘和Ctrl键(苹果是Command键)的同时拖动鼠标左键,摄像机平行移动。滚动鼠标滚轮,画面将向场景深处前后移动。

涉及的一个问题:unity在处理数字的时候,并未特别说明是按照米或者厘米为单位进行计算。不过在重力值设为9.8的时候,就意味着Rigibody组件是按照1.0=1米的尺寸模拟物理运动。

若游戏中玩家角色和小球的尺寸是1.0,那么就是直径为1米的庞然大物。而在现实世界中,人们会觉得大物体是缓慢落下的,也因此许多微缩电影摄影中,通过把高速摄影拍下的东西进行缓慢播放,就可以把某些小的细节放大。

此时,为了消除或者减缓这种慢悠悠落下的效果,可以采取的方法有两种,1,减小对象自身的尺寸,2增加重力值,增强重力后,物体对象下落的速度会变得很快。

Edit--> Project setting --> Physics ,在检视面板将Gravity项的Y 值首位提高一些,比如设置为-20。(注意:是负值)

栗子:

脚本:

public class Launcher : MonoBehaviour {
    public GameObject ballPrefab;
    void Start () {
        
    } 
    void Update () {
        if (Input.GetMouseButtonDown(1))
        {
            Instantiate(ballPrefab);//创建预制体ballPrefab
        }
    }
    void OnBecameInvisible()
    {
        Destroy(this.gameObject);//删除游戏对象
    }
}
public class Ball : MonoBehaviour {
     
    void Start () {
        this.GetComponent<Rigidbody>().velocity = new Vector3(-8.0f, 8.0f, 0.0f);
    }
     
    void Update () {
        
    }
}
public class Player : MonoBehaviour {

    protected float jump_speed = 5.0f;
    private bool is_inland = true;//着陆标记
    void Start () {
        
    }


    void Update()
    {
        if (this.is_inland)
        {
            if (Input.GetMouseButtonDown(0))
            {
                this.is_inland = false;
                this.GetComponent<Rigidbody>().velocity = Vector3.up * this.jump_speed;
                //若玩家第一次起跳未结束就进行了第二次的鼠标左键点击,此时是会继续向上运动的,此时需要加入开关/标记
               // Debug.Break();//暂停游戏运行,观看效果;此函数便于检测,游戏中速度等的很快的时候,不方便查看的时候
            }
        }

    }
    void OnCollisionEnter(Collision collision)
    {
        //用于区分不同对象
        if (collision.gameObject.tag == "Finish")
        {
            this.is_inland = true; 
        }
        
    }

注:弹性材质:

 注:可以利用最高点的高度计算得到起跳速度

public class Player : MonoBehaviour {

    protected float jump_height = 4.0f;
    private bool is_inland = true;//着陆标记
    void Start () {
        
    }


    void Update()
    {
        if (this.is_inland)
        {
            if (Input.GetMouseButtonDown(0))
            {
                this.is_inland = false;
              float y_speed=Mathf.Sqrt(2.0f*Mathf.Abs(Physics.gravity.y)*this.jump_height);
               this.GetComponent<Rigidbody>().velocity = Vector3.up *y_speed; } 
    }
}

利用的公式是v=根号下 2*g*h;

原文地址:https://www.cnblogs.com/allyh/p/9902671.html