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使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- lua源代码学习(一)lua的c api外围实现
工作后,整个人已经比較松懈了.尽管一直在看lua的源代码.可是一直是比較零碎的时间,没有系统的整理,所以还是收获不多.由于近期工作也不是非常忙了,就想整理下lua的源代码学习的笔记.加深下印象,并分享 ...
- testng入门教程3用TestNG执行case的顺序
本教程介绍了TestNG中执行程序的方法,这意味着该方法被称为第一和一个接着.下面是执行程序的TestNG测试API的方法的例子. 创建一个Java类文件名TestngAnnotation.java在 ...
- linux服务后台管理
把进程放到后台有两种方法 1.cmmand & 2.ctrl+z 暂停到后台 查看后台服务 jobs 把后台进程移到前台 fg %2 工作号 恢复到前台 后台服务继续执行 bg ...
- css样式表--样式表分类
样式表分类 1.内联式.写在body里.控制精确,可重复性差. <body> <div style="color:#90F">更好发挥的返回结果还 < ...
- Java事件监听的四种实现方式
1.事件对象: 一般继承自java.util.EventObject对象,由开发者自行定义. 2.事件源: 就是触发事件的源头,不同的事件源会触发不同的事件类型. 3.事件监听器: 事件监听器负责监听 ...
- 网页中自适应的显示PDF
PDF格式呢,是一个高大的新式,如何在不同的浏览器中自适应显示,是一个值得研究的问题. 这里说明重点部分:获取浏览器宽高. IE中: document.body.clientWidth ==> ...
- ServiceStack DotNet Core前期准备
下载DotNet Core SDK 下载地址:https://dotnet.microsoft.com/download. 安装完成之后通过cmd的命令行进行确认. 官方自带的cmd比较简陋,可以用c ...
- MySQL从删库到跑路_高级(四)——存储过程
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.存储过程简介 1.存储过程简介 存储过程是一组具有特定功能的SQl语句集组成的可编程的函数,经编译创建并保存在数 ...
- testng使用DataProvider+Excel实现DDT
DDT,即数据驱动测试 Data Driver Test,我曾经记录了一篇关于python的DDT框架(Excel+DDT数据驱动实例),那么java中的DDT是怎么样的呢?在java中,可以用tes ...