Unity3d unity |通过触摸动态创建和销毁Jostick
我想制作一个在触摸位置(Input.getTouch(I.position)创建(Instatiated或其他)并在用户不再触摸屏幕时销毁的jostick。这是下面的源代码。请告诉我怎么做。多谢各位Unity3d unity |通过触摸动态创建和销毁Jostick,unity3d,unityscript,Unity3d,Unityscript,我想制作一个在触摸位置(Input.getTouch(I.position)创建(Instatiated或其他)并在用户不再触摸屏幕时销毁的jostick。这是下面的源代码。请告诉我怎么做。多谢各位 using UnityEngine; using System.Collections; public class joy : MonoBehaviour { public GameObject joystick; //Self explanitory public GameObject bg
using UnityEngine;
using System.Collections;
public class joy : MonoBehaviour
{
public GameObject joystick; //Self explanitory
public GameObject bg; //Background object for the joystick boundaries
public const float deadzone = 0f; //Deadzone for the joystick
public const float radius = 2.0f; //The radius that the joystick thumb is allowed to move from its origin
public Camera cam;
protected Collider joyCol;
protected int lastfingerid = -1; //Used for latching onto the finger
public Vector2 position = Vector2.zero;
void Awake ()
{
joyCol = bg.GetComponent<Collider>();
}
public void Disable ()
{
gameObject.active = false; //Turns off the joystick when called
}
public void ResetJoystick () //Called when the joystick should be moved back to it's original position (relative to parent object)
{
lastfingerid = -1;
joystick.transform.localPosition = Vector3.zero;
}
void Update ()
{
if (Input.touchCount == 0)
{
ResetJoystick (); //I would have figured this would be obvious? :P
}
else
{
for (int i=0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
bool latchFinger = false;
if (touch.phase == TouchPhase.Began) //When you begin the touch, the code here gets called once
{
//Vector3 touchpos = Input.GetTouch(i).position;
//touchpos.z = 0.0f;
//Vector3 createpos = cam.ScreenToWorldPoint(touchpos);
Ray ray = cam.ScreenPointToRay (touch.position); //Creates a ray at the touch spot
RaycastHit hit; //Used for determining if something was hit
//joystick = (GameObject)Instantiate(Resources.Load("Type3/joystick"),createpos, Quaternion.identity);
//bg = (GameObject)Instantiate(Resources.Load("Type3/Bg"),createpos, Quaternion.identity);
if (joyCol.Raycast(ray,out hit,Mathf.Infinity)) //The ray hit something...
{
if (hit.collider == joyCol) //Apparently, that ray hit your joystick
{
latchFinger = true; //This turns on and sets off a chain reaction
}
}
}
if (latchFinger ) //Latch finger being true turns this code on
{
if ((lastfingerid == -1 || lastfingerid != touch.fingerId))
{
lastfingerid = touch.fingerId;
}
}
if (lastfingerid == touch.fingerId) //The real meat of the code
{
Vector3 sTW = cam.ScreenToWorldPoint (new Vector3 (touch.position.x, touch.position.y, 1)); //Transforms the screen touch coordinates into world coordinates to move our joystick
joystick.transform.position = sTW; //Hurr :O
joystick.transform.localPosition = new Vector3(joystick.transform.localPosition.x,joystick.transform.localPosition.y,0);
//float xClamp = Mathf.Clamp (joystick.transform.localPosition.x, xClampMin, xClampMax); //Limit movement to the boundaries
//float yClamp = Mathf.Clamp (joystick.transform.localPosition.y, yClampMin, yClampMax);
/*float distFromCenter = Mathf.Sqrt(joystick.transform.localPosition.x*joystick.transform.localPosition.x + joystick.transform.localPosition.y*joystick.transform.transform.localPosition.y);
float actualPercent = Mathf.Clamp01(distFromCenter/radius);
float percent = Mathf.Clamp01((distFromCenter - deadzone)/(radius - deadzone));
*/
joystick.transform.localPosition = Vector3.ClampMagnitude(joystick.transform.localPosition,radius);
//joystick.transform.localPosition = new Vector3 (xClamp, yClamp, joystick.transform.localPosition.z); //Finally, the joystick moves like it should with everything in place
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
ResetJoystick ();
}
}
Ray touchray = cam.ScreenPointToRay (touch.position); //Creates a ray at the touch spot
//RaycastHit hit; //Used for determining if something was hit
Debug.DrawRay (touchray.origin, touchray.direction * 100, Color.yellow); //Delete this line. It's not important.
}
}
//set our position variables x and y values to the joysticks values but clamped to a percent value instead of world coords
position.x = joystick.transform.localPosition.x/radius;
position.y = joystick.transform.localPosition.y/radius;
if(position.magnitude < deadzone)
{
position.x = 0;
position.y = 0;
}
}
}
使用UnityEngine;
使用系统集合;
公共阶层的快乐:单一行为
{
公共游戏对象操纵杆;//自我解释
公共游戏对象bg;//操纵杆边界的背景对象
public const float deadzone=0f;//操纵杆的死区
public const float radius=2.0f;//允许操纵手柄拇指从原点移动的半径
公共摄像机;
保护对撞机;
受保护的int-lastfingerid=-1;//用于锁定到手指上
公共向量2位置=向量2.0;
无效唤醒()
{
joyCol=bg.GetComponent();
}
公共无效禁用()
{
gameObject.active=false;//调用时关闭操纵杆
}
public void resetmogage()//当操纵杆应移回其原始位置(相对于父对象)时调用
{
lastfingerid=-1;
操纵杆.transform.localPosition=Vector3.0;
}
无效更新()
{
如果(Input.touchCount==0)
{
重置操纵杆();//我本以为这是显而易见的?:P
}
其他的
{
对于(int i=0;i
到底是什么问题?到目前为止,你尝试了什么?@golergka我制作了(操纵杆和背景)预设,并在触摸位置实例化。这是个非常糟糕的主意。我将尝试创建一个游戏对象,它将实例化预置,并使其在脚本中启用或禁用触摸位置。为什么这是一个坏主意,您遇到了什么确切的问题?我还是不明白这个问题是关于什么的。