【问题标题】:Unity Game Engine Tutorial?Unity 游戏引擎教程?
【发布时间】:2011-07-07 00:14:16
【问题描述】:

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

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 哦,Unity 论坛 (forum.unity3d.com) 有一个脚本部分,它可能会比 StackOverflow 更快地响应 Unity 问题。
    【解决方案2】:

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

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

    private bool thisIsPlayer = true;
    

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

    只是我的 2 美分 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多