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基本使用 需求 当多个进 ...
随机推荐
- MySQL如何开启慢查询
一 简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二 参数说明 slow_query_log 慢查询开启状态 slo ...
- 廖威雄: 思维导图:利用__attribute__((section()))构建初始化函数表与Linux内核init的实现
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/juS3Ve/article/details/79049404 本文具体解说了利用__attribut ...
- 第十八篇:融汇贯通--谈USB Video Class驱动
USB Video Class驱动是WINDOWS系统包含的一个针对于USB VIDEO 类的驱动程序. 好多project师都做过USB VIDEO设备端的开发, 基本的工作内容为: 使用FIRMW ...
- RGB颜色参考
实色效果 英文名称 R.G.B 16色 实色效果 英文名称 R.G.B 16色 Snow 255 250 250 #FFFAFA PaleTurquoise1 187 255 255 #BBF ...
- 让Logstash每次都从头读文件及常见问题
input { file { path => ["/data/test.log"] start_position => "beginning" si ...
- 简单的应用可以用storyBoard
可是问题,你不知道你的项目有多复杂,storyBoard跳转控制有代码这么灵活吗? 1. 假是是根据推送来推出页面呢? 2. 假如我要根据不同情况不停地推出不同的页面呢?storyBoard怎么确定关 ...
- eigen 笔记1
c++ 的 eigen 类似于 python 的 numpy, 还有一个类似的库是 Armadillo, 当然还有 opencv. Armadillo 与 matlab 在函数名称上更接近, 但是 T ...
- JSP—cookie
cookie的作用: 1.对特定对象的追踪,如访问次数,最后访问时间,路径等 2.统计网页的浏览次数 3.在cookie有效期内,记录用户的登录信息 4.实现个性化,记录用户的喜好 5.保存的数据存在 ...
- JSON语法2
把 JSON 文本转换为 JavaScript 对象 JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 Jav ...
- Linux命令: 向文件写内容,编辑文件,保存文件,查看文件,不保存文件
1.找到要编辑的文件 2.敲 vi t1.txt ,显示文件内容(vim命令) 3.敲 i,最下面变成INSERT 4.编辑自己想要的内容 5a.敲ESC:wq回车 5b.如果不想保存文件在时敲ES ...