我正在关注Unity教程(https://www.youtube.com/watch?v=BrlJsdP-VGo)。
我遇到的问题是,每当实例化我的玩家游戏对象的射击时,在开始移动之前会有相当长的延迟(大约1秒)。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Controller : MonoBehaviour
{
public GameObject bolt;
public float fireRate;
private Rigidbody rb;
private int speed = 10;
private float nextFire;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float mh = Input.GetAxis("Horizontal"), mv = Input.GetAxis("Vertical");
Vector3 move = new Vector3(mh, 0f, mv);
rb.velocity = move * speed;
rb.position = new Vector3(Mathf.Clamp(rb.position.x, -6, 6), 0f, Mathf.Clamp(rb.position.z, -4, 8));
rb.rotation = Quaternion.Euler(0, 0, rb.velocity.x * -5);
}
private void Update()
{
if (Input.GetKey("space") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(bolt, new Vector3(transform.position.x, 0,
transform.position.z + 1.25f), transform.rotation);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bolt_Mover : MonoBehaviour
{
Rigidbody rb;
public float speed;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Start()
{
rb.velocity = transform.forward * speed;
}
}