简体   繁体   English

我的统一 2d 运动脚本中有很多错误

[英]I'm getting so many errors in my unity 2d movement script

I wrote a movement script and it wasn't working and I had many errors.我写了一个动作脚本,但它不起作用,而且我有很多错误。 I'm new so I don't know how to read the errors and I don't have any coding friends.我是新手,所以我不知道如何阅读错误,也没有任何编码朋友。 I had 9 errors and did research before.我有9个错误,之前做过研究。 There was something called forcemode that I did overlook that might have to be used.我确实忽略了可能必须使用的称为 forcemode 的东西。

These were my errors:这些是我的错误:

*Assets\Scripts\PlayerMovement.cs(36,27): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2' *Assets\Scripts\PlayerMovement.cs(36,27): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2'

Assets\Scripts\PlayerMovement.cs(33,27): error CS1503: Argument 1: cannot convert from 'int' to 'UnityEngine.Vector2' Assets\Scripts\PlayerMovement.cs(33,27):错误 CS1503:参数 1:无法从“int”转换为“UnityEngine.Vector2”

Assets\Scripts\PlayerMovement.cs(30,27): error CS1503: Argument 1: cannot convert from 'int' to 'UnityEngine.Vector2' Assets\Scripts\PlayerMovement.cs(30,27):错误 CS1503:参数 1:无法从“int”转换为“UnityEngine.Vector2”

Assets\Scripts\PlayerMovement.cs(17,39): error CS0236: A field initializer cannot reference the non-static field, method, or property 'PlayerMovement.ForceD2' Assets\Scripts\PlayerMovement.cs(17,39):错误 CS0236:字段初始化程序无法引用非静态字段、方法或属性“PlayerMovement.ForceD2”

Assets\Scripts\PlayerMovement.cs(17,33): error CS0236: A field initializer cannot reference the non-static field, method, or property 'PlayerMovement.Force' Assets\Scripts\PlayerMovement.cs(17,33):错误 CS0236:字段初始化程序无法引用非静态字段、方法或属性“PlayerMovement.Force”

Assets\Scripts\PlayerMovement.cs(16,27): error CS0236: A field initializer cannot reference the non-static field, method, or property 'PlayerMovement.Force' Assets\Scripts\PlayerMovement.cs(16,27):错误 CS0236:字段初始化程序无法引用非静态字段、方法或属性“PlayerMovement.Force”

Assets\Scripts\PlayerMovement.cs(36,30): error CS1503: Argument 2: cannot convert from 'int' to 'UnityEngine.ForceMode2D' Assets\Scripts\PlayerMovement.cs(36,30):错误 CS1503:参数 2:无法从“int”转换为“UnityEngine.ForceMode2D”

Assets\Scripts\PlayerMovement.cs(39,27): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2' Assets\Scripts\PlayerMovement.cs(39,27):错误 CS1503:参数 1:无法从“float”转换为“UnityEngine.Vector2”

Assets\Scripts\PlayerMovement.cs(39,30): error CS1503: Argument 2: cannot convert from 'int' to 'UnityEngine.ForceMode2D'* Assets\Scripts\PlayerMovement.cs(39,30):错误 CS1503:参数 2:无法从“int”转换为“UnityEngine.ForceMode2D”*

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

public class PlayerMovement : MonoBehaviour
{
    //rb2D is assigned to the selected rigid body
    public Rigidbody2D rb2D;


    void Awake(){
        rb2D = gameObject.AddComponent<Rigidbody2D>();
    }

    //Lets you remotely change the amount of force being put on the object 
    public int Force;
    private int ForceD2 = Force*2f;
    private int NegitiveForce = Force-ForceD2;

    //Allows you to remotely turn off keys
    public bool left;
    public bool right;
    public bool down;
    public bool up;
    // Update is called once per frame
    void Update()
    {
        //Moves Object Based on Key

        if(Input.GetKey("a")|| Input.GetKey("left") && left){
            rb2D.AddForce(NegitiveForce,0f);
        }
        if(Input.GetKey("d")|| Input.GetKey("right") && right){
            rb2D.AddForce(Force,0f);
        }
        if(Input.GetKey("s")|| Input.GetKey("down") && down){
            rb2D.AddForce(0f,NegitiveForce);
        }
        if(Input.GetKey("w")|| Input.GetKey("up") && up){
            rb2D.AddForce(0f,Force);
        }
    }
}

So to the first所以对于第一个

Assets\Scripts\PlayerMovement.cs(16,27): error CS0236: A field initializer cannot reference the non-static field, method, or property 'PlayerMovement.Force' Assets\Scripts\PlayerMovement.cs(16,27):错误 CS0236:字段初始化程序无法引用非静态字段、方法或属性“PlayerMovement.Force”

this is caused by这是由

public int Force;
private int ForceD2 = Force*2f;
private int NegitiveForce = Force-ForceD2;

and telling you, you can't simply access the Force and later Force2D at this point.告诉你,此时你不能简单地访问Force和后来的Force2D You rather need to shift this into eg您宁愿将其转换为例如

public int Force;
private int ForceD2;
private int NegitiveForce;

pivate void Awake()
{
    ForceD2 = Force * 2f;
    NegitiveForce = Force - ForceD2;
}

or if you want to keep these updating on runtime you could use properties like eg或者,如果您想在运行时保持这些更新,您可以使用类似的属性

public int Force;
private int ForceD2 => Force*2f;
private int NegitiveForce => Force-ForceD2;

which means they will be recalculated for each and every access.这意味着它们将为每次访问重新计算。

Besides that,why is NegitiveForce not rather simply -Force ..?除此之外,为什么NegitiveForce不是简单的-Force ..?


And then to然后到

Assets\Scripts\PlayerMovement.cs(36,30): error CS1503: Argument 2: cannot convert from 'int' to 'UnityEngine.ForceMode2D' Assets\Scripts\PlayerMovement.cs(36,30):错误 CS1503:参数 2:无法从“int”转换为“UnityEngine.ForceMode2D”

and

Assets\Scripts\PlayerMovement.cs(39,27): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2' Assets\Scripts\PlayerMovement.cs(39,27):错误 CS1503:参数 1:无法从“float”转换为“UnityEngine.Vector2”

Then other than for Rigidbody.AddForce where there is an overload taking float, float, float is parameters, there is no such overload for Rigidbody2D.AddForce !然后除了Rigidbody.AddForce有一个重载采用float, float, float是参数, Rigidbody2D.AddForce没有这样的重载!

The only overload here is这里唯一的重载是

public void AddForce(Vector2 force, ForceMode2D mode = ForceMode2D.Force);

and the compiler is telling you that your input parameters are not compatible with it;)并且编译器告诉您您的输入参数与它不兼容;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM