Unity游戏开发笔记(一)

Window->Rendering->Lighting Settings 下的Scene->Auto Generate 关闭(自动渲染)节约时间和资源

选中当前摄像机Main Camera,修改Clear Flags(背景)把Skybox(天空盒子)换成Solid Color(纯色)

MainCamera的Projection可选择Perspective【透视】,Orthographic【正交】,是指摄像机两种不同的视图

正交视图无法看到一个物体是远离自己还是正在我们面前,因为它不会根据距离收缩(不存在透视),调节size可以修改摄像机观察的范围

透视视图和我们从眼睛看到的视图是一样的(远小近大)

设置碰撞体:通过Add Component添加Box Collider,然后xyz轴调整到对应的垂直面,调整碰撞体大小

然后添加Rigidbody(刚体)

FixedUpdate () 和 Update ()    

同:当MonoBehaviour启用时,其在每一帧被调用。都是用来更新的

异:Update()每一帧的时间不固定,即第一帧与第二帧的时间t1和第三帧与第四帧的时间t2不一定相同。FixedUpdate()每帧与每帧之间相差的时间是固定的.

Update受当前渲染的物体影响,这与当前场景中正在被渲染的物体有关(比如人物的面数,个数等),有时快有时慢,帧率会变化,Update被调用的时间间隔就会发生变化。但是FixedUpdate则不受帧率的变化影响,它是以固定的时间间隔来被调用。

所以一些物理属性的更新操作应该放在FxiedUpdate中操作,比如Force,Collider,Rigidbody等。外设的操作也是,比如说键盘或者鼠标的输入输出Input,因为这样GameObject的物理表现的更平滑,更接近现实。

transform.forward 物体正方向

transform.up 围绕Y轴旋转

大小键盘同时控制2个不同的物体

Edit->Project Settings->Input,拷贝2份Horizontal,分别修改Name为HorizontalPlayer1、HorizontalPlayer2,代表2个玩家水平轴控制系统

把HorizontalPlayer1 的 left、right删除(小键盘控制键)

把HorizontalPlayer2 的 a、d删除(主键盘控制键)

拷贝2份Vertical,分别修改Name为VerticalPlayer1、VerticalPlayer2,代表2个玩家垂直轴控制系统

把VerticalPlayer1 的 up、down删除(小键盘控制键)

把VerticalPlayer2 的 w、s删除(主键盘控制键)

public class TankMovement : MonoBehaviour
{
    public float speed = 5; //移动速度
    public float angularSpeed = 10; //旋转速度(10度)
    public float number = 1; //编号

    private Rigidbody rigidbody;

    void Start()
    {
        rigidbody = this.GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("HorizontalPlayer" + number); //获取水平轴的偏移量
        float vertical = Input.GetAxis("VerticalPlayer" + number); //获取垂直轴的偏移量

        rigidbody.velocity = transform.forward * vertical * speed; //速度
        rigidbody.angularVelocity = transform.up * horizontal * angularSpeed; //旋转

        // 按住shift时快速移动
        if (number == 1 && Input.GetKey(KeyCode.LeftShift))
        {
            rigidbody.velocity = transform.forward * vertical * 1.8f * speed;
        }
        // 按住+时快速移动
        if (number != 1 && Input.GetKey(KeyCode.Plus))
        {
            rigidbody.velocity = transform.forward * vertical * 1.8f * speed;
        }
    }
}

子弹使用 胶囊碰撞体(Capsule Collider)

设置方向 Direction 为 Z-Axis(Z轴)

调整Radius和Height使胶囊体大小和子弹大小相近

然后给子弹添加刚体(Rigidbody)属性

之后把子弹设置为预制体,后面子弹发射时要实例化这个预制体

定位坦克炮弹发射位置

tank下创建一个GameObject,调整到希望的炮弹发射位置,命名为FirePosition

创建一个开火脚本(TankAttack),内容如下:

脚本挂载在tank上后,设置shellPrefab为之前做好的子弹预制体

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

public class TankAttack : MonoBehaviour
{
    public GameObject shellPrefab; //子弹预制体
    public KeyCode fireKey = KeyCode.Space; //发射键,默认空格space键
    private Transform firePosition; //子弹发射位置

    // Start is called before the first frame update
    void Start()
    {
        firePosition = transform.Find("FirePosition");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(fireKey))
        {
            //GameObject.Instantiate为实例化元素,3个参数分别为 要实例化的预制体,实例化的位置,实例化的方向
            GameObject.Instantiate(shellPrefab, firePosition.position, firePosition.rotation);
        }
    }
}

 

发布了97 篇原创文章 · 获赞 21 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/sun124608666/article/details/101681830