在我的2D游戏中,我有一些OnGui元素供用户选择,但是,我使用的光标是另一个ongui元素(使用kinect导航)这是可能的任何机会,目前我正在使用飞机,但我将放大和缩小相机,所以生病基本上需要它们附着在屏幕上。任何想法,建议或解决方法。 这是我目前的光标。
using UnityEngine;
using System;
using System.Collections;
public class PillarAgent : MonoBehaviour {
public SkeletonWrapper sw;
public Vector3 distance;
public float progress =0f;
public Texture2D cursor;
public Texture2D load;
public Camera mainCam;
public float startTime;
private int roundedRestSecounds;
// Use this for initialization
float differencex = 0;
float differencey = 0;
void Start () {
distance =new Vector3(0f,0f,0f);
}
float translate(float value, float leftMin, float leftMax,
float rightMin,float rightMax)
{
float leftSpan = leftMax - leftMin;
float rightSpan= rightMax - rightMin;
float valueScaled = (value-leftMin)/(leftSpan);
return rightMin+(valueScaled * rightSpan);
}
// Update is called once per frame
void Update () {
if (sw.pollSkeleton())
{
distance.x=sw.bonePos[0,0].x - sw.bonePos[0,7].x;//5 is left shoulder
distance.y=sw.bonePos[0,0].y -sw.bonePos[0,7].y;
differencex=translate(distance.x,.6f,0,0,Screen.width);
differencey=translate(distance.y,-.5f,0,0,Screen.height);
//Debug.Log();
float width = sw.bonePos[0,5].x+ sw.bonePos[0,9].x;
float height =sw.bonePos[0,4].y- sw.bonePos[0,0].y;
float heightdiv= (height/2)+sw.bonePos[0,0].y;
}
}
void OnGUI() {
//left top width height
Rect r = new Rect(differencex,differencey,80,50);
GUI.Label(r,cursor);
GUI.BeginGroup(new Rect(differencex,differencey+50,50*Mathf.Clamp01(progress),15));
//Debug.Log(progress);
GUI.DrawTexture(new Rect(0,0,50,50),load);
GUI.EndGroup();
transform.position =mainCam.ScreenToWorldPoint(new Vector3(differencex,Screen.height-differencey,50));
//mainCam.fieldOfView()
}
void OnCollisionStay(Collision Other)
{
startTime+=Time.deltaTime;
if(Other.gameObject.GetComponent(typeof(TextControl)))
{
roundedRestSecounds=Mathf.CeilToInt(Time.time);
progress = Time.time *0.2f;
CurrentState=true;
}
else if(Other.gameObject.tag==("Scalpal")){
progress = startTime *0.5f;
//scallpall activated
//
}
}
void OnCollisionExit(Collision Other){
startTime =0f;
progress =0f;
}
public Boolean CurrentState{get;set;}
}
下一课本质上是我拿起我的工具的类,目前这段代码不起作用(不确定原因),但我想做的是选择一些显示在屏幕上的工具,以便我可以使用他们,例如拿起油漆刷开始画砖或什么不。目前我的工具在飞机上,我希望在相机移动时始终将它们放在屏幕上。
using UnityEngine;
using System.Collections;
public class SelectTool : MonoBehaviour {
public Tools tools;
public float startTime;
public bool ScalpalSelected;
public GameObject selectedTool;
void Start()
{
tools = this.GetComponent<Tools>(); //in order to use this tools muyst be attached to the game object
//this is essentially saying with regards to this game object get the component named tools
}
void update()
{
}
void OnCollisionStay(Collision Other)
{
startTime +=Time.deltaTime;
if(startTime >5f){
if(Other.collider.tag==("Scalpal"))
{
selectedTool = Other.collider.gameObject;
Debug.Log(selectedTool+" What in gods good name is:" +tools.utilities[0]);
}
else {
selectedTool=null;
}
if(selectedTool){
for(int i=0;i<tools.utilities.Length;i++)
{
}
}
ScalpalSelected=true;
renderer.material.color = Color.yellow;
}
}
void OncollisionStay(Collision other){
startTime = 0f;
}
}
答案 0 :(得分:1)
从评论部分到问题,我将假设您想知道如何执行此操作:
“......你希望你的飞机物体与相机一起移动......” - Steven Mills
“谢谢你@StevenMills你有任何关于如何做到这一点的例子吗?” j bel
虽然评论中提供的答案只是手动添加飞机作为摄像机的子项(一种非常简单的手动方法),但我将通过脚本编写另一种方法(在也许这可以帮助别人忽视使用这种解决方案的人的可能性。)
这样做的想法是创建一个脚本(因此在附加到MainCamera
后)将使用方法{{3搜索GameObject
中的所有Object Hierarchy
}}。一旦我们将GameObject
与关联的Tag
放在一起,我们就可以循环遍历数组,并将附加脚本的GameObject
作为其中的一部分。
public class ParentGameObjects : MonoBehaviour {
//The tag to search for on all game objects in the hierarchy
public String objectTag;
//The game objects that we will parent the main camera to
private GameObject[] children;
//It's not necessary to store references to the children but if you want to modify them at some point you will be able to
void Start() {
//Find all game objects with the tag we want
children = GameObject.FindGameObjectsWithTag(objectTag);
//Loop through all of the game objects found and parent this object's transform
for(int i = 0; i < children.Length; i++) {
children[i].transform.parent = transform;
}
}
}
现在,您需要为此脚本执行一些操作:
GameObject
。Inspector
的{{1}}中,输入您要使用的GameObject
的名称。Tag
中应添加为儿童的所有GameObject
(s),请指定相同的Hierarchy
。当然还有其他一些你可以做的事情,例如,而不是只搜索一个Tag
能够搜索多个,但这需要一点(不是很多)更多的工作。尽管如此,我希望这至少对于有关父母如何通过脚本工作的人来说是有用的信息。