卷发牙套在c#unity中不起作用
我在做一个统一游戏,然后我脚本中的花括号变傻了。他们都搞砸了,我不知道我做错了什么。这是我的剧本: ps:我正在使用visual studio卷发牙套在c#unity中不起作用,c#,unity3d,C#,Unity3d,我在做一个统一游戏,然后我脚本中的花括号变傻了。他们都搞砸了,我不知道我做错了什么。这是我的剧本: ps:我正在使用visual studio using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveSript : MonoBehaviour { public GameObject myObject; // Use this for initi
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSript : MonoBehaviour {
public GameObject myObject;
// Use this for initialization
private Vector3 direction = new Vector3(0, 0, 0);
// Update is called once per frame
private float speed = 40f;
void Start() { // error here? --> "} expected"
private Camera cam = Camera.main;
private float height = 2f * cam.orthographicSize;
private float width = height * cam.aspect;
} // i close it here, but it closes the mono beh. class instead?
void Update () {
int y = 0;
int x = 0;
if (Input.GetKey("up"))
{
y = 1;
}
if (Input.GetKey("down"))
{
y = -1;
}
if (Input.GetKey("right"))
{
x = 1;
}
if (Input.GetKey("left"))
{
x = -1;
}
direction = new Vector3(x, y, 0);
myObject.transform.position += direction.normalized*speed*Time.deltaTime;
}
}
我做错了什么?谢谢你的进步 不能在方法内部定义的局部变量中定义可访问性。应该是
private void Start()
{
var cam = Camera.main;
var height = 2f * cam.orthographicSize;
var width = height * cam.aspect;
// Makes only sense if you now use the width
// and/or other values for something
}
或者在类级别定义它们(例如,以后也可以在其他方法中访问它们),如
是否发生任何错误?你的意思是缩进搞砸了吗?@fhcimolin我在visual studio中做缩进,在“start(){”它说“}预期”,但我稍后关闭了它?如果你在实例方法中声明全局变量,请检查你的
start
方法,private
不能在那里使用。private
关键字不属于(或有意义)方法中的局部声明变量。试着少用侮辱性的名称来调用编译器,多用代码的语法和结构。我向你保证,你不会伤害编译器的感情或让它改变主意。啊,好的,谢谢。我对c相当陌生,所以我忘了private做什么,但用了它哈哈
private Camera cam;
private float height;
private float width;
private void Start()
{
cam = Camera.main;
height = 2f * cam.orthographicSize;
width = height * cam.aspect;
}