我試過每種方式都可以使這個工作沒有成功。我有一個滑動腳本和一個遊戲/控制腳本。我試圖引用控制腳本中的滑動控件作爲控制角色的手段。到目前爲止,我在Unity中只有一個錯誤。Unity3d如何從其他腳本引用滑動控制?
資產/ RuinRunStarterKit /腳本/ csTempleRun.cs(307,25):錯誤CS1501:沒有重載方法UpdatePlayer' takes
0' 參數
我試過UpdatePlayer的括號內添加TouchDetector.enTouchType但是,讓我三個錯誤,而不僅僅是一個。我已經聯繫了許多人,並訪問了許多網站尋找答案,但未能產生工作結果。我沒有受過編程教育。我只是在網上進行了研究,才知道如何做一些編程工作。我在這裏獲得的第二個腳本來自我在Unity資源商店購買的資產。我已經聯繫他們支持添加滑動控件,他們通過電子郵件向我發送了觸摸腳本,但沒有告訴我如何實現它。我只是想在我的手機上玩這個遊戲與控制滑動。這是供個人使用的。如果有人能夠幫助我達到預期的效果,我真的很感激。同樣,我希望能夠使用滑動來控制角色。謝謝。這裏是我的腳本:
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;
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");
}
我希望我沒有做任何錯誤的發佈這個腳本,但我覺得我需要發佈這個,以獲得我需要的幫助。您會注意到在csTempleRun.cs腳本中我用TouchDetector.enTouchType.SwipeDown替換了一個KeyCode調用。但我仍然遇到錯誤。預先感謝任何人的幫助。謝謝你的時間。
我想我正確地理解了你。我將TouchDetector.enTouchType T移動到了Update()方法中UpdatePlayer()的第一個調用,但是這給了我多個錯誤。我需要使用全部四個滑動方向。 – 2013-03-23 03:17:44