【发布时间】: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);
}
}
}
【问题讨论】: