统一按住触摸水平移动

时间:2018-08-13 23:03:35

标签: c# unity3d mobile touch

我不知道该如何开始,我想在保持触摸时左右移动角色。

喜欢这个游戏:

Example Game - Stairs from Ketchapp

我只有我的脚本可以检测屏幕的左右空间。

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;
}

3 个答案:

答案 0 :(得分:1)

此解决方案对我有用。它用于简单的断块游戏中以向左或向右移动球拍。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:annotation-config  />
<!--    <context:component-scan base-package="com.app.controller, com.app.security" /> -->
<context:component-scan base-package="com.app.controller" />
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <mvc:annotation-driven />
....
<bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.app.model.Student</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
...

答案 1 :(得分:0)

我有一个不太顺利的解决方案

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);
}

答案 2 :(得分: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;
    }
    else{
            deltaPosition = sideSpeed;
    }
    transform.position += deltaPosition * Time.deltaTime;
}

pd:尚未测试,因为