unity3d 学习笔记_____Native2d 刚体、碰撞器、关节的使用

本文深入探讨游戏开发中关节与碰撞器的应用,包括HingeJoint、SpringJoint、DistanceJoint的使用方法及参数设置,以及如何实现物体间的动态交互。通过实例演示,展示如何利用这些组件创建互动性强的游戏环境。
部署运行你感兴趣的模型镜像





MassMass of the rigidbody.
Linear DragDrag coefficient affecting positional movement.
Angular DragDrag coefficient affecting rotational movement.
Gravity ScaleDegree to which the object is affected by gravity.
Fixed AngleCan the rigidbody rotate when forces are applied?
Is KinematicIs the rigidbody moved by forces and collisions?
InterpolateHow the object's movement is interpolated between physics updates (useful when motion tends to be jerky).
NoneNo movement smoothing is applied.
InterpolateMovement is smoothed based on the object's positions in previous frames.
ExtrapolateMovement is smoothed based on an estimate of its position in the next frame.
Sleeping ModeHow the object "sleeps" to save processor time when it is at rest.
Never SleepSleeping is disabled.
Start AwakeObject is initially awake.
Start AsleepObject is initially asleep but can be woken by collisions.
Collision DetectionHow collisions with other objects are detected.
DiscreteA collision is registered only if the object's collider is in contact with another during a physics update.
ContinuousA collision is registered if the object's collider appears to have contacted another between updates.


碰撞器Collider 分为两种:

(1)刚体碰撞

(2) 触发碰撞、会穿透其他刚体

对应Collider组件中的is Trigger,两种都会产生碰撞事件

	void OnCollisionEnter2D(Collision2D cod)
	{
		print (cod.gameObject.name);
		if(cod.rigidbody)
		cod.rigidbody.AddForce(new Vector2(0,500f));
	}


	void OnTriggerEnter2D(Collider2D other)
	{
		Destroy(other.gameObject);
	}

两个方法都属于MonoBehaviour的Message 回调方法,注意区分它们的参数类型是不同的

另外还有 OnTriggerExit2DOnTriggerStay2D、 OnCollisionExit2DOnCollisionStay2D 


关节的使用:



SpringJoint和DistantanceJoint有点类似多了弹性参数和频率设置(在unity中暂时没看出效果)


hingeJoint 可以理解为一个围绕Z轴旋转的关节,可以设置响应moto、以及角度的限制


sliderJoint 滑动关节类似hingeJoint 以一个角度值设置Moto进行滑动,可以对距离(translation)进行限制


下面是一个用HingeJoint做的一个demo:




两只小鸟同时添加HingeJoint 连接到盒子,后面的小鸟加下以下脚本控制方向键盘即可前后运动

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(HingeJoint2D))]
public class MotoControl : MonoBehaviour {

	public float MotoSpeed = 0;
	private JointMotor2D motor;
	HingeJoint2D hj;

	// Use this for initialization
	void Start () {
		hj = GetComponent<HingeJoint2D>();
		motor = hj.motor;
		hj.useMotor = true;
		motor = hj.motor;
		motor.motorSpeed = MotoSpeed;
		motor.maxMotorTorque = 10000;
	}
	
	// Update is called once per frame
	void Update () {
		motor.motorSpeed = Input.GetAxis("Horizontal") * MotoSpeed;
		hj.motor = motor;
	}
}

做Demo时遇到的问题:

(1)刚开始用hj.motor.motorSpeed一直报错,后来分两步写终于没问题
motor = hj.motor;
motor.motorSpeed = MotoSpeed;
(2)错是没了,可是小车还是不走,最后知道还需要把motor对象重新赋给HingeJoint
motor.motorSpeed = Input.GetAxis("Horizontal") * MotoSpeed;
hj.motor = motor;


其他一些常用的属性:

breakForce、breakTorque分别设定多大力、多大扭矩能给丫的拆了

connectedBody 连接的另外一个刚体的引用 (Joint2D中不存在)

hj.connectedBody = null;连接到一个空对象上

要想断开关节直接去掉关节组件 destroy(hj);

断开将发送 OnJointBreak Message.



您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值