尝试向游戏对象发射射弹,不会移动!=
Trying to launch a projectile towards a gameobject, doesn't move!=
我正在制作一款 2D 塔防游戏,希望我的塔能在小兵面前发射预制件。然而,它目前只生成我想要的预制件,但不会移动它。
我的两个脚本:
public class Attacker : MonoBehaviour {
// Public variables
public GameObject ammoPrefab;
public float reloadTime;
public float projectileSpeed;
// Private variables
private Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider co){
if (co.gameObject.tag == "Enemy" || co.gameObject.tag == "BlockTower") {
Debug.Log("Enemy tag detected");
if(this.gameObject.tag == "Enemy" && co.gameObject.tag != "Enemy"){
Debug.Log("This is an Enemy");
// Insert for Enemey to attack Block Towers.
}
if(this.gameObject.tag == "Tower" && co.gameObject.tag != "BlockTower"){
Debug.Log("This is a Tower");
Tower Tower = GetComponent<Tower>();
Tower.CalculateCombatTime(reloadTime, projectileSpeed);
Transform SendThis = co.transform;
Tower.SetTarget(SendThis);
}
}
}
}
和
public class Tower : MonoBehaviour {
private Transform target;
private float fireSpeed;
private double nextFireTime;
private GameObject bullet;
private Attacker source;
// Use this for initialization
public virtual void Start () {
source = this.GetComponent<Attacker> ();
}
// Update is called once per frame
public virtual void Update () {
if (target) {
Debug.Log("I have a target");
//if(nextFireTime <= Time.deltaTime)
FireProjectile ();
}
}
public void CalculateCombatTime(float time, float speed){
Debug.Log("Calculate Combat Speed");
nextFireTime = Time.time + (time * .5);
fireSpeed = speed;
}
public void SetTarget(Transform position){
Debug.Log("Set Target");
target = position;
}
public void FireProjectile(){
Debug.Log("Shoot Projectile");
bullet = (GameObject)Instantiate (source.ammoPrefab, transform.position, source.ammoPrefab.transform.rotation);
float speed = fireSpeed * Time.deltaTime;
bullet.transform.position = Vector3.MoveTowards (bullet.transform.position, target.position, speed);
}
}
基本上Attacker检测到与它碰撞的物体,然后如果它的标签是Tower,它就会将信息发送给Tower。我的调试显示每个函数都有效,甚至 "Debug.Log("Shoot Projectile");"
也出现了。
但是它没有朝我的目标移动所以我猜 "bullet.transform.position = Vector3.MoveTowards (bullet.transform.position, target.position, step);"
永远不会被执行?
您必须更新项目符号的位置。你只有在创建子弹时才会移动。
尝试做一个子弹列表,使用update函数改变位置
Vector3.MoveTowards
只移动一次对象,调用FireProjectile
时只是瞬间位移
您需要创建某种带有 Update()
函数的射弹脚本,使其随时间移动。
这是一个例子:
public class Projectile : MonoBehaviour
{
public Vector3 TargetPosition;
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, TargetPosition, speed * Time.DeltaTime);
}
}
然后在子弹实例化后立即设置目标:
bullet.GetComponent<Projectile>().TargetPosition = target.position;
希望对您有所帮助。
我正在制作一款 2D 塔防游戏,希望我的塔能在小兵面前发射预制件。然而,它目前只生成我想要的预制件,但不会移动它。
我的两个脚本:
public class Attacker : MonoBehaviour {
// Public variables
public GameObject ammoPrefab;
public float reloadTime;
public float projectileSpeed;
// Private variables
private Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider co){
if (co.gameObject.tag == "Enemy" || co.gameObject.tag == "BlockTower") {
Debug.Log("Enemy tag detected");
if(this.gameObject.tag == "Enemy" && co.gameObject.tag != "Enemy"){
Debug.Log("This is an Enemy");
// Insert for Enemey to attack Block Towers.
}
if(this.gameObject.tag == "Tower" && co.gameObject.tag != "BlockTower"){
Debug.Log("This is a Tower");
Tower Tower = GetComponent<Tower>();
Tower.CalculateCombatTime(reloadTime, projectileSpeed);
Transform SendThis = co.transform;
Tower.SetTarget(SendThis);
}
}
}
}
和
public class Tower : MonoBehaviour {
private Transform target;
private float fireSpeed;
private double nextFireTime;
private GameObject bullet;
private Attacker source;
// Use this for initialization
public virtual void Start () {
source = this.GetComponent<Attacker> ();
}
// Update is called once per frame
public virtual void Update () {
if (target) {
Debug.Log("I have a target");
//if(nextFireTime <= Time.deltaTime)
FireProjectile ();
}
}
public void CalculateCombatTime(float time, float speed){
Debug.Log("Calculate Combat Speed");
nextFireTime = Time.time + (time * .5);
fireSpeed = speed;
}
public void SetTarget(Transform position){
Debug.Log("Set Target");
target = position;
}
public void FireProjectile(){
Debug.Log("Shoot Projectile");
bullet = (GameObject)Instantiate (source.ammoPrefab, transform.position, source.ammoPrefab.transform.rotation);
float speed = fireSpeed * Time.deltaTime;
bullet.transform.position = Vector3.MoveTowards (bullet.transform.position, target.position, speed);
}
}
基本上Attacker检测到与它碰撞的物体,然后如果它的标签是Tower,它就会将信息发送给Tower。我的调试显示每个函数都有效,甚至 "Debug.Log("Shoot Projectile");"
也出现了。
但是它没有朝我的目标移动所以我猜 "bullet.transform.position = Vector3.MoveTowards (bullet.transform.position, target.position, step);"
永远不会被执行?
您必须更新项目符号的位置。你只有在创建子弹时才会移动。
尝试做一个子弹列表,使用update函数改变位置
Vector3.MoveTowards
只移动一次对象,调用FireProjectile
时只是瞬间位移
您需要创建某种带有 Update()
函数的射弹脚本,使其随时间移动。
这是一个例子:
public class Projectile : MonoBehaviour
{
public Vector3 TargetPosition;
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, TargetPosition, speed * Time.DeltaTime);
}
}
然后在子弹实例化后立即设置目标:
bullet.GetComponent<Projectile>().TargetPosition = target.position;
希望对您有所帮助。