【问题标题】:Creating distance in time between objects unity在对象统一之间创建时间距离
【发布时间】:2016-05-04 13:50:18
【问题描述】:

如何将时间创建为两个对象之间的距离?

我有一个玩家和一个物体,它必须以 1 秒、0.85 秒、0.7 秒等的速度向玩家靠近。但是,我不知道这样做。我有一个移动的 2D 地图,速度为 -4,给人一种向上奔跑的错觉:

图片上的红色三角形是需要向玩家靠近的物体,它的速度也是-4。我使用 Time.deltaTime 创建了这个动作,所以我可以实时工作。根据这个逻辑,我认为我必须将对象 4F 远离玩家以创建 1 秒间隔(desiredSeconds * speed)。

播放器脚本

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

    public GameObject obstacle;
    public GameObject player;

    public float playerDimensionY;

    public bool isRight = true;
    public bool inAir = false;
    public bool mouseClicked = false;

    public int flyingSpeed;

    float timeStamp1;
    float timeStamp2;

    bool runOnce = false;

    // Use this for initialization
    void Start () {
        Vector2 sprite_size = GetComponent<SpriteRenderer> ().sprite.rect.size;
        Vector2 spriteScale = transform.localScale;

        float sizeAndScaleY = sprite_size.y * spriteScale.y;

        float player_local_sprite_sizeY = (sizeAndScaleY / GetComponent<SpriteRenderer> ().sprite.pixelsPerUnit) * 0.5F;

        playerDimensionY = player_local_sprite_sizeY;
    }


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

        if (isRight == true && mouseClicked == true) {
            transform.position += Vector3.right * flyingSpeed * Time.deltaTime;
        } else if (isRight == false && mouseClicked == true) {
            transform.position += Vector3.left * flyingSpeed * Time.deltaTime;
        }

        if (Input.GetMouseButtonDown (0) && inAir == false) {
            mouseClicked = true;
            inAir = true;

            if (isRight == true) {
                isRight = false;
            } else if (isRight == false) {
                isRight = true;
            }
        }

        if (GameObject.FindWithTag ("Obstacle") != null && runOnce == false) {
            Debug.Log (string.Format("Spawn time: " + timeStamp1));
            runOnce = true;
        }

        timeStamp1 = Time.fixedTime;
        timeStamp2 = Time.fixedTime;
    }

    void OnCollisionEnter2D(Collision2D coll) {
        inAir = false;
        mouseClicked = false;
    }

    void OnTriggerEnter2D(Collider2D collTrig) {
        Debug.Log (string.Format ("Trigger time: " + timeStamp2));
    }
}

障碍脚本:

using UnityEngine;
using System.Collections;

public class ObstacleScript : MonoBehaviour {

    public float constantSpeed;
    public float destroyTime;
    public float obstacleDimensionY;

    private float selfDestroyTime;

    // Use this for initialization
    void Awake () {
        selfDestroyTime = Time.time + destroyTime;
    }

    void Start() {
        Vector2 sprite_size = GetComponent<SpriteRenderer> ().sprite.rect.size;
        Vector2 spriteScale = transform.localScale;

        float sizeAndScaleY = sprite_size.y * spriteScale.y;

        float obstacle_local_sprite_sizeY = (sizeAndScaleY / GetComponent<SpriteRenderer> ().sprite.pixelsPerUnit) * 0.5F;

        obstacleDimensionY = obstacle_local_sprite_sizeY;
    }

    // Update is called once per frame
    void Update () {
        transform.position += Vector3.up * constantSpeed * Time.deltaTime;

        if (Time.time > selfDestroyTime) {
            Destroy (gameObject);
        }
    }
}

ObstacleSpawn:

using UnityEngine;
using System.Collections;

public class ObstacleSpawn : MonoBehaviour {

    public PlayerScript pScript;
    public ObstacleScript oScript;

    public GameObject player;
    public GameObject obstacle;

    public float randomSpawnMin;
    public float randomSpawnMax;

    // Use this for initialization
    void Start () {
        InvokeRepeating ("Spawn", 2F, Random.Range (randomSpawnMin, randomSpawnMax));
    }

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

    }

    void Spawn() {
        if (pScript.isRight == true && pScript.inAir == false) {
            obstacle.transform.localScale = new Vector3 (-1, 1, 1);
            Instantiate (obstacle, player.transform.position + new Vector3 (0.05F, 4F, 0), Quaternion.identity);
        } else if (pScript.isRight == false && pScript.inAir == false) {
            obstacle.transform.localScale = new Vector3 (1, 1, 1);
            Instantiate (obstacle, player.transform.position + new Vector3 (-0.05F, 4F, 0), Quaternion.identity);
        }
    }
}

但是,通过在障碍物生成和角色触发之间使用时间戳,我得到了 0.5 间隔而不是 1 的结果。因此,我尝试将其设为 8F 而不是 4F,这应该会导致我的 1 秒间隔,但令人惊讶的是给出了 0.86 的区间。

我完全不知道自己缺少什么,但我觉得我设置 deltaTime 的方式可能存在缺陷。

亲切的问候。

编辑 - 添加代码:

using UnityEngine;
using System.Collections;

public class ObstacleSpawn : MonoBehaviour {

    public PlayerScript pScript;
    public ObstacleScript oScript;

    public GameObject player;
    public GameObject obstacle;

    public float randomSpawnMin;
    public float randomSpawnMax;

    // Use this for initialization
    void Start () {
        InvokeRepeating ("Spawn", 2F, Random.Range (randomSpawnMin, randomSpawnMax));
    }

    // Update is called once per frame
    void Update () { 
        var diff = (player.transform.position - obstacle.transform.position);
        diff = diff.normalized;
        Vector3 speed = new Vector3 (100 * Time.deltaTime, 100 * Time.deltaTime, 100 * Time.deltaTime);
        diff.x *= speed.x;
        diff.y *= speed.y;
        diff.z *= speed.z;
        obstacle.transform.position = obstacle.transform.position + diff;

    }

    void Spawn() {
        if (pScript.isRight == true && pScript.inAir == false) {
            obstacle.transform.localScale = new Vector3 (-1, 1, 1);
            Instantiate (obstacle, obstacle.transform.position, Quaternion.identity);
        } else if (pScript.isRight == false && pScript.inAir == false) {
            obstacle.transform.localScale = new Vector3 (1, 1, 1);
            Instantiate (obstacle, obstacle.transform.position, Quaternion.identity);
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    var diff = ( Player.transform.position - RedTriangle.transform.position    );
    diff = diff.normalized;
    var speed = Vector3(1*Time.deltaTime,1*Time.deltaTime,1*Time.deltaTime); // <- speed
    diff.x *= speed.x;
    diff.y *= speed.y;
    diff.z *= speed.z;    
    RedTriangle.transform.position = RedTriangle.transform.position + diff; 
    

    这样的事情需要在脚本的更新中进行。 (它会将三角形移向玩家)

    【讨论】:

    • 感谢您的回答,能否详细说明一下让我理解?
    • 我需要你详细说明一下——我如何使用这段代码,我应该把它放在哪里?目前我尝试将它放在 Start() 函数的 ObstacleSpawn 脚本中,但出现错误:Assets/Scripts/ObstacleSpawn.cs(21,29): error CS0019: Operator *' cannot be applied to operands of type UnityEngine.Vector3' and `UnityEngine. Vector3'
    • 嗨 WONDEREGG - 代码只是一个例子,我忘记了 Unity 不支持将两个向量相乘。您需要乘以 .x,.y,.z - 代码需要放入更新中。
    • 感谢您的解释-但是,我尝试弄乱代码,无论我给出什么属性的速度,我的两个对象之间的距离总是相同,为 0.2 秒
    • 忘掉我以前的cmets吧,我搞得太多了。我想我只能使用 deltaTime 进行进动,但我仍然只能得到 0.02 的时间间隔,不可更改。建议?
    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多