欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[U3D Learning Note] Unity C# Surival Guide (1) -- Quick Tips and Assets

程序员文章站 2022-07-09 15:13:51
Change GameObject PositionKEY: transform.positontransform.position = new Vector3(1,2,3);//orpublic Vector3 newposi;transform.position = newposi;脚本已附加在某个GameObject上时 transform.position就是该物体的坐标User Input获取键盘的输入进行操作 因为是在游戏过程中进行操作 所以要放在Update()函数里voi...

Change GameObject Position

KEY: transform.positon

transform.position = new Vector3(1,2,3);
//or
public Vector3 newposi;
transform.position = newposi;

脚本已附加在某个GameObject上时 transform.position就是该物体的坐标

User Input

获取键盘的输入进行操作 因为是在游戏过程中进行操作 所以要放在Update()函数里

void Update()
{
    //if space key press down
    // print a message once
    if(Input.GetKeyDown(KeyCode.Space)){
        Debug.Log("press down space");
    }
    // if held on key A
    // print message every frame
    if(Input.GetKey(KeyCode.A)){
        Debug.Log("held on A");
    }
    // if key lift up
    // print message once
    if(Input.GetKeyUp(KeyCode.B)){
        Debug.Log("lift up B");
    }
}

Simple Movement

x轴或y上移动
KEY:

  • transform.Translate(): Moves the transform in the direction and
    distance of translation.
  • Time.deltaTime: The completion time in seconds since the last frame (Read Only). Use Time.deltaTime to move a GameObject in the y direction, at n units per second. Multiply n by Time.deltaTime and add to the y component.
  • Vector3.right : Shorthand for writing Vector3(1, 0, 0).
    Vector3.left: Shorthand for writing Vector3(-1, 0, 0).
    Vector3.up: Shorthand for writing Vector3(0, 1, 0).
    Vector3.down: Shorthand for writing Vector3(0, -1, 0).
  • [SerializeField]: 强制序列化 使成员变量出现在inspector中 public变量默认是序列化的 通常用于private变量
  • Input.GetAxis(): Returns the value of the virtual axis identified by axisName. 由键盘控制移动 获取方向上坐标 (查看: Edit -> Project Settings -> Input Manager)
    • 对于键盘和操纵杆: 以"Horizontal"为例 水平方向上(x轴) 往负方向由左键控制正方向由右键控制 The value will be in the range -1…1 for keyboard and joystick input devices.The meaning of this value depends on the type of input control, for example with a joystick’s horizontal axis a value of 1 means the stick is pushed all the way to the right and a value of -1 means it’s all the way to the left; a value of 0 means the joystick is in its neutral position.(场景:角色控制
    • 对于鼠标: 以"Mouse X"为例 光标在水平方向上移动时物体跟随移动 If the axis is mapped to the mouse, the value is different and will not be in the range of -1…1. Instead it’ll be the current mouse delta multiplied by the axis sensitivity. Typically a positive value means the mouse is moving right/down and a negative value means the mouse is moving left/up. (场景:附在Main Camera上视觉画面移动(农药挂了以后巡视全地图
      [U3D Learning Note] Unity C# Surival Guide (1) -- Quick Tips and Assets
	[SerializeField] private float _speed = 0.5f; 
    void Update()
    {
        transform.Translate(Vector3.right); 
        //automatically move obj to right, move 1unit per frame
        transform.Translate(Vector3.right*Time.deltaTime); 
        //automatically move obj to right, move 1unit per second, new Veector3(1,0,0)*0.5f*realtime
        transform.Translate(Vector3.right*Time.deltaTime*_speed);
        //automatically move obj to right, move 1*_speed unit per second
        float horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(new Vector3(horizontalInput, 0, 0)*Time.deltaTime*_speed);
        //use keyboard to control
        float h = 5*Input.GetAxis("Mouse X");
        transform.Translate(new Vector3(h, 0, 0)*Time.deltaTime*_speed);
        //use mouse to control
    }

Update loop run 60 frames per second --> move 60 unit per second

Collectible GameObject

KEY: Box Collider
- Is Trigger: if checked, we can pass through the object; else, it’s a solid object.
- Ridgebody: 刚体。向对象添加刚体组件将使其运动在Unity物理引擎的控制下。即使不添加任何代码,如果还存在正确的Collider组件,则刚体对象将在重力作用下被下拉,并且会对与传入对象的碰撞做出反应。是让物体更自然运动的组件
- onTriggerEnter(): get info about the object collide with us when a collision occurs.(if we use solid object, use onTriggerCollision()

	private void OnTriggerEnter(Collider other) {
        // Collider other: whatever obj hit
        if(other.tag == "Player"){
            Destroy(this.gameObject); 
            // when the obj with script hit by the object with tag "Player",
            // destory the obj with script
        }
    }

场景:smash hit, fps, 贪吃蛇

Pause System

(这节用到GameDevHQ一个自带动画的模型…emmmmm因为付费所以就没用 但是也不妨碍学习 给随便一个cube附加自动移动的脚本就可以检验了
KEY: Pause the Game 场景:smash hit关卡内加减速

  • Time.timeScale: The scale at which time passes. This can be used for slow motion effects. When timeScale is 1.0 time passes as fast as realtime. When timeScale is 0.5 time passes 2x slower than realtime. When timeScale is set to zero the game is basically paused if all your functions are frame rate independent.
	 // on main camera
	 void Update()
	{
	    if(Input.GetKeyDown(KeyCode.Space)){
	        Time.timeScale = 0;
	        Debug.Log("press on space and stop the sence");
	    }
	    if(Input.GetKeyDown(KeyCode.R)){
	        Time.timeScale = 1;
	        Debug.Log("press on R and continue the sence");
	    }
	}

一个疑问 timeScale的改变不单单只是附着的相机视角下画面减速(类似倍速看)而是整个场景真实减速???
cube移动脚本将Time.deltaTime*_speed序列化显示 并且暂停脚本暂停时改为Time.timeScale = 0.25f可以看到结果如下 说明是整个场景真实减速
[U3D Learning Note] Unity C# Surival Guide (1) -- Quick Tips and Assets
[U3D Learning Note] Unity C# Surival Guide (1) -- Quick Tips and Assets
然后看了眼官方文档有下面这句话:

Except for realtimeSinceStartup and fixedDeltaTime, timeScale affects all the time and delta time measuring variables of the Time class.

Time.deltaTime改成 Time.fixedDeltaTime 果然不受影响 但是返回的数值都是Time.fixedDeltaTime 的默认值0.22 也就是说_speed的附加影响没有成功 所以要注意FixedUpdate函数或 Update函数中,Time.fixedDeltaTime 将自动返回正确的增量时间(这就是为什么建议使用Time.deltaTime
如果降低timeScale,建议也将Time.fixedDeltaTime降低相同的量。设置为零时,不会调用 具体还是看官方链接的代码

Post-Processing Effects

Window ->Package Manager -> Post Processing

  • Post Processing: allows u to add filters in Main Camera

For Main Camera, in Inspector, add component Post Processing Layer and we can notice that in option “layers” there shows “Nothing”. Edit Layers, in user layer, add a layer call “Post Processing”. Then edit the option “layers” for Main Camera as “Post Processing” layer.

  • Post Processing Layer: decide which layer is going to accept a Post Processing Volume.

Create a GameObject called “Post Process Volume”, which will store all Post Processing Effects we want to apply to the scene. Add a component Post Processing Volume. Choose the layer “Post Processing Layer”. Option Is Gobal, if checked, this post processing effects will be applied to entire scene. Option Profile , click New to create effects. We can adjust the Intensity (强度), Threshold(阈值), Diffusion(扩散) and other attributes. 像还有其他的比如配合Color Grading食用可以让场景光效更酷炫 这边就不多提 用于场景处理
[U3D Learning Note] Unity C# Surival Guide (1) -- Quick Tips and Assets

Destructible Crate

KEY: Smoke-Mirror Effect: When a solid object explodes, it breaks into a million pieces. (啊啊啊啊是我最喜欢的环节!!! 破坏破碎效果!!!!玩smash hit的时候就各种惊叹怎么做到的玻璃破碎得那么真实好看
原理:Physics and colliders 我们需要一个完整的solid object 还有一组fractured part(就是好多个碎块但是可以完整拼凑成一个一毛一样的solid object), 在solid object被执行Destory的同时, 这些fractured part形成爆炸飞出效果。对于fractured part,需要添加Rigidbody 组件, 并赋予重力选项(use gravity),再添加 Mesh Collider 使触面是几何映射(box collider是一个方体 而mesh collider是不规则几何图形即物体的样子,记得点选Convex。注意 这边fractured part是以prefabs的形式设置的。 然后看看代码

public class Crate : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject fracturedCrate;
    public GameObject explosionEffect;
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space)){
        	// before we destory the solid obj, we first instantiate the prefabs 
            // which is a series of fractured part as children in the same position and same rotation
            // and also the explosion effect
            Instantiate(explosionEffect, transform.position, Quaternion.identity);
            //Instantiate(fracturedCrate, transform.position, Quaternion.identity);
            // to let the power of destory more greater 
            GameObject fractureObj = Instantiate(fracturedCrate, transform.position, Quaternion.identity) as GameObject;
            Rigidbody[] allRigidbodies = fractureObj.GetComponentsInChildren<Rigidbody>();
            // grab rigidbody from all of the children elements 
            if(allRigidbodies.Length > 0){
                // if we grab rigidbody, write a loop, 
                // for each rigidbody we give it a invidual force
                foreach(var body in allRigidbodies){
                    body.AddExplosionForce(500, transform.position, 1);
                    // 500 newtons of force
                    // 1 explosionRadius: scale of affect other objects
                }
            }
            Destroy(this.gameObject);
        }
    }
}

emmmm但是这边讲的是爆炸场景效果 smash hit的话是对撞破碎 猜想smash hit也是一个完整的solid obj 和 一组破碎的prefabs 当撞击产生时根据弹珠接触点进行爆炸? 明天写个试试看 写对了就更

本文地址:https://blog.csdn.net/sinat_41663922/article/details/107596465

上一篇: 藏私房钱了

下一篇: 睡过头的理由