[英]Unity3d how to reference swipe control from another script?
I have tried every which way possible to get this to work with no success. 我已尽一切可能使此方法无法成功工作。 I have a swipe script and a game/control script.
我有一个滑动脚本和一个游戏/控制脚本。 I am trying to reference the swipe control in my control script as the means to control my character.
我试图在我的控制脚本中引用滑动控件作为控制角色的方法。 So far I am getting only one error from within Unity.
到目前为止,我在Unity内部仅收到一个错误。
Assets/RuinRunStarterKit/Scripts/csTempleRun.cs(307,25): error CS1501: No overload for method UpdatePlayer' takes
0' arguments 资产/ RuinRunStarterKit /脚本/csTempleRun.cs(307,25):错误CS1501:方法
UpdatePlayer' takes
重载没有UpdatePlayer' takes
0'参数
I've tried adding TouchDetector.enTouchType within the parenthesis of UpdatePlayer but that gives me three errors rather than just one. 我尝试在UpdatePlayer的括号内添加TouchDetector.enTouchType,但这给了我三个错误,而不仅仅是一个错误。 I've contacted numerous people and visited numerous sites looking for answers but have failed to yield a working result.
我已经联系了许多人,并访问了许多站点以寻找答案,但未能获得有效的结果。 I am not educated in programming.
我没有编程知识。 I have only researched online to figure out how to do something as far as programming goes.
我只是在网上进行研究,以弄清楚如何才能进行编程。 The second script I have here is from an Asset I purchased on the Unity Asset Store.
我这里的第二个脚本来自我在Unity Asset Store上购买的资产。 I have contacted them for support for adding swipe controls and they emailed me the touch script but failed to tell me how to implement it.
我已与他们联系以获取添加滑动控件的支持,他们通过电子邮件向我发送了触摸脚本,但未能告诉我如何实现。 I just want to play this game on my phone with swipe as the controls.
我只想在手机上玩游戏,只需滑动即可。 This is for personal use.
这是供个人使用。 I would really really appreciate it if someone could help me achieve the desired results.
如果有人可以帮助我达到预期的效果,我将非常感激。 Again, I want to be able to control the character using swipe.
同样,我希望能够使用滑动来控制角色。 Thank you.
谢谢。 Here are my scripts:
这是我的脚本:
TouchDetector.cs; TouchDetector.cs;
using UnityEngine;
using System.Collections;
public class TouchDetector : MonoBehaviour {
public delegate void deTouchEvent
(enTouchType touchType);
public static event
deTouchEvent
evTouchEvent;
public enum enTouchType
{
SwipeLeft,
SwipeRight,
SwipeDown,
SwipeUp,
}
void Start ()
{
}
void Update ()
{
if (evTouchEvent == null)
return;
if (Input.GetKeyDown(KeyCode.UpArrow )) evTouchEvent(enTouchType.SwipeUp );
if (Input.GetKeyDown(KeyCode.DownArrow )) evTouchEvent(enTouchType.SwipeDown );
if (Input.GetKeyDown(KeyCode.LeftArrow )) evTouchEvent(enTouchType.SwipeLeft );
if (Input.GetKeyDown(KeyCode.RightArrow)) evTouchEvent(enTouchType.SwipeRight);
if (Input.touchCount > 0)
{
foreach (Touch t in Input.touches)
{
Vector3 swipe = t.deltaPosition * t.deltaTime;
if (swipe.y > 0.5f) evTouchEvent(enTouchType.SwipeUp );
if (swipe.y < -0.5f) evTouchEvent(enTouchType.SwipeDown );
if (swipe.x > 0.5f) evTouchEvent(enTouchType.SwipeRight);
if (swipe.x < -0.5f) evTouchEvent(enTouchType.SwipeLeft );
}
}
}
}
csTempleRun.cs; csTempleRun.cs;
void Update ()
{
UpdatePlayer();
if (m_player != null)
{
// Make the camera follow the player (if active)
SmoothFollow sF = (SmoothFollow)Camera.mainCamera.GetComponent(typeof(SmoothFollow));
sF.target = m_player.transform;
// Check for collisions with interactive objects
UpdateCoins();
UpdateMines();
// Dynamically update the track
CreateNewCellsIfNeeded(false);
}
}
private void UpdatePlayer(TouchDetector.enTouchType T)
{
// if the player is dead (replaced with ragdoll) then exit since none of this code should fire.
if (m_player == null)
{
return;
}
// Gradually increase the players' running speed, and update the animation to match.
m_playerRunSpeed += Time.deltaTime * 0.005f;
m_playerRunSpeed = Mathf.Clamp(m_playerRunSpeed, 0.5f, 3.0f);
m_player.animation["run"].speed = m_playerRunSpeed * 2.0f;
// ****************************************************************************************
// INPUT
// Player can only turn if they are not already sliding / jumping.
// Equally, sliding / jumping are mutually exclusive.
if (Input.GetKeyDown (KeyCode.LeftArrow)&& m_playerJump <= -1.0f && m_playerSlide <=0.0f)
{
if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.West;
if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.North;
if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.East;
if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.South;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
{
if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.East;
if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.South;
if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.West;
if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.North;
}
if (T==TouchDetector.enTouchType.SwipeDown && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
{
m_playerSlide = 1.0f;
m_player.animation.Play("slide_fake");
}
if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
{
AudioSource.PlayClipAtPoint(m_jumpAudio, m_player.transform.position);
m_playerJump = 1.0f;
m_playerYvel = 0.0f;
m_player.animation.Play("jump");
}
I hope I'm not doing anything wrong by posting this script but I feel I need to post this in order to get the help I need. 我希望通过发布此脚本没有做任何错误,但是我觉得需要发布此脚本才能获得所需的帮助。 You'll notice in the csTempleRun.cs script that I replaced one of the KeyCode calls with TouchDetector.enTouchType.SwipeDown.
您会注意到,在csTempleRun.cs脚本中,我用TouchDetector.enTouchType.SwipeDown替换了其中一个KeyCode调用。 Yet I am still getting an error.
但是我仍然遇到错误。 Thank you in advance for anyone's help.
预先感谢任何人的帮助。 Thank you for your time as well.
也感谢您的宝贵时间。
Look at your error - No overload for method UpdatePlayer' takes0' arguments
. 查看您的错误-
No overload for method UpdatePlayer' takes0' arguments
。 It means that you need to supply an additional data to your method. 这意味着您需要为方法提供其他数据。 Which data you need to supply intellisense can tell you.
您需要提供intellisense的哪些数据可以告诉您。
In your code: 在您的代码中:
void Update ()
{
UpdatePlayer();
...
You call UpdatePlayer()
with zero arguments. 您使用零参数调用
UpdatePlayer()
。 However, UpdatePlayer
needs an argument: 但是,
UpdatePlayer
需要一个参数:
private void UpdatePlayer(TouchDetector.enTouchType T)
{
So, when you call UpdatePlayer()
, you need to send an object of the type TouchDetector.enTouchType
as parameter. 因此,当您调用
UpdatePlayer()
,您需要发送TouchDetector.enTouchType
类型的对象作为参数。 That would be one of the following: 那将是以下之一:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.