碰撞體組件collider
兩個物體發(fā)生碰撞,實際上產(chǎn)生碰撞的是兩個碰撞體
edit collider---修改碰撞體


當碰撞體邊框碰到物體的時候會產(chǎn)生效果

當一個cube的碰撞體是使用的球形碰撞體,那么物理性質也會是球的表現(xiàn)形式
物理材質
新建的一個物理材質有如下屬性:

動態(tài)摩擦力(dynamic friction)
靜態(tài)摩擦力(static friction)
彈力 (bounciness)
組合摩擦力
組合彈力
角色移動控制
由于我的unity里面沒有人物模型因此需要在unity商店中找一個免費的資源,簡述一下如何在商店中購買資源以及引用到unity中。
1,首先找到一個免費的資源包,然后購買

2,購買之后點擊 open in unity
3,當unity中打開了package manager之后,再依次點擊download 和 import


素材導入成功!

可以看到模型和預設體都在這個文件目錄里面了,然后我們看一下模型的格式----一般來說unity官方推薦的模型格式為fbx

可以直接將預設體拖到場景當中

模型的Z軸一般來說都是移動的前方,因此在選用模型的時候盡量選擇Z軸是模型前方的模型,以方便寫代碼。

不同的預設體有不同的動作表示


動作控制器---不同的動作可以通過不同的動作控制器實現(xiàn),,
游戲腳本
游戲角色需要少量代碼來控制游戲角色的控制,以及物品控制。
右鍵--》新建游戲腳本==》C#

比如說我們要控制角色移動,那么我們應該知道是通過的什么方式控制的,可以是鍵盤也可以是搖桿手柄。如果是搖桿手柄。那么如下圖:

可以通過三角公式求遙感移動的方向以及距離?;蛘咄ㄟ^求向量來控制角色移動,顯然后者更簡單,因此有一下代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
//算出方向向量
Vector3 direction = new Vector3(horizontal,0,vertical);
if (direction != Vector3.zero) {
//將游戲角色轉至指定的方向
transform.rotation = Quaternion.LookRotation(direction);
//將游戲對象移動
transform.Translate(Vector3.forward * 1 * Time.deltaTime);
}}}
寫完之后可以添加到角色整體模型上之后就可以控制角色移動了

然后還可以使用CrossFade播放動作效果,但是前提是模型采用的動畫組件是Animation
transform.GetComponent<Animation>().CrossFade("Idle");
如果是其他的動畫組件例如:Animator 方法就不同了
首先需要新建一個 Animator controlller 然后構建狀態(tài)機

這些狀態(tài)機中 entry是鏈接的一個 默認的狀態(tài),anyState是任意狀態(tài)中切換過來,善用anystate可以節(jié)約很大的邏輯量,構建好狀態(tài)機后,以下代碼為:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
private Animator animator;
// Start is called before the first frame update
void Start()
{}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
//算出方向向量
Vector3 direction = new Vector3(horizontal,0,vertical);
if (direction != Vector3.zero)
{
//讓角色做走的動畫
transform.GetComponent<Animator>().SetInteger("walkState", 1);
//將游戲角色轉至指定的方向
transform.rotation = Quaternion.LookRotation(direction);
//將游戲對象移動
transform.Translate(Vector3.forward * 1 * Time.deltaTime);
}
else {
//讓角色站立的動畫
//transform.GetComponent<Animation>().CrossFade("Idle01"); 當游戲角色采用的是 animation 屬性的時候使用
transform.GetComponent<Animator>().SetInteger("walkState", 0);
} }}
由于這里想到可以構建一個游戲角色完整的動畫因此我花了一點時間構建了一個游戲角色比較完整的動畫,代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
private Animator animator;
// Start is called before the first frame update
void Start()
{
Debug.Log("游戲啟動");
}
void walk(int speed, Vector3 direction) {
//將游戲角色轉至指定的方向
transform.rotation = Quaternion.LookRotation(direction);
//將游戲對象移動
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
//算出方向向量
Vector3 direction = new Vector3(horizontal,0,vertical);
if (direction != Vector3.zero)
{
//計算向量距離長度
Debug.Log("手柄長度為:" + Vector3.Magnitude(direction));
// 距離小于一半手柄長度時為跑狀態(tài)
if (Vector3.Magnitude(direction) < 0.7 ) {
//讓角色做走的動畫
transform.GetComponent<Animator>().SetInteger("walkState", 1);
walk(1, direction);
}
// 距離大于一半手柄長度時為跑狀態(tài)
if (Vector3.Magnitude(direction) >= 0.7 ) {
transform.GetComponent<Animator>().SetInteger("walkState", 3);
walk(3, direction);
//奔跑的時候按下contrl鍵為沖刺
if(Input.GetKey(KeyCode.LeftControl))
{
transform.GetComponent<Animator>().SetInteger("walkState", 5);
walk(5, direction);
}}
if (Input.GetKeyDown(KeyCode.Space))
{
transform.GetComponent<Animator>().SetInteger("walkState", 2);
}
}
if (direction == Vector3.zero)
{
//讓角色站立的動畫
//transform.GetComponent<Animation>().CrossFade("Idle01"); 當游戲角色采用的是 animation 屬性的時候使用
transform.GetComponent<Animator>().SetInteger("walkState", 0);
if (Input.GetKeyDown(KeyCode.Space))
{
transform.GetComponent<Animator>().SetInteger("walkState", 2);
}
} }}
加上碰撞體,實現(xiàn)碰撞


AR項目
1,注冊高通VR賬號
https://developer.vuforia.com/
2,下載高通SDK,并導入到項目中

3,配置license key


下載后直接安裝到unity目錄即可
如果不想注冊賬號可以上傳圖片到高通




圖片上傳完成,評級越高越好識別,如果評分過低建議換一張圖片
將數(shù)據(jù)庫下載下來,

配置項目
1,刪掉main camera
2,使用 ARcamera
gameobject=》vuforia engine =>AR camera
3,使用imagetarget,并配置
gameobject=》vuforia engine => camera image =》 Camera Image Target

4,配置 vuforia configuration
window =》vuforia configuration

5,放置要出現(xiàn)的AR物體
把要出現(xiàn)的物體作為imagetarget的子物體
6,啟動項目掃描物體即可看到
總結
經(jīng)過你這段時間對C#的學習了解,熟悉了C#的一些語法特性以及unity引擎的基本操作,基本了解到了游戲開發(fā)的大概,一半來說C#現(xiàn)在常用的桌面的開發(fā)是WPF框架,而C#最基礎的winforms不怎么美觀,而游戲開發(fā)基本上都是基于某一個游戲引擎,從而降低了動畫操作編程的門檻。