【发布时间】: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 句话。我们可以看到你是新人,不关心通读。最后,让你的问题尽可能简短。具体显示您遇到的问题,然后再详细说明。
-
人们真的很想分享他们的知识,但是他们非常犹豫是否要花很多时间来确定他们是否可以帮助你。让他们很容易快速确定。同时,我会尽可能地为您编辑此内容。