繁体   English   中英

如何让玩家在向左滑动时彼此靠近,向右滑动时他们移动到两侧?

[英]How can I make players get close to each other on swipe left, and on swipe right they move to the sides?

所以我想让我的球员在任何时候都有前进的速度,当我向左滑动或使用 A 键时,他们靠着绳索靠近彼此,当我向右滑动时,他们远离彼此到同一个位置他们以前是。

视频游戏链接: https : //streamable.com/45ahyc

我现在的运动代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{


    public float movementSpeed = 10f;



    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float hMovement = Input.GetAxis("Horizontal") * movementSpeed;
        float vMovement = Input.GetAxis("Vertical") * movementSpeed / 2;

        transform.Translate(new Vector3(hMovement, 0, vMovement) * Time.deltaTime);
    }
}

您可以为此使用标志标志。 例如,您可以将ìsRight布尔公共属性添加到您的PlayerController ,当您读取水平移动时,您可以乘以 -1 或 1。

float hMovement = Input.GetAxis("Horizontal") * movementSpeed * (isRight)?1:-1;

因此,当您向左滑动时,您的右侧角色可以向左移动,左侧角色将向右移动。 因为,他们会关闭中心。

此外,您可以在此作业中使用角色的 x 轴符号。

float hMovement = Input.GetAxis("Horizontal") * movementSpeed * Mathf.Sign(transform.position.x);

如果您在右侧的字符可能它的 x 位置是 + 而左侧字符的位置符号是 -。 您可以获得与上述代码相同的结果。

注意在我的第二个建议中,我假设您使用了 2 个玩家角色并且屏幕中心为 0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM