C# 统一保持接触水平移动
我不知道如何开始,我想在按住触摸键时左右移动我的角色 就像在这个游戏中: 我只有检测屏幕左侧或右侧空间的脚本C# 统一保持接触水平移动,c#,unity3d,mobile,touch,C#,Unity3d,Mobile,Touch,我不知道如何开始,我想在按住触摸键时左右移动我的角色 就像在这个游戏中: 我只有检测屏幕左侧或右侧空间的脚本 public float forwardSpeed = 5f; public float sideSpeed = 5f; private void Update() { Vector3 deltaPosition = transform.forward * forwardSpeed; if (Input.touchCount > 0) {
public float forwardSpeed = 5f;
public float sideSpeed = 5f;
private void Update()
{
Vector3 deltaPosition = transform.forward * forwardSpeed;
if (Input.touchCount > 0)
{
Vector3 touchPosition = Input.GetTouch(0).position;
if (touchPosition.x > Screen.width * 0.5f)
deltaPosition += transform.right * sideSpeed;
else
deltaPosition -= transform.right * sideSpeed;
}
transform.position += deltaPosition * Time.deltaTime;
}
这个解决方案对我有效。在一个简单的拦网游戏中,它用来向左或向右移动球拍
void Update () {
if (Input.touchCount > 0){
Touch touch = Input.GetTouch(0);
int direction = (touch.position.x > (Screen.width / 2)) ? 1 : -1;
MovePaddle(direction);
}
}
void MovePaddle(int direction){
float xPos = transform.position.x + (direction * Time.deltaTime * paddleSpeed);
playerPos = new Vector3 (Mathf.Clamp (xPos, -8f, 8f), -9.5f, 0f);
transform.position = playerPos;
}
我有一个不是很顺利的解决方案
public float speed = 5;
public Rigidbody rb;
public void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
//Add touch support
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Touch touch = Input.touches[0];
h = touch.deltaPosition.x;
}
//Move only if we actually pressed something
if (h > 0 || h < 0)
{
Vector3 tempVect = new Vector3(h, 0, 0);
tempVect = tempVect.normalized * speed * Time.deltaTime;
//rb.MovePosition(rb.transform.position + tempVect);
Vector3 newPos = rb.transform.position + tempVect;
checkBoundary(newPos);
}
}
void checkBoundary(Vector3 newPos)
{
//Convert to camera view point
Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos);
//Apply limit
camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f);
camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f);
//Convert to world point then apply result to the target object
Vector3 finalPos = Camera.main.ViewportToWorldPoint(camViewPoint);
rb.MovePosition(finalPos);
}
公共浮动速度=5;
公共刚体;
public void FixedUpdate()
{
float h=输入。GetAxis(“水平”);
//添加触摸支持
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Moved)
{
触摸=输入。触摸[0];
h=触摸。deltaPosition.x;
}
//只有当我们真的按下某个按钮时才移动
如果(h>0 | | h<0)
{
向量3 tempVect=新向量3(h,0,0);
tempVect=tempVect.normalized*速度*时间.deltaTime;
//rb.MovePosition(rb.transform.position+tempVect);
Vector3 newPos=rb.transform.position+tempVect;
检查边界(newPos);
}
}
无效检查边界(矢量3新位置)
{
//转换为摄影机视点
Vector3 camViewPoint=Camera.main.WorldToViewportPoint(newPos);
//应用限制
camViewPoint.x=数学夹具(camViewPoint.x,0.04f,0.96f);
camViewPoint.y=数学夹具(camViewPoint.y,0.07f,0.93f);
//转换为世界点,然后将结果应用于目标对象
Vector3 finalPos=Camera.main.ViewportToWorldPoint(camViewPoint);
rb.移动位置(最终位置);
}
我想你想说的是,只在按下屏幕时移动,而不是?
也许这可以帮助你:
public float forwardSpeed = 5f;
public float sideSpeed = 5f;
private void Update()
{
Vector3 deltaPosition = transform.forward * forwardSpeed;
if (Input.touchCount > 0)
{
Vector3 touchPosition = Input.GetTouch(0).position;
if (touchPosition.x > Screen.width * 0.5f)
deltaPosition += transform.right * sideSpeed;
else
deltaPosition -= transform.right * sideSpeed;
}
else{
deltaPosition = sideSpeed;
}
transform.position += deltaPosition * Time.deltaTime;
}
pd:没有测试,因为还没有谢谢你的回复,但这是我已经拥有的东西。这个脚本从左侧和右侧将sceen减半,但与我发布的示例不同