unity3D-基礎2----用C#腳本操作基本的組件

unity3D-基礎2----用C#腳本操作基本的組件

一. 鼠標與鍵盤輸入

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

public class input : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.A)){
			Debug.Log("getA");
		}

		if (Input.GetKeyDown(KeyCode.A))
		{
			Debug.Log("k down");
		}
		if(Input.GetKeyUp(KeyCode.A))
		{
			Debug.Log("key up");
		}
// 0-左鍵1-右鍵 2-中建
		if (Input.GetMouseButton(0))
		{
			Debug.Log("左鍵");
		}

		if (Input.GetMouseButtonUp(0))
		{
			Debug.Log("左鍵uo");
		}
		if (Input.GetMouseButtonDown(0))
		{
			Debug.Log("左鍵down");
		}
	}
}

二. 變換組件移動遊戲物體

運動特點:

  1. 移動的物體會"穿透"場景中的其他物理模型;
  2. 移動的物體不會受重力影響,到達場景邊緣外,不會下落
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class student : MonoBehaviour
{
	private Transform m_Transform;
	// Use this for initialization
	void Start () {
		m_Transform=gameObject.GetComponent<Transform>();
		
	}
	
	// Update is called once per frame
	void Update () {
//		m_Transform.Translate(Vector3.forward * 0.1f /*向前*/, Space.Self /*以自身爲座標系*/);
		if (Input.GetKey(KeyCode.W))
		{
			m_Transform.Translate(Vector3.forward * 0.1f /*向前*/, Space.Self /*以自身爲座標系*/);
		}if (Input.GetKey(KeyCode.S))
		{
			m_Transform.Translate(Vector3.back * 0.1f /*向前*/, Space.Self /*以自身爲座標系*/);
		}if (Input.GetKey(KeyCode.A))
		{
			m_Transform.Translate(Vector3.left * 0.1f /*向前*/, Space.Self /*以自身爲座標系*/);
		}if (Input.GetKey(KeyCode.D))
		{
			m_Transform.Translate(Vector3.right * 0.1f /*向前*/, Space.Self /*以自身爲座標系*/);
		}
	}
}

三. 剛體簡介

作用:添加了剛體左鍵的遊戲物體,就有了重力,就會做足以與落體運動,也就意味着可以像現實生活中的物體一樣運動
屬性:

  1. Mass(質量): 單位Kg
  2. Drag(阻力): 0表示無阻力,值很大時物體會停止運動
  3. Angular Drag(角阻力): 受到扭曲時的空氣阻力,0表示無阻力,值很大時會停止運動
  4. Use Gravity(使用重力):是否使用重力,默認使用

使用剛體移動的物體:

  1. 相關方法

Rigidbody.MovePosition(Vextor3) : 使用剛體移動物體的位置
注意:
a. 使用剛體移動位置,物體時根據世界座標系的方向移動的
b. 使用剛體移動物體,物體會觸發物理的相關事件

  1. 參數

MovePosition中的Vector3要使用"當前位置"+方向 的方式
**Transform.Position:**當前物體的位置

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

public class Ridibody : MonoBehaviour
{
	private Rigidbody _rigidbody;

	private Transform _transform;
	// Use this for initialization
	void Start () {
		_rigidbody=gameObject.GetComponent<Rigidbody>();
		_transform = gameObject.GetComponent<Transform>();

	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.W))
		{
			_rigidbody.MovePosition(_transform.position+Vector3.forward*0.1f);
		}if (Input.GetKey(KeyCode.A))
		{
			_rigidbody.MovePosition(_transform.position+Vector3.left*0.1f);
		}if (Input.GetKey(KeyCode.S))
		{
			_rigidbody.MovePosition(_transform.position+Vector3.back*0.1f);
		}if (Input.GetKey(KeyCode.D))
		{
			_rigidbody.MovePosition(_transform.position+Vector3.right*0.1f);
		}
		
	}
	
}


四. 碰撞體

  1. 簡介:

使用剛體移動的物體,與場景中其他的物體相碰撞:
其實是碰撞的目標物體的“碰撞體”組件,也就是 Collider。
另外和目標物體碰撞的,是我們移動的物體的自身的“碰撞體”組件。(即不是兩個物體碰撞二十兩個物體的Collider組件相碰撞)
碰撞體可以理解爲我們的模型的“外骨骼”。
模型只要加了剛體,就必須要加碰撞體,否則沒有意義(基本模型都帶有碰撞體)


物體碰撞之間的關係如下圖剛體和碰撞器.png

  1. Box Collider
  2. 簡介盒子碰撞體,形狀是立方體
  3. 組件屬性:

Center[中心點]  設置模型的中心點 x,y,z 
size : 碰撞體組件的大小,默認與大小與其他組件大小相同

  1. sphere collider(球星碰撞體 )

radius[半徑 ] 碰撞體組件的半徑是多少

  1. capsule collider(膠囊碰撞體)

Height[高度]
設置 Capsule Collider 的高度。
Direction[方向]
設置 Capsule Collider 的高度方向(軸向)

  1. mesh collider(用於精細的碰撞計算)

網格碰撞體,用於包裹複雜結構的模型
mesh: 根據指定的網格,生成碰撞體


五. 剛體常用方法介紹

要點:

  1. AddForce()
  2. AddRelative()
  3. FixedUpdate()
  4. AddForce()
  5. 作用

給剛體添加一個力,讓剛體按"世界座標系"進行運動.

  1. 代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class forceTest : MonoBehaviour
{
	private Rigidbody _rigibody;
	// Use this for initialization
	void Start () {
		_rigibody = gameObject.GetComponent<Rigidbody>();//獲取定義組件的信息 
		
	}
	
	// Update is called once per frame
	void Update () {
		_rigibody.AddForce(Vector3.forward,ForceMode.Force);
	}
}

  1. ForceMode 參數:

Acceleration: (加速度)
Force: (力)這種模式通常用於設置真實的物理
Impulse:(衝擊力)這種模式通常用於瞬間發生的力
VelocityCHange: 速度的變化

  1. AddRelative()

作用給剛體添加一個力,讓物體按"自身座標系"進行運動

  1. FixedUpdate()
固定更新方法。
所有和物理相關的操作,代碼都要寫在 FixedUpdate()方法體內。
固定更新的時間間隔是 0.02 秒,1 秒執行 50 次。
Edit-->Project Settings-->Time 面板中的Fixed Timestep參數設置。
Update()方法是每幀執行一次。
畫面每渲染完一次,就是一幀,每幀的時間是不固定的。
在 Update()方法中執行物理操作,會出現卡頓的情況。

六. 剛體的碰撞事件與處理

  • 碰撞事件簡介

一個用剛體控制的物體與另一個物體碰撞時,就會除發碰撞事件:注意:目標物體必須帶有Collider組件,不然就會穿過目標.

  • 碰撞事件監測方法
  1. OnCollisionEnter

當碰撞開始時調用,只會調用該方法一次

  1. OnCollisionEixt

當碰撞結束時調用,只會調用該方法一次

  1. OnCollisionStay

當碰撞進行中時,會持續調用該方法

  1. Collision 參數

碰撞 一個類; 作用 用於傳遞碰撞信息

Collision.gameObject 屬性:與當前碰撞的1物體的引用
gameObject.name 屬性,當前物體的名字

using UnityEngine;
using System.Collections;

public class RgidibodyMove : MonoBehaviour {

	private Rigidbody m_Rigidboby;
	private Transform m_Transform;

	void Start () {
		m_Rigidboby = gameObject.GetComponent<Rigidbody> ();
		m_Transform = gameObject.GetComponent<Transform> ();
	}

	void Update () {
		if(Input.GetKey(KeyCode.W))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.forward * 0.2f);
		}

		if(Input.GetKey(KeyCode.S))
		{	
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.back * 0.2f);
		}

		if(Input.GetKey(KeyCode.A))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.left * 0.2f);
		}

		if(Input.GetKey(KeyCode.D))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.right * 0.2f);
		}
	}

	private void OnCollisionEnter(Collision other)
	{
		if (other.gameObject.name != "Ground")
		{
			Debug.Log("enter"+other.gameObject.name);
		}
	}

	private void OnCollisionExit(Collision other)
	{
		if (other.gameObject.name != "Ground")
		{
			Debug.Log("exit"+other.gameObject.name);
		} 

	}
	private void OnCollisionStay(Collision other)
	{
		if (other.gameObject.name != "Ground")
		{
			Debug.Log("stay"+other.gameObject.name);
		}
	}
}

誰是碰撞的發起者,在被配碰撞的物體上添加

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

public class collisioncube : MonoBehaviour {
 private void OnCollisionEnter(Collision other)
 {
  if (other.gameObject.name != "Ground")
  {
   Debug.Log("enter"+other.gameObject.name);
  }
 }

 private void OnCollisionExit(Collision other)
 {
  if (other.gameObject.name != "Ground")
  {
   Debug.Log("exit"+other.gameObject.name);
  } 

 }
 private void OnCollisionStay(Collision other)
 {
  if (other.gameObject.name != "Ground")
  {
   Debug.Log("stay"+other.gameObject.name);
  }
 }
}


七. 剛體出發事件的監測與處理

觸發事件簡介:

  1. 觸發器:

碰撞體屬性面板的"**IS Trigger"**選項,當前遊戲物體變成了觸發器,(移動的剛體組件會穿過勾選此選項的物體)

  1. 觸發事件

當一個用剛體控制的物體進入到另外一個物體的觸發器範圍內,就是觸發事件。
觸發用途:不與目標物體發生直接的碰撞(接觸),而是隻要進入目標物體的“觸
發範圍”就能執行某些特定操作。
代碼

using UnityEngine;
using System.Collections;

public class RgidibodyMove : MonoBehaviour {

	private Rigidbody m_Rigidboby;
	private Transform m_Transform;

	void Start () {
		m_Rigidboby = gameObject.GetComponent<Rigidbody> ();
		m_Transform = gameObject.GetComponent<Transform> ();
	}

	void Update () {
		if(Input.GetKey(KeyCode.W))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.forward * 0.2f);
		}

		if(Input.GetKey(KeyCode.S))
		{	
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.back * 0.2f);
		}

		if(Input.GetKey(KeyCode.A))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.left * 0.2f);
		}

		if(Input.GetKey(KeyCode.D))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.right * 0.2f);
		}
	}

//	private void OnCollisionEnter(Collision other)
//	{
//		if (other.gameObject.name != "Ground")
//		{
//			Debug.Log("enter"+other.gameObject.name);
//		}
//	}
//
//	private void OnCollisionExit(Collision other)
//	{
//		if (other.gameObject.name != "Ground")
//		{
//			Debug.Log("exit"+other.gameObject.name);
//		} 
//
//	}
//	private void OnCollisionStay(Collision other)
//	{
//		if (other.gameObject.name != "Ground")
//		{
//			Debug.Log("stay"+other.gameObject.name);
//		}
//	}
	private void OnTriggerEnter(Collider other)
	{
		Debug.Log("student enter"+other.gameObject.name);
	}

	private void OnTriggerStay(Collider other)
	{
		Debug.Log("student stay"+other.gameObject.name);
	}

	private void OnTriggerExit(Collider other)
	{
		Debug.Log("student exit"+other.gameObject.name);
	}	
}


ublic class Triggercube : MonoBehaviour {

	// Use this for initialization
	private void OnTriggerEnter(Collider other)
	{
		Debug.Log("cube enter"+other.gameObject.name);
	}

	private void OnTriggerStay(Collider other)
	{
		Debug.Log("cube stay"+other.gameObject.name);
	}

	private void OnTriggerExit(Collider other)
	{
		Debug.Log("cube exit"+other.gameObject.name);
	}	
}


八. 網格組件之網絡過濾器和渲染器

1.簡介
網格過濾器:Mesh Filter。
該組件只有一個“Mesh”屬性,用於設置當前遊戲物體使用哪個模型進行展示。
Mesh:網格,也就是模型 (決定物體的屬性)
2.屬性
Cast Shadows [投射陰影]
On:開啓陰影顯示
Off:關閉陰影顯示
Receive Shadows [接收陰影]
選中就是接收
不選中就是不接收
Materials [材質球]
用於設置用哪個材質球渲染當前的模型(Mesh)。
我們拖拽到遊戲物體身上的材質球,其實就是賦予給了這個組件的這個屬性上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章