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





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.



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习Unity3D时,以下是一些重要的笔记: 1. Unity3D基础知识: - 游戏对象(Game Objects)和组件(Components):了解游戏对象的层次结构和组件的作用。 - 场景(Scenes)和摄像机(Cameras):学会如何创建场景并设置摄像机视角。 - 材质(Materials)和纹理(Textures):掌握如何创建和应用材质和纹理。 - 动画(Animations):学习如何创建和控制游戏对象的动画。 2. 脚本编程: - C#语言基础:了解C#语言的基本语法和面向对象编程概念。 - Unity脚本编写:学习如何编写脚本来控制游戏对象的行为和交互。 - 常见组件和功能:掌握常见的Unity组件和功能,如碰撞(Colliders)、刚体(Rigidbodies)、触发(Triggers)等。 3. 游戏开发流程: - 设计游戏关卡:了解如何设计游戏场景和关卡,包括布局、道具、敌人等。 - 游戏逻辑实现:将游戏规则和玩家交互转化为代码实现。 - UI界面设计:学习如何设计游戏中的用户界面,包括菜单、计分板等。 - 游戏优化和调试:优化游戏性能,解决常见的错误和问题。 4. 学习资源: - Unity官方文档和教程:官方提供了大量的文档和教程,逐步引导你学习Unity3D。 - 在线教程和视频教程:网上有很多免费和付费的Unity教程和视频教程,可根据自己的需求选择学习。 - 社区论坛和博客:加入Unity开发者社区,与其他开发者交流并获取帮助。 通过系统地学习这些内容,你将能够掌握Unity3D的基础知识并开始开发自己的游戏项目。记得不断实践和尝试,不断提升自己的技能!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值