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组件相碰撞)
碰撞体可以理解为我们的模型的“外骨骼”。
模型只要加了刚体,就必须要加碰撞体,否则没有意义(基本模型都带有碰撞体)


物体碰撞之间的关系如下图unity3D-基础2----用C#脚本操作基本的组件

  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)。
我们拖拽到游戏物体身上的材质球,其实就是赋予给了这个组件的这个属性上。