gpt4 book ai didi

c# - 向上、向下、向左或向右滑动时,如何调用另一个类中的方法? Unity2D

转载 作者:行者123 更新时间:2023-12-05 06:14:32 24 4
gpt4 key购买 nike

我有一个几天前开始的 Unity 项目。这是一款简单的 2D 俯视射击游戏,旨在在智能手机平台上玩。

我有一个射击脚本,它基本上有一个名为 Shoot1 的方法,它会在子弹中生成。

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

public class Shooting : MonoBehaviour
{
public Transform firePoint;
public GameObject BulletRedPrefab;
public GameObject BulletGreenPrefab;
public float bulletForce = 20f;
// Update is called once per frame
void Start()
{

}

void Update()
{

}

public IEnumerator Shoot1()
{
yield return new WaitForSeconds(0.00001f);
GameObject bullet = Instantiate(BulletRedPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}

我还有 Swipe 脚本,它确定滑动的方向等。

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

public class Swipe : MonoBehaviour
{
private bool tap, swipeUp, swipeDown, swipeLeft, swipeRight;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
// Update is called once per frame
private void Update()
{
tap = swipeUp = swipeDown = swipeLeft = swipeRight = false;
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
isDraging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDraging = false;
Reset();
}
}

if (Input.GetMouseButtonDown(0))
{
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
isDraging = false;
Reset();
}

swipeDelta = Vector2.zero;

if (isDraging)
{
if (Input.touches.Length > 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}

if (swipeDelta.magnitude > 125)
{
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y))
{
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else
{
if (y < 0)
swipeDown = true;
else
swipeUp = true;
}
Reset();
}
}

private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}

public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
}

我有 GestureDetector 脚本,它旨在在用户向左、向右、向上或向下滑动时发射子弹。当我试图通过滑动使播放器对象(称为机器人)移动时,它起作用了。但是当我尝试通过滑动调用另一个类的方法时,它不起作用。

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

public class GestrueDetector : MonoBehaviour
{
public Shooting other;
public Swipe swipeControls;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
private void Update()
{
if (GameObject.Find("robot") != null)
{
if (swipeControls.SwipeLeft)
desirePosition += Vector3.left;
other.Shoot1();

if (swipeControls.SwipeRight)
desirePosition += Vector3.right;
other.Shoot1();

if (swipeControls.SwipeUp)
desirePosition += Vector3.up;
other.Shoot1();

if (swipeControls.SwipeDown)
desirePosition += Vector3.down;
other.Shoot1();
}
}
}

我这周才开始使用 unity,所以我对这个软件还很陌生。非常感谢!

最佳答案

对于协同程序,您需要像这样调用此方法:

StartCoroutine(Shoot1());

我稍微改变了你的类(class):

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

public class Shooting : MonoBehaviour
{
public Transform firePoint;
public GameObject BulletRedPrefab;
public GameObject BulletGreenPrefab;
public float bulletForce = 20f;

void Start ()
{
}

void Update()
{
}

public void Shoot()
{
StartCoroutine(Shoot1());
}

private IEnumerator Shoot1()
{
yield return new WaitForSeconds(0.00001f);
GameObject bullet = Instantiate(BulletRedPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();

rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}

对于 GestrueDetector:

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

public class GestrueDetector : MonoBehaviour
{
public Shooting other;

public Swipe swipeControls;


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

}

// Update is called once per frame
private void Update()
{

if (GameObject.Find("robot") != null)
{

if (swipeControls.SwipeLeft)
desirePosition += Vector3.left;
other.Shoot();

if (swipeControls.SwipeRight)
desirePosition += Vector3.right;
other.Shoot();

if (swipeControls.SwipeUp)
desirePosition += Vector3.up;
other.Shoot();




if (swipeControls.SwipeDown)
desirePosition += Vector3.down;
other.Shoot();





}
}
}

只需阅读更多关于协程在 Unity 中的工作方式 - https://docs.unity3d.com/ScriptReference/Coroutine.html

关于c# - 向上、向下、向左或向右滑动时,如何调用另一个类中的方法? Unity2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62849926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com