简体   繁体   English

Unity 2D撞机卡在墙上?

[英]Unity 2D Colliders Stuck to Walls?

I have just updated Unity to version 5.2.1 from 5.0.0 this morning and I experienced a few problems. 我今天早上刚刚将Unity从5.0.0更新到5.2.1版本,遇到了一些问题。 After Unity translated the game to work for the new version, I tested the game. Unity将游戏翻译成适用于新版本的游戏后,我测试了游戏。 Everything worked fine, except that when I shot a bullet in my game, they would stick to the walls instead of bouncing off of them, but only at certain angles and distances. 一切工作正常,除了当我在游戏中射击子弹时,它们会粘在墙上而不是从墙壁上弹开,但只能在一定角度和距离下进行。 It was like the bullet was so fast that it skipped hitting the collider and got stuck inside of the collider. 好像子弹是如此之快,以至于它跳过了撞机而被卡在撞机内部。 That would make a little bit of sense, but the weird part is that I have a C# script for slow motion in the game. 那会有点道理,但是奇怪的是我有一个C#脚本来播放游戏中的慢动作。 Whenever I turn the slow motion on and then turn it off again, the problem with bullets sticking goes away. 每当我打开慢动作然后再次将其关闭时,子弹卡住的问题就会消失。 I cannot seem to figure out what is causing the problem and it definitely wasn't there before I updated the software. 我似乎无法弄清楚是什么原因引起的,并且在更新软件之前肯定还不存在。 Can anyone help me out with this? 谁能帮我这个忙吗? Thanks. 谢谢。 I'll post the bullet script and the slow motion script below. 我将在下面发布项目符号脚本和慢动作脚本。 The bullet is instantiated inside the player script by the way. 顺便说一下,项目符号是在播放器脚本中实例化的。

Bullet Script: 项目符号脚本:

using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {

public float bulletSpeed;
public float bulletOpacity;
public bool fadeOut;

Animator anim;
Rigidbody2D rigidbod;
SpriteRenderer spriterend;

public GameObject player;
public float aimRotation;

// Use this for initialization
void Start () {

    anim = GetComponent<Animator> ();
    rigidbod = GetComponent<Rigidbody2D> ();
    spriterend = GetComponent<SpriteRenderer> ();

    rigidbod.velocity = transform.right * bulletSpeed;
    rigidbod.gravityScale = 0;

    bulletOpacity = 1;

}

// Update is called once per frame
void Update () {

    Destroy (gameObject, 3f);

    spriterend.color = new Color (1f, 1f, 1f, bulletOpacity);

    if (fadeOut == true)
        bulletOpacity -= 0.03f;

    if (bulletOpacity <= 0)
        Destroy (gameObject);

    aimRotation = player.GetComponent<Player> ().aimRotation;

}

void OnTriggerEnter2D (Collider2D bulletHit) {

    /*if (bulletHit.gameObject.layer == LayerMask.NameToLayer ("vulnerableLayer")) {

    }*/
    rigidbod.gravityScale = 1;
    rigidbod.drag = 1;
    fadeOut = true;

}

}

Slow Motion Script: 慢动作脚本:

using UnityEngine;
using System.Collections;

public class SlowMotion : MonoBehaviour {

public float currentLongevity = 0f;
public float slowAmount = 0.2f;
public float normalTime = 1f;
public float slowLongevity = 0.4f;


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    if (Input.GetMouseButtonDown (1)) {
        Time.fixedDeltaTime = 0.0f * Time.timeScale;
        if (Time.timeScale == normalTime)
            Time.timeScale = slowAmount;
    }

    if (Time.timeScale == slowAmount) {
        currentLongevity += Time.deltaTime;
    }

    if (currentLongevity > slowLongevity) {
        currentLongevity = 0;
        Time.timeScale = normalTime;
    }

}
}

Since you indicated that the 'weird' bullet behavior ceases after the SlowMotion script is run, I would suggest that the issue has to do with setting Time.fixedDeltaTime as you are doing in the Update() method of the SlowMotion script. 由于您指出在运行SlowMotion脚本后“怪异”项目符号行为停止了,因此我建议该问题与在SlowMotion脚本的Update()方法中进行设置Time.fixedDeltaTime有关。 This is also supported by your later comment that the 'weird' behavior no longer appears when you set the rigidBody2D's collision detection to continuous (the bullet's RigidBody2D I assume). 您以后的评论也支持了这一点,即当您将rigidBody2D的碰撞检测设置为连续(我假设的是子弹头的RigidBody2D)时,“怪异”的行为不再出现。

Continuous RididBody2D collision detection enables determining if contact occurred between updates , where as Discrete collision detection registers a collision during a physics update . 连续的RididBody2D碰撞检测可确定更新之间是否发生接触,而离散碰撞检测则在物理更新期间记录了碰撞。 In your SlowMotion script, the following line (in the Update() method) sets fixedDeltaTime to zero : 在SlowMotion脚本中,以下行(在Update()方法中)将fixedDeltaTime设置为零

Time.fixedDeltaTime = 0.0f * Time.timeScale;

Since Time.fixedDeltaTime is the interval in seconds at which physics and other fixed frame rate updates are performed , there must be some minimum value at which the frame rate is realistically run, (0.0 cannot be an actual frame-rate update interval). 由于Time.fixedDeltaTime执行物理和其他固定帧速率更新的时间间隔(以秒为单位) ,因此必须存在一些实际运行帧速率的最小值(0.0不能是实际的帧速率更新间隔)。 When Time.fixedDeltaTime == 0.0, Unity may use a default minimum value or use this to indicate that frame updates run as often as possible, (though I have not tested this). 当Time.fixedDeltaTime == 0.0时,Unity可以使用默认最小值或使用此最小值来指示帧更新尽可能频繁地运行(尽管我尚未对此进行测试)。 Since the Bullet script calls Update() rather than FixedUpdate(), there is no guarantee that the actual interval between the frame updates is uniform . 由于Bullet脚本调用Update()而不是FixedUpdate(),因此不能保证帧更新之间的实际间隔是一致的 I suspect that when you run the SlowMotion script, and set Time.fixedDeltaTime to 0.0, Unity runs with the smallest possible frame update interval and following this, it does render the expected bullet behavior. 我怀疑当您运行SlowMotion脚本并将Time.fixedDeltaTime设置为0.0时,Unity以最小的帧更新间隔运行,并且在此之后,它确实呈现了预期的项目符号行为。

One way to test this would be to set your initial Time.fixedDeltaTime to 0.0 in the editor (and your collision detection behavior back to discrete for the bullet's RididBody2D). 一种测试方法是在编辑器中将您的初始Time.fixedDeltaTime设置为0.0(对于子弹的RididBody2D,您的碰撞检测行为应恢复为离散状态)。

To set Time Manager fixedDeltaTime in the editor: main menu -> Edit -> Project Settings -> Time 要在编辑器中设置时间管理器fixedDeltaTime主菜单->编辑->项目设置->时间

If you find a fixedDeltaTime that works, you can set this in the editor and retrieve the initial value in your SlowMotion script in the Awake() method to initialize the base fixedDeltaTime. 如果找到一个有效的fixedDeltaTime,则可以在编辑器中进行设置,然后在Awake()方法的SlowMotion脚本中检索初始值,以初始化基本的fixedDeltaTime。 I would not recommend using fixedDeltaTime == 0.0 since there is no indication in the documentation of a consistent behavior for a time interval of 0.0 and it may be bound more to the underlying hardware and could result in more unexpected behavior. 我不建议使用fixedDeltaTime == 0.0,因为在文档中没有显示时间间隔为0.0的一致行为,并且它可能更多地绑定到底层硬件,并且可能导致更多意外行为。

Hope this helps, cheers! 希望这会有所帮助,加油!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM