玩法 / Impulse
Impulse Class
冲量
冲量是一个瞬间爆发力,为对象提供单一且即时的电源,是一种将力集中在一帧上的感觉。
冲量对象是如何工作的呢 ?
创建一个冲量对象。可手动将左侧栏中逻辑对象中的冲量拖入场景中,在属性面板中调整参数;也可以在脚本中动态创建冲量对象。
设置冲量对象属性 enable 为 true ,才可触发冲量效果。
impulseVector 属性表示力的方向。
impulseRadialForce 属性表示力的大小。
(示例代码中impulseId = "1602E908"中的1602E908替换方式为右键冲量对象,复制对象ID。更换为你的冲量对象ID即可)
ts
@Component
export default class ImpulseSample extends Script {
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 先设置为 false,玩家进入范围后再设置为 true,会有玩家突然凭空被弹开的效果
impulse.enable = true;
// 绝对冲量应用时会先清空物体自身速度,相对冲量会将冲量和物体当前速度叠加
impulse.impulseType = ImpulseType.Absolute;
// 只有为矢量力的情况下,impulseVector 属性才有意义
impulse.impulseForceType = ImpulseForceType.VectorForce;
// 设置为自定义的带方向的冲量值
impulse.impulseVector = new Vector(0, 0, 2000);
impulse.onImpulseEnter.add(()=>{
console.log("Impulse onImpulseEnter")
})
}
}@Component
export default class ImpulseSample extends Script {
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 先设置为 false,玩家进入范围后再设置为 true,会有玩家突然凭空被弹开的效果
impulse.enable = true;
// 绝对冲量应用时会先清空物体自身速度,相对冲量会将冲量和物体当前速度叠加
impulse.impulseType = ImpulseType.Absolute;
// 只有为矢量力的情况下,impulseVector 属性才有意义
impulse.impulseForceType = ImpulseForceType.VectorForce;
// 设置为自定义的带方向的冲量值
impulse.impulseVector = new Vector(0, 0, 2000);
impulse.onImpulseEnter.add(()=>{
console.log("Impulse onImpulseEnter")
})
}
}Hierarchy
↳
Impulse
Table of contents
Properties
onImpulseEnter: MulticastGameObjectDelegate |
|---|
| 发生冲量碰撞后的回调函数 |
click
Properties
onBeforeDestroyDelegate: MulticastDelegate<() => void> |
|---|
| 物体销毁前事件回调 |
onCustomPropertyChange: Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>> other |
| 监听自定义属性同步事件 |
onDestroyDelegate: MulticastDelegate<() => void> |
| 物体销毁后事件回调 |
onPropertyChange: Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>> |
| 监听系统属性同步事件 |
Accessors
enable(): boolean |
|---|
| 获取是否启用冲量 |
impulseForceType(): ImpulseForceType |
| 获取当前冲量力类型 |
impulseRadialForce(): number |
| 获取径向力的冲量值 |
impulseType(): ImpulseType |
| 获取冲量应用方式,绝对或相对 |
impulseVector(): Vector |
| 获取矢量力的冲量向量 |
click
Accessors
assetId(): string |
|---|
| 获取当前物体使用资源的GUID |
gameObjectId(): string |
| 获取物体的唯一标识(唯一标识一个对象的字符串)。 |
isDestroyed(): boolean |
| 当前物体是否被销毁 |
isReady(): boolean |
| 当前物体状态 |
localTransform(): Transform |
| 当前物体本地变换 |
name(): string |
| 返回当前物体名称 |
netStatus(): NetStatus |
| 获取当前物体同步状态 |
parent(): GameObject |
| 获取当前父物体 |
tag(): string |
| 获取当前物体的标签 |
worldTransform(): Transform |
| 当前物体世界变换 |
Methods
click
Methods
addComponent<T: extends Script<T>>(constructor: (...args: unknown[]) => T: extends Script<T>, bInReplicates?: boolean): T: extends Script<T> |
|---|
| 添加一个脚本组件 |
asyncGetChildByName(name: string): Promise<GameObject> |
| 异步根据名称查找子物体 |
asyncReady(): Promise<GameObject> |
| 物体准备好后返回 |
clone(gameObjectInfo?: GameObjectInfo): GameObject |
| 复制对象 |
destroy(): void |
| 删除对象 |
getBoundingBox(nonColliding?: boolean, includeFromChild?: boolean, outer?: Vector): Vector |
| 获取物体包围盒大小 |
getBounds(onlyCollidingComponents: boolean, originOuter: Vector, boxExtentOuter: Vector, includeFromChild?: boolean): void |
| 获取物体边界 |
getChildByGameObjectId(gameObjectId: string): GameObject |
| 根据 gameObjectId 查找子物体 |
getChildByName(name: string): GameObject |
| 根据名称查找子物体 |
getChildByPath(path: string): GameObject |
| 根据路径查找子物体 |
getChildren(): GameObject[] |
| 获取子物体 |
getChildrenBoundingBoxCenter(outer?: Vector): Vector |
| 获取所有子对象包围盒中心点(不包含父对象,父对象不可用返回[0,0,0]) |
getChildrenByName(name: string): GameObject[] |
| 通过名字查找所有的子物体 |
getComponent<T: extends Script<T>>(constructor?: (...args: unknown[]) => T: extends Script<T>): T: extends Script<T> |
| 获取指定类型的组件 |
getComponentPropertys<T: extends Script<T>>(constructor: (...args: unknown[]) => T: extends Script<T>): Map<string, IPropertyOptions> |
| 获取脚本组件属性 |
getComponents<T: extends Script<T>>(constructor?: (...args: unknown[]) => T: extends Script<T>): T: extends Script<T>[] |
| 获取指定类型的所有组件 |
getCustomProperties(): string[] |
| 获取所有自定义属性 |
getCustomProperty<T: extends CustomPropertyType>(propertyName: string): T: extends CustomPropertyType |
| 获取自定义属性 |
getCustomPropertyChangeDelegate(property): Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>> other |
| 给定对象属性修改时触发的事件代理 |
getPropertyChangeDelegate(property): Readonly<MulticastDelegate<(path: string, value: unknown, oldValue: unknown) => void>> other |
| 给定对象属性修改时触发的事件代理 |
getVisibility(): boolean |
| 获取物体是否被显示 |
moveBy(velocity: Vector, isLocal?: boolean): void other |
| 按给定的速度矢量随时间平滑地移动对象 |
moveTo(targetPosition: Vector, time: number, isLocal?: boolean, onComplete?: () => void): void other |
| 在指定时间内从当前位置平滑移动至目标位置 |
rotateBy(rotation: Quaternion Rotation, multiplier: number, isLocal?: boolean): void other |
| 按给定的旋转量随时间平滑地旋转对象 |
rotateTo(targetRotation: Quaternion Rotation, time: number, isLocal?: boolean, onComplete?: () => void): void other |
| 在指定时间内从当前旋转平滑变化至目标旋转 |
scaleBy(scale: Vector, isLocal?: boolean): void other |
| 按每秒给定的缩放矢量随时间平滑缩放对象 |
scaleTo(targetScale: Vector, time: number, isLocal?: boolean, onComplete?: () => void): void other |
| 在指定时间内从当前缩放平滑变化至目标缩放 |
setAbsolute(absolutePosition?: boolean, absoluteRotation?: boolean, absoluteScale?: boolean): void |
| 设置物体localTransform是相对于父物体或者世界 |
setCustomProperty(propertyName: string, value: undefined CustomPropertyType): void |
| 设置自定义属性 |
setVisibility(status: boolean PropertyStatus, propagateToChildren?: boolean): void |
| 设置物体是否被显示 |
stopMove(): void other |
| 中断moveTo()、moveBy()的进一步移动 |
stopRotate(): void other |
| 中断从rotateTo()或rotateBy()的进一步旋转 |
stopScale(): void other |
| 中断从ScaleTo()或ScaleBy()的进一步缩放 |
asyncFindGameObjectById(gameObjectId: string): Promise<GameObject> |
| 通过 gameObjectId 异步查找 GameObject |
asyncGetGameObjectByPath(path: string): Promise<GameObject> |
| 通过路径异步查找物体 |
asyncSpawn<T: extends GameObject<T>>(assetId: string, gameObjectInfo?: GameObjectInfo): Promise<T: extends GameObject<T>> |
| 异步构造一个物体 |
bulkPivotTo(gameObjects: GameObject[], transforms: Transform[]): void |
| 批量设置位置 |
findGameObjectById(gameObjectId: string): GameObject |
| 通过 gameObjectId 查找物体 |
findGameObjectByName(name: string): GameObject |
| 通过名字查找物体 |
findGameObjectsByName(name: string): GameObject[] |
| 通过名字查找物体 |
findGameObjectsByTag(tag: string): GameObject[] |
| 通过自定义标签获取物体 |
getGameObjectByPath(path: string): GameObject |
| 通过路径查找物体 |
spawn<T: extends GameObject<T>>(assetId: string, gameObjectInfo?: GameObjectInfo): T: extends GameObject<T> |
| 构造一个物体 |
Properties
onImpulseEnter
• onImpulseEnter: MulticastGameObjectDelegate
发生冲量碰撞后的回调函数
使用示例:创建一个名为"ImpulseOnEnterSample"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,你将在场景中看到一个蹦床的效果,代码如下:ts
@Component
export default class ImpulseOnEnterSample extends Script {
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
impulse.onImpulseEnter.add(()=>{
console.log("Impulse onImpulseEnter")
})
}
}@Component
export default class ImpulseOnEnterSample extends Script {
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
impulse.onImpulseEnter.add(()=>{
console.log("Impulse onImpulseEnter")
})
}
}Accessors
enable
• | • | ||||
|---|---|---|---|---|---|
获取是否启用冲量 Returns
| 设置是否启用冲量,禁用状态下,不会应用冲量到物体上 Parameters
|
ts
@Component
export default class ImpulseSample extends Script {
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 先设置为 false,玩家进入范围后再设置为 true,会有玩家突然凭空被弹开的效果
impulse.enable = true;
}
}@Component
export default class ImpulseSample extends Script {
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 先设置为 false,玩家进入范围后再设置为 true,会有玩家突然凭空被弹开的效果
impulse.enable = true;
}
}impulseForceType
• | • | ||||
|---|---|---|---|---|---|
获取当前冲量力类型 Returns
| 设置冲量力类型。使用示例详见属性 impulseRadialForce 和 impulseVector 中示例代码。 Parameters
|
impulseRadialForce
• | • | ||||
|---|---|---|---|---|---|
获取径向力的冲量值 Returns
| 设置径向力的冲量值 Parameters
|
ts
@Component
export default class ImpulseRadialForceSample extends Script {
// 场景中冲量对象的 id
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 只有为径向力的情况下,impulseRadialForce 属性才有意义
impulse.impulseForceType = ImpulseForceType.RadialForce;
impulse.impulseRadialForce = 500;
}
}@Component
export default class ImpulseRadialForceSample extends Script {
// 场景中冲量对象的 id
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 只有为径向力的情况下,impulseRadialForce 属性才有意义
impulse.impulseForceType = ImpulseForceType.RadialForce;
impulse.impulseRadialForce = 500;
}
}impulseType
• | • | ||||
|---|---|---|---|---|---|
获取冲量应用方式,绝对或相对 Returns
| 设置冲量应用方式 Precautions 在编辑器中使用会自动更新箭头方向 Parameters
|
ts
@Component
export default class ImpulseTypeSample extends Script {
// 场景中冲量对象的 id
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 绝对冲量应用时会先清空物体自身速度,相对冲量会将冲量和物体当前速度叠加
impulse.impulseType = ImpulseType.Absolute;
}
}@Component
export default class ImpulseTypeSample extends Script {
// 场景中冲量对象的 id
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 绝对冲量应用时会先清空物体自身速度,相对冲量会将冲量和物体当前速度叠加
impulse.impulseType = ImpulseType.Absolute;
}
}impulseVector
• | • | ||||
|---|---|---|---|---|---|
获取矢量力的冲量向量 Returns
| 设置矢量力的冲量向量 Parameters
|
ts
@Component
export default class ImpulseVectorSample extends Script {
// 场景中冲量对象的 id
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 只有为矢量力的情况下,impulseVector 属性才有意义
impulse.impulseForceType = ImpulseForceType.VectorForce;
// 设置为自定义的带方向的冲量值
impulse.impulseVector = new Vector(0, 0, 2000);
}
}@Component
export default class ImpulseVectorSample extends Script {
// 场景中冲量对象的 id
impulseId = "1602E908";
// 当脚本被实例后,会在第一帧更新前调用此函数
protected async onStart(): `Promise`<`void`\> {
const impulse = (await GameObject.findGameObjectById(this.impulseId)) as Impulse;
// 只有为矢量力的情况下,impulseVector 属性才有意义
impulse.impulseForceType = ImpulseForceType.VectorForce;
// 设置为自定义的带方向的冲量值
impulse.impulseVector = new Vector(0, 0, 2000);
}
}