Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一游戏引擎教程?_C#_Unity3d - Fatal编程技术网

C# 统一游戏引擎教程?

C# 统一游戏引擎教程?,c#,unity3d,C#,Unity3d,我的游戏是2D RTS,我想知道是否有人知道Unity的好教程,或者是否有精通该教程语法的人可以告诉我我可能做错了什么 所以,我有我的相机对象和我的播放器对象,都被标记了。玩家对象上只有一个精灵,并设置为刚体。脚本如下所示: using UnityEngine; using System.Collections; public class AIsciript : MonoBehaviour { private bool thisIsPlayer = true; private GameObje

我的游戏是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()
,但什么都没有发生。

你不应该对这个isplayer使用所有这些检查。玩家实体和非玩家实体应该有单独的类

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

在更新方法中,不应向刚体添加力。有一种固定更新的方法用于应用物理。这是因为无论帧速率如何,每帧都会调用一次更新,并且只在特定的时间间隔调用FixedUpdate,因此物理力不受帧速率的影响

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


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

我不知道我是否理解正确:你的问题是它没有被AI移动

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

private bool thisIsPlayer = true;
使用true,但我看不到任何条件将其设置为false(进入ai模式)

只有我的2美分:)

哦,Unity论坛()有一个脚本部分,可以比StackOverflow更快地回答Unity问题。