C#“预期错误“ [英] C# " } expected error "

查看:78
本文介绍了C#“预期错误“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bracks让我生气:D



 使用系统; 
使用 UnityEngine;

命名空间 UnitySampleAssets._2D
{

public class Camera2DFollow:MonoBehaviour
{

public 转变目标;
public float damping = 1 ;
public float lookAheadFactor = 3 ;
public float lookAheadReturnSpeed = 0 .5f;
public float lookAheadMoveThreshold = 0 .1F;
public float yPosRestriction = -1;

private float offsetZ;
private Vector3 lastTargetPosition;
private Vector3 currentVelocity;
private Vector3 lookAheadPos;

float nextTimeToSearch = 0 ;

// 用于初始化
private void Start()
{
lastTargetPosition = target.position;
offsetZ =(transform.position - target.position).z;
transform.parent = null ;
}

// 每帧调用一次更新
private void 更新()
{
if (target == null ){
target = FindPlayer();
return ;
}

// 仅在加速或改变方向时更新前瞻pos
float xMoveDelta =(target.position - lastTargetPosition).x;

bool updateLookAheadTarget = Mathf.Abs(xMoveDelta)> lookAheadMoveThreshold;

if (updateLookAheadTarget)
{
lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
}
else
{
lookAheadPos = Vector3.MoveTowards(lookAheadPos,Vector3.zero,Time.deltaTime * lookAheadReturnSpeed );
}

Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position,aheadTargetPos, ref currentVelocity,damping);

float clamp = Mathf.Clamp(newPos.y,-1,Mathf.Infinity);
newPos = new Vector3(newPos.x,clamp,newPos.z);

transform.position = newPos;

lastTargetPosition = target.position;
}

private 转换FindPlayer()
{
转换searchResult = GameObject.FindGameObjectWithTag( player)。transform;

if (searchResult == null
{
Debug.LogWarning( 无法找到播放器对象。);
return null ;
}
else
{
return searchResult;
}

}
}





我尝试过:



预计错误是最后一个问题



公共类Camera2DFollow:MonoBehaviour

{

解决方案

每个开头'{'应该有相应的'}'



例如

 {
{
}
}





在您的情况下,文档末尾缺少'}'。


该类由}关闭,但名称空间不是't。

在文档的末尾添加一个},如果没有;请自动重新格式化代码,尝试按CTRL + K,D强制重新格式化。


引用:

C#}预期错误



使用适当的工具,你应该在几秒钟内发现问题。



学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。



专业程序员的编辑器具有此功能以及其他功能,如括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]


bracks are make me mad :D

using System;
using UnityEngine;

namespace UnitySampleAssets._2D
{

    public class Camera2DFollow : MonoBehaviour
    {

        public Transform target;
        public float damping = 1;
        public float lookAheadFactor = 3;
        public float lookAheadReturnSpeed = 0.5f;
        public float lookAheadMoveThreshold = 0.1f;
        public float yPosRestriction = -1;

        private float offsetZ;
        private Vector3 lastTargetPosition;
        private Vector3 currentVelocity;
        private Vector3 lookAheadPos;

        float nextTimeToSearch = 0;

        // Use this for initialization
        private void Start()
        {
            lastTargetPosition = target.position;
            offsetZ = (transform.position - target.position).z;
            transform.parent = null;
        }

        // Update is called once per frame
        private void Update()
        {
            if (target == null) {
                target = FindPlayer();
                return;
            }

            // only update lookahead pos if accelerating or changed direction
            float xMoveDelta = (target.position - lastTargetPosition).x;

            bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;

            if (updateLookAheadTarget)
            {
                lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
            }
            else
            {
                lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
            }

            Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
            Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);

            float clamping = Mathf.Clamp(newPos.y, -1, Mathf.Infinity);
            newPos = new Vector3(newPos.x, clamping, newPos.z);

            transform.position = newPos;

            lastTargetPosition = target.position;
        }

            private Transform FindPlayer()
            {
            Transform searchResult = GameObject.FindGameObjectWithTag("player").transform;

            if (searchResult == null)
            {
                Debug.LogWarning("Player object could not be found.");
                return null;
            }
            else
            {
                return searchResult;
            }

            }
    }



What I have tried:

expected error is the last bracks to this

public class Camera2DFollow : MonoBehaviour
{

解决方案

Each opening '{' should have a corresponding '}'

e.g.

{
    {
    }
}



In your case, there is a missing '}' at the end of the document.


The class is closed by a "}", but the namespace isn't.
Add a "}" at the end of the document, and if it doesn;t reformat you code correctly automatically, try CTRL+K, D to force a reformat.


Quote:

C# " } expected error "


With proper tools, you should have spot the problem in matter of seconds.

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]


这篇关于C#“预期错误“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆