我不知道为什么我使用的脚本不起作用,我已经尝试了很多次才能让它工作,但事实并非如此。我环顾四周的视频,网站和其他地方,但我找不到解决方案。所以现在我希望我来对了地方,但无论如何这里是脚本和我得到的错误。
using UnityEngine;
namespace UnitySampleAssets._2D
{
[RequireComponent(typeof (PlatformerCharacter2D))]
public class Platf {
private PlatformerCharacter2D character;
private bool jump;
public object CrossPlatformInputManager { get; private set; }
public PlatformerCharacter2D Character
{
get
{
return Character2;
}
set
{
Character2 = value;
}
}
public bool Jump
{
get
{
return Jump2;
}
set
{
Jump2 = value;
}
}
public PlatformerCharacter2D Character1
{
get
{
return Character2;
}
set
{
Character2 = value;
}
}
public bool Jump1
{
get
{
return Jump2;
}
set
{
Jump2 = value;
}
}
public PlatformerCharacter2D Character2
{
get
{
return Character3;
}
set
{
Character3 = value;
}
}
public bool Jump2
{
get
{
return Jump3;
}
set
{
Jump3 = value;
}
}
public PlatformerCharacter2D Character3
{
get
{
return character;
}
set
{
character = value;
}
}
public bool Jump3
{
get
{
return jump;
}
set
{
jump = value;
}
}
private void Awake()
{
Character = GetComponent<PlatformerCharacter2D>();
}
private void Update()
{
if (!Jump)
// Read the jump input in Update so button presses aren't missed.
Jump = CrossPlatformInputManager.GetButtonDown("Jump");
}
private void FixedUpdate()
{
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftControl);
float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
Character.Move(h, crouch, Jump);
Jump = false;
}
}
}Platformer2DUserControl.cs : MonoBehaviour
我得到的问题是'CS0116名称空间不能直接包含诸如字段或方法之类的成员',以及'当前上下文中不存在'Platformer2DUserControl.cs'
如果您有解决方案或提示,请告诉我。
答案 0 :(得分:0)
将班级定义更改为:
[RequireComponent(typeof (PlatformerCharacter2D))]
public class Platf : MonoBehavior
{
// Implementation
} // remove: Platformer2DUserControl.cs : MonoBehaviour
结束括号后的部分被解释为您想要声明的内容。要定义继承,只需在类名后添加:
,然后添加要继承的类的名称。
修改强> 最后,您的代码将如下所示:
using UnityEngine;
namespace UnitySampleAssets._2D
{
[RequireComponent(typeof (PlatformerCharacter2D))]
public class Platf : MonoBehavior {
private PlatformerCharacter2D character;
private bool jump;
public object CrossPlatformInputManager { get; private set; }
public PlatformerCharacter2D Character
{
get
{
return Character2;
}
set
{
Character2 = value;
}
}
public bool Jump
{
get
{
return Jump2;
}
set
{
Jump2 = value;
}
}
public PlatformerCharacter2D Character1
{
get
{
return Character2;
}
set
{
Character2 = value;
}
}
public bool Jump1
{
get
{
return Jump2;
}
set
{
Jump2 = value;
}
}
public PlatformerCharacter2D Character2
{
get
{
return Character3;
}
set
{
Character3 = value;
}
}
public bool Jump2
{
get
{
return Jump3;
}
set
{
Jump3 = value;
}
}
public PlatformerCharacter2D Character3
{
get
{
return character;
}
set
{
character = value;
}
}
public bool Jump3
{
get
{
return jump;
}
set
{
jump = value;
}
}
private void Awake()
{
Character = GetComponent<PlatformerCharacter2D>();
}
private void Update()
{
if (!Jump)
// Read the jump input in Update so button presses aren't missed.
Jump = CrossPlatformInputManager.GetButtonDown("Jump");
}
private void FixedUpdate()
{
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftControl);
float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
Character.Move(h, crouch, Jump);
Jump = false;
}
}
}