-3

我的游戏是 2D RTS,我想知道是否有人知道 Unity 的好教程,或者是否有精通其语法的人可以告诉我我可能做错了什么。

所以,我有我的相机对象和我的播放器对象,都被标记了。玩家对象上只有一个精灵,并设置为刚体。脚本如下:

using UnityEngine;
using System.Collections;

public class AIsciript : MonoBehaviour
{
private bool thisIsPlayer = true;
private GameObject objPlayer;
private GameObject objCamera;

//input variables (variables used to process and handle input)
private Vector3 inputRotation;
private Vector3 inputMovement;

//identity variables (variables specific to the game object)
public float moveSpeed = 100f;

// calculation variables (variables used for calculation)
private Vector3 tempVector;
private Vector3 tempVector2;

// Use this for initialization
void Start()
{
    objPlayer = (GameObject)GameObject.FindWithTag("Player");
    objCamera = (GameObject)GameObject.FindWithTag("MainCamera");
    if (gameObject.tag == "Player")
    {
        thisIsPlayer = true;
    }
}

// Update is called once per frame
void Update()
{
    FindInput();
    ProcessMovement();
    if (thisIsPlayer == true)
    {
        HandleCamera();
    }
}

void FindInput()
{
    if (thisIsPlayer == true)
    {
        FindPlayerInput();
    }
    else
    {
        FindAIInput();
    }
}
void FindPlayerInput()
{
    //find vector to move
    inputMovement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

    //find vector to the mouse
    tempVector2 = new Vector3(Screen.width * 0.5f, 0, Screen.height * 0.5f);

    // the position of the middle of the screen
    tempVector = Input.mousePosition;

    // find the position of the mouse on screen
    tempVector.z = tempVector.y;

    tempVector.y = 0;
    Debug.Log(tempVector);
    inputRotation = tempVector - tempVector2;
}
void FindAIInput()
{

}
void ProcessMovement()
{
    rigidbody.AddForce(inputMovement.normalized * moveSpeed * Time.deltaTime);
    objPlayer.transform.rotation = Quaternion.LookRotation(inputRotation);
    objPlayer.transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 180, 0);
    objPlayer.transform.position = new Vector3(transform.position.x, 0, transform.position.z);
}
void HandleCamera()
{
    objCamera.transform.position = new Vector3(transform.position.x, 15, transform.position.z);
    objCamera.transform.eulerAngles = new Vector3(90, 0, 0);
}
}

我只是想我会发布代码以防万一,但我认为这可能不是问题,因为我试图强迫它进入Start()并且什么也没发生。

4

2 回答 2

2

您不应该对 thisIsPlayer 使用所有这些检查。您应该为玩家实体和非玩家实体设置单独的类。

公共变量在编辑器中公开,并在保存关卡时与实体一起序列化。这可能意味着 moveSpeed 当前未设置为在此类中初始化的值。

您不应该在 Update 方法中对刚体施加力。有一个用于应用物理的 FixedUpdate 方法。这是因为无论帧速率如何,Update 都会每帧调用一次,而 FixedUpdate 仅在特定的时间间隔内调用,因此物理力不受帧速率的影响。

此外,您不应尝试施加力并设置同一对象的变换位置。奇怪的事情会发生。

如果您进入 Unity 资源商店(在 Unity 的“窗口”菜单中可用),则会有一个名为“完整项目”的部分,其中包含一些免费教程。我不记得其中哪些是用 C# 编写的,但即使是 JavaScript 的也会给你一些关于如何构建项目的想法。

于 2011-03-02T05:16:49.887 回答
0

不知道我理解对不对:你的问题是它不是被AI移动的吗?

如果是这样,一个问题可能是你初始化你的

private bool thisIsPlayer = true;

为 true 但我看不到任何将其设置为 false 的条件(进入 AI 模式)

只是我的 2 美分 :)

于 2011-03-03T12:04:06.360 回答