操纵杆UI不关注播放器/相机
我的屏幕上的操纵杆正好工作在屏幕上...除了它脱离屏幕。我对团结非常陌生,只知道我正在通过Google/YouTube教程做什么,但是Google/YouTube这次无济于事! (可能是由于我使用可怕的术语)。请给我建议!谢谢你!
我尝试的是:
我尝试了所有3种画布渲染模式 - 无济于事。 我小时候尝试了操纵杆,摄影机等 -
这是我问题的GIF: https://gfycat.com/plasticperiodic flyingsquirrel
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Joystick : MonoBehaviour
{
public Transform player;
public float speed = 5.0f;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
public Transform circle;
public Transform outerCircle;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
circle.transform.position = pointA * 1;
outerCircle.transform.position = pointA * 1;
circle.GetComponent<SpriteRenderer>().enabled = true;
outerCircle.GetComponent<SpriteRenderer>().enabled = true;
}
if (Input.GetMouseButton(0))
{
touchStart = true;
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
else
{
touchStart = false;
}
}
private void FixedUpdate()
{
if (touchStart)
{
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
moveCharacter(direction * 1);
circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * 1;
}
else
{
circle.GetComponent<SpriteRenderer>().enabled = false;
outerCircle.GetComponent<SpriteRenderer>().enabled = false;
}
}
void moveCharacter(Vector2 direction)
{
player.Translate(direction * speed * Time.deltaTime);
}
}
I have my onscreen joystick working exactly how I want it... except it goes off screen. I am EXTREMELY new to unity and only know what I am doing through google/YouTube tutorials, but google/YouTube couldn't help me this time! (Likely due to me using terrible terminology). Please give me suggestions! Thank you!
What I have tried:
I have attempted all 3 canvas render modes - to no avail.
I have tried the joystick as a child to the camera, player, etc - no avail
Here is an Gif of my issue:
https://gfycat.com/plasticperiodicflyingsquirrel
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Joystick : MonoBehaviour
{
public Transform player;
public float speed = 5.0f;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
public Transform circle;
public Transform outerCircle;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
circle.transform.position = pointA * 1;
outerCircle.transform.position = pointA * 1;
circle.GetComponent<SpriteRenderer>().enabled = true;
outerCircle.GetComponent<SpriteRenderer>().enabled = true;
}
if (Input.GetMouseButton(0))
{
touchStart = true;
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
else
{
touchStart = false;
}
}
private void FixedUpdate()
{
if (touchStart)
{
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
moveCharacter(direction * 1);
circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * 1;
}
else
{
circle.GetComponent<SpriteRenderer>().enabled = false;
outerCircle.GetComponent<SpriteRenderer>().enabled = false;
}
}
void moveCharacter(Vector2 direction)
{
player.Translate(direction * speed * Time.deltaTime);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论