java 锚,spring 接头与相对连接的锚

我最终采取了略微不同的方法 . 我没有更改 spring 的连接锚,而是将其保持不变,并将连接的主体设置为静态定位的原点变换 .

按下键时,我暂时将 spring 设置为0,然后更改柱塞对象的位置 . 未按下按键时,我将 spring 设置为较高值,以便弹回到定位点 .

为了处理约束问题,我使用了一个脚本,它将旋转和x / y位置设置为每帧的原始值,而不是使用刚体检查器上的选项(仅锁定世界空间轴上的位置) .

脚本现在看起来像这样:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlungerController : MonoBehaviour {

public float movement_increment_z;

public float max_movement_z;

public float spring_power;

private SpringJoint spring;

private Vector3 orig_anchor;

private Quaternion orig_rotation;

private float orig_pos_x, orig_pos_y;

private void Start() {

spring = GetComponent();

if (!spring) { throw new System.Exception("spring joint is needed"); };

orig_anchor = spring.connectedAnchor;

orig_rotation = transform.localRotation;

orig_pos_y = transform.localPosition.y;

orig_pos_x = transform.localPosition.x;

}

public void processPlungerInput (bool isKeyPressed) {

if (isKeyPressed) {

spring.spring = 0;

if (transform.localPosition.z < max_movement_z) {

transform.localPosition += new Vector3(0,0,movement_increment_z);

}

} else {

spring.spring = spring_power;

}

}

// preserve only z axis movement and no rotation at all.

private void FixedUpdate() {

transform.localRotation = orig_rotation;

transform.localPosition = new Vector3(orig_pos_x, orig_pos_y, transform.localPosition.z);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值