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基本使用 需求 当多个进 ...
随机推荐
- 关于微信小程序,你想知道的他们都问了
微信公开课深圳站小程序专场刚刚结束,大家通过"微信公开课+"互动小程序提出了许多问题.我们筛选了后台问得最多的九个问题进行解答,快来看看这里有没有你想要的答案吧! @谢杨:小程序是 ...
- spring boot 中用@value给static变量赋值
需求:改写一个JedisUtils,工具类,所以最好用静态方法和变量. @value("${redis.host}") private static String redisHos ...
- ASCII对照表
ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 A 97 a 2 ST ...
- python对象反射和函数反射
python的对象反射功能,经常在编程时使用.相比较其它的编程语言使用非常方便.反射就是用字符串来操作对象或者类,模块中的成员. 一.对象的反射 反射功能的实现,由这4个内置函数来实现(hasattr ...
- 安恒X计划12月月赛
ezweb 主要是序列化问题.没有PHP环境,在线运行的.实例化对象之后修改一下file.然后echo输出序列化的结果.不过下面有一个正则检查.数字前加一个+,影响了正则的匹配,但是对于序列化的还原没 ...
- js实现轮播图2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php深入学习
关于PHP程序员解决问题的能力 http://rango.swoole.com/archives/340 深入理解PHP内核 by xuhong大牛 http://www.php-internals. ...
- VS中出现“链接器工具错误,XXX工具模块对于SAFESEH映像是不安全的”的解决方法
1.“调试”菜单->“属性”->“配置属性”->“链接器”->“命令行” 2.在“其它选项”的输入框里输入 /SAFESEH:NO ,点击“应用”
- 把HDFS上的数据导入到Hive中
1. 首先下载测试数据,数据也可以创建 http://files.grouplens.org/datasets/movielens/ml-latest-small.zip 2. 数据类型与字段名称 m ...
- Vue源码解析之nextTick
Vue源码解析之nextTick 前言 nextTick是Vue的一个核心功能,在Vue内部实现中也经常用到nextTick.但是,很多新手不理解nextTick的原理,甚至不清楚nextTick的作 ...