【问题标题】:Instantiated Prefab not moving as expected实例化的预制件没有按预期移动
【发布时间】:2020-02-29 08:34:37
【问题描述】:

我在 Unity 中实例化预制件时遇到问题。

我正在开发一款游戏,其中有敌人向你移动,你必须杀死他们。我最初的敌人游戏对象几乎没有问题地向玩家移动,但该对象的实例不会移动。

为了让事情更混乱,当我复制游戏对象并将其添加到场景中而不实例化时,两个游戏对象都会向玩家移动就好了。

敌人脚本:

public class EnemieController : MonoBehaviour
{
    [SerializeField]
    float moveSpeed = 1;

    [SerializeField]
    private Rigidbody2D rb;
    public Transform player;
    public float health = 50;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        Debug.Log(player);
    }

    // Update is called once per frame
    void Update()
    {
        MoveTowardsPlayer();
    }

    void MoveTowardsPlayer()
    {
        Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 direction = (player.position - transform.position).normalized;
        rb.velocity = new Vector2(direction.x * moveSpeed, direction.y * (moveSpeed * 1));

    }
}

实例化代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using System;

[System.Serializable]
public class GameManagement : MonoBehaviour
{
    public GameObject circleEnemie;
    public Transform player;
    [SerializeField]
    float moveSpeed = 1;
    // Start is called before the first frame update
    void Start()
    {
        //Get random position to spawn ball
        Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
        GameObject enemie = Instantiate(circleEnemie, screenPosition, Quaternion.identity) as GameObject;
        enemie.name = "enemiecircle";
        enemie.tag = "Enemie";
    }

    // Update is called once per frame
    void Update()
    {
    }

}

如果需要,这里是敌人检查员规范 Inspector Specifications

抱歉链接,我的声望点还没有达到 10,所以我不能直接发布图片。

【问题讨论】:

  • 您好 Stellan 欢迎来到 Stackoverflow。我的任务是审查新用户的帖子,以帮助引导他们走向成功。这是我的反馈:我最初提出这个问题的想法是“我不可能读到那段文字。”不过我确实读过它,我很难过我不得不读这么多才知道你需要预制件的帮助。我建议删除与你是新人有关的 2 句话。我们可以看到你是新人,不关心通读。最后,让你的问题尽可能简短。具体显示您遇到的问题,然后再详细说明。
  • 人们真的很想分享他们的知识,但是他们非常犹豫是否要花很多时间来确定他们是否可以帮助你。让他们很容易快速确定。同时,我会尽可能地为您编辑此内容。

标签: c# unity3d


【解决方案1】:

我的猜测是你的敌人预制件中引用的Player 本身就是一个永远不会移动的预制件。

您应该将预制字段本身设为EnemyController 类型。这确保您只能在此处引用实际附加EnemyController 的预制件。

那么在实例化之后就可以传入GameManagement脚本的播放器引用了

public class GameManagement : MonoBehaviour
{
    // Give this field the correct type
    public EnemyController circleEnemie;

    public Transform player;
    [SerializeField]
    float moveSpeed = 1;
    // Start is called before the first frame update
    void Start()
    {
        //Get random position to spawn ball
        Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));

        // Instantiate returns the same type as the given prefab
        EnemyController enemy = Instantiate(circleEnemie, screenPosition, Quaternion.identity);
        enemy.name = "enemiecircle";
        enemy.gameObject.tag = "Enemie";

        // Now pass in the player reference
        enemy.player = player;
    }

    // NOTE: When not needed better remove Unity message methods
    // It would just cause overhead
    //void Update()
    //{
    //}
}

旁注:在您的EnemyControllerMoveTowardsPlayer 中您需要这个做什么?

Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

然后每当与Rigidbody 打交道时,请执行FixedUpdate 中的操作,否则可能会破坏物理和碰撞检测!

然后也不要使用 Transform 值,而是再次使用刚体

private void FixedUpdate ()
{
    MoveTowardsPlayer();
}

private void MoveTowardsPlayer ()
{
    var direction = ((Vector2)(player.position - rb.position)). normalized;
    rb.velocity = direction * moveSpeed;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多