Unity3D学习笔记(四):物理系统碰撞和预制体
 

 
 
----Constraints:约束(冻结)某一根轴,或者位移,或者旋转,冻结偏移度


 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionTest : MonoBehaviour {
//检测碰撞
//Collision是类,是发生碰撞信息的集合(另一个物体的信息)
//两个物体,发生接触的时候
private void OnCollisionEnter(Collision collision)
{
Debug.Log("我进来了!");
Debug.Log(collision.gameObject.name);
}
//两个物体,持续接触的时候
private void OnCollisionStay(Collision collision)
{
Debug.Log("我在里面了!");
}
//两个物体,发生分离的时候
private void OnCollisionExit(Collision collision)
{
Debug.Log("我出来了!");
}
//触发器检测
//Collider是碰撞器的引用,包含于collision
//触发器和触发器或碰撞器,发生接触的时候
private void OnTriggerEnter(Collider other)
{
Debug.Log("触发了!");
}
private void OnTriggerStay(Collider other)
{ }
private void OnTriggerExit(Collider other)
{ }
}
 
 
Unity复习

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallTest : MonoBehaviour {
Rigidbody rig;
private void Awake()
{
rig = GetComponent<Rigidbody>();
}
// Use this for initialization
void Start()
{
// 可以碰撞的 (运动学干刚体和非运动学刚体)
// 冻结的时候 刚体会进入休眠吗? 不会
// 如果不冻结的状态 经过几秒种以后 就会进入休眠状态
}
private void OnGUI()
{
GUILayout.Label("Sleep :" + rig.IsSleeping());
}
void OnCollisionEnter(Collision collision)
{
Debug.Log("有碰撞到");
}
// Update is called once per frame
void Update () {
rig.WakeUp(); // 唤醒 }
}
 
子弹销毁:延时销毁,集中销毁
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletMove : MonoBehaviour {
public float moveSpeed = ;
// Use this for initialization
void Start()
{
Destroy(gameObject, );
}
// Update is called once per frame
void Update () { }
void FixedUpdate()
{
transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Enemy")
{
// 敌人掉血
EnemyInfo enemyInfo = collision.gameObject.GetComponent<EnemyInfo>();
if(enemyInfo) enemyInfo.GetDamage(Random.Range(, ));
// 子弹销毁
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
// 子弹的预制体
public GameObject bulletPrefab; // 资源面板中的
// 生成的位置
public Transform firePos;
public float moveSpeed = ;
public float rotSpeed = ;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space))
{
// 发射子弹 在某个位置 实例化一个子弹
GameObject bullet = Instantiate(bulletPrefab, firePos.position, firePos.rotation);
BulletMove bulletMove = bullet.GetComponent<BulletMove>();
bulletMove.moveSpeed = ;
} }
// 物理相关的 写在 FixedUpdate
private void FixedUpdate()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime * Input.GetAxis("Vertical"));
transform.Rotate(Vector3.up * rotSpeed * Time.deltaTime * Input.GetAxis("Horizontal"), Space.World);
}
}
敌人信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyInfo : MonoBehaviour {
public int hp = ;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
public void GetDamage(int dmg)
{
hp -= dmg;
if(hp <= )
{
hp = ;
Destroy(gameObject);
}
}
}
敌人控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControl : MonoBehaviour {
public float moveSpeed = ;
Transform playerTrans;
// Use this for initialization
void Start () {
GameObject player = GameObject.Find("Player");
if(player)
{
playerTrans = player.transform; // 如果没有找到主角 会报 空引用
}
} // Update is called once per frame
void Update () {
transform.LookAt(playerTrans);
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); }
}
敌人生成点:需要生成的预制体,需要生成的位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemy : MonoBehaviour {
// 需要生成的预制体
public GameObject enemyPrefab;
float spawnInterval;
float timeCount = ;
// Use this for initialization
void Start () {
spawnInterval = Random.Range(, );
} // Update is called once per frame
void Update () {
timeCount += Time.deltaTime;
if(timeCount >= spawnInterval)
{
Instantiate(enemyPrefab, transform.position, Quaternion.identity); // Quaternion.identity 表示没有旋转
spawnInterval = Random.Range(, );
timeCount = ;
} }
}
Unity3D学习笔记(四):物理系统碰撞和预制体的更多相关文章
- java之jvm学习笔记四(安全管理器)
		java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ... 
- javascript学习笔记(四) Number 数字类型
		数字格式化方法toFixed().toExponential().toPrecision(),三个方法都四舍五入 toFixed() 方法指定小数位个数 toExponential() 方法 用科学 ... 
- muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制
		目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ... 
- kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置
		KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ... 
- Linux学习笔记(四) vi编辑器
		一.vi 编辑器 vi 编辑器 (Visual Interface) 是所有 Unix 及 Linux 系统下标准的编辑器,相当于 Windows 系统中的记事本 它有三种模式,分别是: Comman ... 
- Linux学习笔记(七) 查询系统
		1.查看命令 (1)man 可以使用 man 命令名称 命令查看某个命令的详细用法,其显示的内容如下: NAME:命令名称 SYNOPSIS:语法 DESCRIPTION:说明 OPTIONS:选项 ... 
- MySql学习笔记四
		MySql学习笔记四 5.3.数据类型 数值型 整型 小数 定点数 浮点数 字符型 较短的文本:char, varchar 较长的文本:text, blob(较长的二进制数据) 日期型 原则:所选择类 ... 
- unity3d学习笔记(一)  第一人称视角实现和倒计时实现
		unity3d学习笔记(一) 第一人称视角实现和倒计时实现 1. 第一人称视角 (1)让mainCamera和player(视角对象)同步在一起 因为我们的player是生成的,所以不能把mainCa ... 
- ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁
		作者:Grey 原文地址: ZooKeeper学习笔记四:使用ZooKeeper实现一个简单的分布式锁 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 当多个进 ... 
随机推荐
- 2018最新php笔试题及答案(持续更新)
			php中include和require的区别 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容.include 和 require 语句用于在执行流中插入写在其他文件中 ... 
- AngularJS2.0起步
			ES6工具链 要让Angular2应用跑起来不是件轻松的事,因为它用了太多还不被当前主流浏览器支持的技术.所以,我们需要一个工具链: 
- iOS UI基础-2.0按钮操作与形变
			按钮状态 1.normal:默认状态 Default 对应的枚举常量:UIControlStateNormal 2.highlighted(高亮状态) 按钮被按下去的时候(未松开) 对应的枚举常量 ... 
- mysql外键使用和事物使用
			mysql外键功能主要是为了保证关联表数据的一致性,主要目的是控制存储在外键表中的数据. 使两张表形成关联,外键只能引用外表中的列的值! 例如: a b 两个表 a表中存有 客户号,客户名称 b表中存 ... 
- IDEA 添加jar包的三种方式(重点:new uer Libraries)
			学习参考: http://zyjustin9.iteye.com/blog/2172445 交代: 本文只讲述类似eclipse中new uer Libraries的功能,即自己新建lib库.其他方法 ... 
- FRM-40212:  Invalid value for field %s.
			Cause: Caused by one of the following: 1. The value is not of the proper data type. 字段类型不对 2 ... 
- Impala与Hive的比较
			1. Impala架构 Impala是Cloudera在受到Google的Dremel启发下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的Hive+MapReduce批 ... 
- pandas练习(一)------ 了解数据
			探索Chipotle快餐数据 (相关数据见github) 步骤1 导入pandas库 import pandas as pd 步骤2 导入数据集 path1 = "./data/chipot ... 
- 20165207 Exp3 免杀原理与实践
			Exp3 免杀原理与实践 1.实验内容 1.1.使用msf 1.1.1. 确定基准线 首先看kali的ip 直接msfvenom的结果,不加其他的东西: 使用VirusTotal得到的检测这个程序得到 ... 
- MYSQL的存储过程和函数简单写法
			存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] ro ... 
