如何将时间创建为两个对象之间的距离?
我有一个玩家和一个物体必须以1秒,0.85秒,0.7秒的速度向玩家走来,依此类推。但是,我不知道这样做。我有一个移动的2D地图,速度为-4,给人一种向上跑的幻觉:
图片上的红色三角形是需要朝向玩家的对象,它的速度也是-4。我使用Time.deltaTime创建了这个动作,所以我会实时工作。有了这个逻辑,我相信我必须将对象4F远离玩家,以创建一个1秒的间隔(desiredSeconds * speed)。
PlayerScript
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));
}
}
ObstacleScript:
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);
}
}
}
答案 0 :(得分:2)
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;
这样的事情需要更新脚本。 (它会将三角形移向玩家)