Unity - 意外的符号“{”

Unity - Unexpected Symbol '{'

这是我的代码 Unity 说-"Unexpected Symbol {"

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera : MonoBehaviour {
//variables

    public Transform player;
    public float smooth = 0.3f;
    private Vector3 velocity = Vector3.zero;  //camera velocity to zero with variable velocity

    //Methods
    void Update()
    {
        Vector3 pos = new Vector3();
        pos.x = player.position.x;  // postion on x axis = player
        pos.z = player.position.z - 7f;  //-7f to move the camera a little back from player position
        pos.y = player.position.y;
        transform.position = Vector3.SmoothDamp{ transform.position, pos,ref velocity, smooth};
        //smoothdamp is a function of vector 3 which smoothenes the movement
    }   
}

Rufus L 是正确的。

您在 SmoothDamp 方法中使用了大括号而不是圆括号,请在此处查看如何使用它https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

在 C# 中,我们使用大括号来显示属于语句的代码块,例如if 块、using 块、方法块、class 块等

大括号也用于对象实例化(调用构造函数时)以初始化变量,例如

Person john = new Person(){ Name = "John" };

简而言之,大括号定义范围,其中定义的值在大括号终止时超出范围,除非这些值存在于其他地方。

然而,括号用于其他多种事物,但其中 none 用于指示范围。它们用于指示参数、转换、更改数学表达式出现的顺序等。 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/invocation-operator

总之,注意不要混淆()和{}+

调用不带大括号“{}”的方法时使用括号“()”。

改变这个:

transform.position = Vector3.SmoothDamp{ transform.position, pos,ref velocity, smooth};

对此:

transform.position = Vector3.SmoothDamp( transform.position, pos, ref velocity, smooth);