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


當(dāng)碰撞體邊框碰到物體的時(shí)候會(huì)產(chǎn)生效果

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

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

2,購(gòu)買之后點(diǎn)擊 open in unity
3,當(dāng)unity中打開(kāi)了package manager之后,再依次點(diǎn)擊download 和 import


素材導(dǎo)入成功!

可以看到模型和預(yù)設(shè)體都在這個(gè)文件目錄里面了,然后我們看一下模型的格式----一般來(lái)說(shuō)unity官方推薦的模型格式為fbx

可以直接將預(yù)設(shè)體拖到場(chǎng)景當(dāng)中

模型的Z軸一般來(lái)說(shuō)都是移動(dòng)的前方,因此在選用模型的時(shí)候盡量選擇Z軸是模型前方的模型,以方便寫代碼。

不同的預(yù)設(shè)體有不同的動(dòng)作表示


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

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

可以通過(guò)三角公式求遙感移動(dòng)的方向以及距離?;蛘咄ㄟ^(guò)求向量來(lái)控制角色移動(dòng),顯然后者更簡(jiǎn)單,因此有一下代碼
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) {
//將游戲角色轉(zhuǎn)至指定的方向
transform.rotation = Quaternion.LookRotation(direction);
//將游戲?qū)ο笠苿?dòng)
transform.Translate(Vector3.forward * 1 * Time.deltaTime);
}}}
寫完之后可以添加到角色整體模型上之后就可以控制角色移動(dòng)了

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

這些狀態(tài)機(jī)中 entry是鏈接的一個(gè) 默認(rèn)的狀態(tài),anyState是任意狀態(tài)中切換過(guò)來(lái),善用anystate可以節(jié)約很大的邏輯量,構(gòu)建好狀態(tài)機(jī)后,以下代碼為:
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)
{
//讓角色做走的動(dòng)畫
transform.GetComponent<Animator>().SetInteger("walkState", 1);
//將游戲角色轉(zhuǎn)至指定的方向
transform.rotation = Quaternion.LookRotation(direction);
//將游戲?qū)ο笠苿?dòng)
transform.Translate(Vector3.forward * 1 * Time.deltaTime);
}
else {
//讓角色站立的動(dòng)畫
//transform.GetComponent<Animation>().CrossFade("Idle01"); 當(dāng)游戲角色采用的是 animation 屬性的時(shí)候使用
transform.GetComponent<Animator>().SetInteger("walkState", 0);
} }}
由于這里想到可以構(gòu)建一個(gè)游戲角色完整的動(dòng)畫因此我花了一點(diǎn)時(shí)間構(gòu)建了一個(gè)游戲角色比較完整的動(dòng)畫,代碼如下:

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("游戲啟動(dòng)");
}
void walk(int speed, Vector3 direction) {
//將游戲角色轉(zhuǎn)至指定的方向
transform.rotation = Quaternion.LookRotation(direction);
//將游戲?qū)ο笠苿?dòng)
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)
{
//計(jì)算向量距離長(zhǎng)度
Debug.Log("手柄長(zhǎng)度為:" + Vector3.Magnitude(direction));
// 距離小于一半手柄長(zhǎng)度時(shí)為跑狀態(tài)
if (Vector3.Magnitude(direction) < 0.7 ) {
//讓角色做走的動(dòng)畫
transform.GetComponent<Animator>().SetInteger("walkState", 1);
walk(1, direction);
}
// 距離大于一半手柄長(zhǎng)度時(shí)為跑狀態(tài)
if (Vector3.Magnitude(direction) >= 0.7 ) {
transform.GetComponent<Animator>().SetInteger("walkState", 3);
walk(3, direction);
//奔跑的時(shí)候按下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)
{
//讓角色站立的動(dòng)畫
//transform.GetComponent<Animation>().CrossFade("Idle01"); 當(dāng)游戲角色采用的是 animation 屬性的時(shí)候使用
transform.GetComponent<Animator>().SetInteger("walkState", 0);
if (Input.GetKeyDown(KeyCode.Space))
{
transform.GetComponent<Animator>().SetInteger("walkState", 2);
}
} }}
加上碰撞體,實(shí)現(xiàn)碰撞


AR項(xiàng)目
1,注冊(cè)高通VR賬號(hào)
https://developer.vuforia.com/
2,下載高通SDK,并導(dǎo)入到項(xiàng)目中

3,配置license key


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




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

配置項(xiàng)目
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,啟動(dòng)項(xiàng)目掃描物體即可看到
總結(jié)
經(jīng)過(guò)你這段時(shí)間對(duì)C#的學(xué)習(xí)了解,熟悉了C#的一些語(yǔ)法特性以及unity引擎的基本操作,基本了解到了游戲開(kāi)發(fā)的大概,一半來(lái)說(shuō)C#現(xiàn)在常用的桌面的開(kāi)發(fā)是WPF框架,而C#最基礎(chǔ)的winforms不怎么美觀,而游戲開(kāi)發(fā)基本上都是基于某一個(gè)游戲引擎,從而降低了動(dòng)畫操作編程的門檻。