Unity-意外的符号“ {”

时间:2019-03-08 23:27:48

标签: c# visual-studio unity3d c#-3.0

这是我的代码,Unity说-“意外的符号{”

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
    }   
}

2 个答案:

答案 0 :(得分:0)

Rufus L是正确的。

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

在C#中,我们使用花括号显示属于语句的代码块,例如if块,使用块,方法块,类块等。

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

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

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

但是,感觉异常用于其他多种情况,但都没有一个表示范围。它们用于指示参数,强制转换,改变数学表达式的出现顺序等。 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/invocation-operator

简而言之,请注意不要混淆()和{} +

答案 1 :(得分:0)

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

更改此内容

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

对此:

transform.position = Vector3.SmoothDamp(transform.position,pos,参考速度,平滑);