我在Unity制作射击游戏。我想延迟拍摄脚本,但现在我的代码似乎没有工作。我可以解决这个问题的任何线索?除延迟功能外,一切正常。
以下是代码:
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}
答案 0 :(得分:1)
您需要在定时器条件之外获取移动代码(W,S,A,D) -
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
}
/********* Move this outside the timer condition *********/
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}
这将在玩家开枪后等待期间(重新加载时间)保持移动。
答案 1 :(得分:0)
我认为你的想法并不那么明确。最简单的方法就是。
function Shoot () {
...
reloadTimer = Time.time + reloadtime;
}
如果应该看起来像这样
if (reloadTimer >= Time.time){
if(Input.GetKey("space")) {
Shoot();
}
}