2011-03-02 191 views
-3

我的游戏是2D RTS,我想知道是否有人知道Unity的优秀教程,或者如果有人熟悉它的语法可以告诉我我可以做错什么。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()移动和什么都没有发生。

回答

2

您不应该对thisIsPlayer使用所有这些检查。你应该为玩家实体和非玩家实体分别分类。

公共变量暴露在编辑器中,并在保存级别时与实体进行序列化。这可能意味着moveSpeed目前没有设置为它在此课程中初始化的内容。

您不应该在Update方法中将力加到刚体上。有一个FixedUpdate方法用于应用物理。这是因为每帧调用Update一次,无论帧速率如何,固定更新仅以特定间隔调用,因此物理力不受帧速率的影响。

此外,您不应该尝试应用强制并设置相同对象的转换位置。奇怪的事情会发生。

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

+0

哦,和Unity论坛(http://forum.unity3d.com/)有一个脚本部分可能会得到比StackOverflow更快的Unity问题响应。 – Calvin 2011-03-02 05:18:41

0

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

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

private bool thisIsPlayer = true; 

与真实的,但我看不到任何条件设置为false(进入AI模式)

只是我的2美分:)