【发布时间】:2019-03-07 21:18:24
【问题描述】:
我在为我的代码实现触摸移动时遇到了一些问题。有人可以写信给我我需要做什么才能让它工作吗?
我想像 Input.GetAxis("Horizontal"); 那样移动
这是我随轴移动的工作代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speedY = 5f, speedX = 3f, boundX = 3f;
void Start()
{
}
// Update is called once per frame
void Update()
{
float input = Input.GetAxis("Horizontal");
print(input);
}
}
void Move()
{
Vector2 temp = transform.position;
temp.y += speedY * Time.smoothDeltaTime;
temp.x += speedX * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
transform.position = temp;
}
}
这是我的解决方案,我认为它会起作用,但它不起作用......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speedY = 5f, speedX = 3f, boundX = 3f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float input = Input.GetAxis("Horizontal");
print(input);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.position.x > (Screen.width / 2))
{
Move();
Debug.Log("Go right");
}
if (touch.position.x < (Screen.width / 2))
{
Debug.Log("justleft");
}
}
}
}
void Move()
{
Vector2 temp = transform.position;
temp.y += speedY * Time.smoothDeltaTime;
temp.x += speedX * Time.smoothDeltaTime * Input.touchCount;
transform.position = temp;
}
}
当我点击最后一段代码之类的代码时,我看不到调试。
有人可以给我写解决方案吗?或者帮我一些小费。
非常感谢
【问题讨论】:
-
你的第一个代码不会工作,因为你从不调用 move 并且你有一个额外的 { in update 这是一个语法错误。同样在第二个代码中,为什么你有
GetAxis你已经使用了触摸。此外,您的movemethod 也没有任何意义。你想向右走还是想向上向右走?因为这就是该方法目前所做的。 -
我想左右走,我不想上去,因为它总会上去。我希望你能把你的答案作为答案。