通知
すべてクリア
29/04/2023 11:12 pm
現在、自分のタンクに向かって敵タンクが攻撃をしてくるようなAIを作成しているのですが、
弾を発射する動作は行うものの、その弾が動いていない(出現後、その場で落ちるだけ)状態になっています。
何度かスクリプトの改修を行ったのですが、一向に改善しないため
どれに問題があるのかご教示いただければ幸いです。
弾を発射する動作は行うものの、その弾が動いていない(出現後、その場で落ちるだけ)状態になっています。
何度かスクリプトの改修を行ったのですが、一向に改善しないため
どれに問題があるのかご教示いただければ幸いです。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnamyTankAI : MonoBehaviour
{
[SerializeField] Transform target;
public GameObject TargetObject;
public Rigidbody m_Shell;
public Transform m_FireTransform;
public float shotSpeed = 10.0f;
private float m_CurrentLaunchForce;
public float m_MinLaunchForce = 15f;
public float m_MaxLaunchForce = 30f;
public float m_MaxChargeTime = 0.75f;
private float time = 1.0f; // 攻撃のインターバル時間
NavMeshAgent NavMeshAgent;
// Start is called before the first frame update
void Start()
{
NavMeshAgent = GetComponent<NavMeshAgent>();
NavMeshAgent.SetDestination(TargetObject.transform.position);
}
// Update is called once per frame
void Update()
{
NavMeshAgent.SetDestination(target.position);
transform.LookAt(TargetObject.transform);
time -= Time.deltaTime;
if (time <= 0)
{
Fired();
time = 1.0f;
}
}
void Fired()
{
Rigidbody shellInstance = Instantiate(m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;
m_Shell.AddForce(transform.forward * shotSpeed);
m_CurrentLaunchForce = m_MinLaunchForce;
}
}
29/04/2023 11:12 pm
Shellの動きは、TankがInstantiateをした直後にShellの速度(velocity)に値を代入することによって速度が発生しています。
代入するスクリプトFired()にある78行目です。
上記のスクリプトだと原本プレハブのm_Shellに力を加えてしまっているので、Instantiate後の"shellInstance"の方に力を加えてください。