Unity3d 如何在Unity中创建投射脚本

Unity3d 如何在Unity中创建投射脚本,unity3d,Unity3d,所以最近我和我的朋友们正在尝试做一个有趣的游戏。目前,我碰到了一堵墙,不知道该怎么做。我正在尝试创建一个简单的基本脚本,在这里我可以通过右键单击来移动和攻击角色。如果它击中地面,它将移动到那里,如果在目标的射程内,它将发射一枚炮弹。因此,游戏正在创建投射物,但它实际上并没有移动。谁能告诉我该怎么办。起初,我以为只制作一个脚本,但现在我认为最好是为射弹制作另一个脚本 void Update() { if (Input.GetMouseButtonDown(1)) {

所以最近我和我的朋友们正在尝试做一个有趣的游戏。目前,我碰到了一堵墙,不知道该怎么做。我正在尝试创建一个简单的基本脚本,在这里我可以通过右键单击来移动和攻击角色。如果它击中地面,它将移动到那里,如果在目标的射程内,它将发射一枚炮弹。因此,游戏正在创建投射物,但它实际上并没有移动。谁能告诉我该怎么办。起初,我以为只制作一个脚本,但现在我认为最好是为射弹制作另一个脚本

void Update()
{
    if (Input.GetMouseButtonDown(1))
    {
        RaycastHit hit;

        if(Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit) && hit.transform.tag == "Minion")
        {
            if(Vector3.Distance(this.transform.position, hit.point) <= atkRange)
            {
                GameObject proj = Instantiate(bullet) as GameObject;
                proj.transform.position = Vector3.MoveTowards(this.transform.position, target, atkSpd);                                  
            }                             
        }
        else if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, speed))
        {
            agent.destination = hit.point;
        }
    }
}
void Update()
{
if(Input.GetMouseButtonDown(1))
{
雷卡斯特击中;
if(Physics.Raycast(Camera.main.screenpointoray(Input.mousePosition)、out-hit)和&hit.transform.tag==“Minion”)
{

如果(Vector3.Distance(this.transform.position,hit.point)不要像当前那样设置位置

proj.transform.position = Vector3.MoveTowards(this.transform.position, target, atkSpd);

相反,添加characterController或刚体并使用rb.addVelocity。

不要像当前那样设置位置

proj.transform.position = Vector3.MoveTowards(this.transform.position, target, atkSpd);

相反,添加characterController或rigidbody并使用rb.addVelocity。

对于初学者,我建议使用
rigidbody
组件并让物理处理移动,但如果您想使用
Vector3.MoveToward
,这将是一项小工作:

Vector3.MoveToward
是每个帧都需要调用的东西。我猜
bullet
是您的预置,因此您需要为移动创建一个新脚本并将其放置在该预置上:

public class MoveToTarget : MonoBehaviour
{
    private float _speed;
    private Vector3 _target;

    public void StartMovingTowards(Vector3 target, float speed)
    {
        _target = target;
        _speed = speed;
    }

    public void FixedUpdate()
    {
        // Speed will be 0 before StartMovingTowards is called so this will do nothing
        transform.position = Vector3.MoveTowards(transform.postion, _target, _speed);
    }
}
将其附加到预置之后,请确保在实例化预置副本时获取引用并开始:

GameObject proj = Instantiate(bullet) as GameObject;
var movement = proj.GetComponent<MoveToTarget>();
movement.StartMovingTowards(target, atkSpd);
然后你可以施加一个力,让物理来接管:

body.AddForce(target - transform.position, ForceMode.Impulse);

对于初学者,我建议使用
刚体
组件,让物理处理运动,但如果您想使用
矢量3.MoveToward
,则需要做一些工作:

Vector3.MoveToward
是每个帧都需要调用的东西。我猜
bullet
是您的预置,因此您需要为移动创建一个新脚本并将其放置在该预置上:

public class MoveToTarget : MonoBehaviour
{
    private float _speed;
    private Vector3 _target;

    public void StartMovingTowards(Vector3 target, float speed)
    {
        _target = target;
        _speed = speed;
    }

    public void FixedUpdate()
    {
        // Speed will be 0 before StartMovingTowards is called so this will do nothing
        transform.position = Vector3.MoveTowards(transform.postion, _target, _speed);
    }
}
将其附加到预置之后,请确保在实例化预置副本时获取引用并开始:

GameObject proj = Instantiate(bullet) as GameObject;
var movement = proj.GetComponent<MoveToTarget>();
movement.StartMovingTowards(target, atkSpd);
然后你可以施加一个力,让物理来接管:

body.AddForce(target - transform.position, ForceMode.Impulse);

嗯,好的。我会尝试两种方法,谢谢你的快速回答!嗯,好的。我会尝试两种方法,谢谢你的快速回答!