【问题标题】:Unity - Unexpected Symbol '{'Unity - 意外符号'{'
【发布时间】:2019-03-08 23:27:47
【问题描述】:

这是我的代码 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
    }   
}

【问题讨论】:

  • 欢迎来到 Stackoverflow。请阅读stackoverflow.com/help/how-to-ask
  • 调用方法时使用括号:transform.position = Vector3.SmoothDamp (transform.position, pos, ref velocity, smooth);

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


【解决方案1】:

Rufus L 是正确的。

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

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

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

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

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

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

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

【讨论】:

    【解决方案2】:

    在调用没有大括号“{}”的方法时使用括号“()”。

    改变这个:

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

    对此:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 2015-09-24
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多