简体   繁体   English

我收到错误 CS7036,我不知道如何解决它

[英]I get error CS7036 and I am clueless on how to solve it

I am new to coding and using it for a school project.我是编码新手并将其用于学校项目。 I have seen some code learning sessions, but my coding abilities are limited.我看过一些代码学习课程,但我的编码能力有限。

What I basically want, is an enemy moving up, stopping at a collider and then moving down and stopping at another ground collider and repeat.我基本上想要的是一个敌人向上移动,停在一个对撞机上,然后向下移动,停在另一个地面对撞机上并重复。 At first, I found a script that, should it be used, the object will move up/down until it meets a collider.起初,我发现了一个脚本,如果使用它,object 将向上/向下移动,直到遇到对撞机。 This is that particular code:这是特定的代码:


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

public class PlayerTest : MonoBehaviour
{
    public float speed = 10.0f;
    public Rigidbody rb;
    public Vector3 movement;

// Start is called before the first frame update
void Start()
{
    rb.GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown("w"))
    {
        movement = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        }
    }
    void FixedUpdate()
    {
            moveCharacter(movement);
    }

    void moveCharacter(Vector3 direction)
    {
        rb.MovePosition(transform.position + (transform.forward * speed * Time.deltaTime));
    }
}

I decided to modify it, to satisfy the needs that I mentioned, like this (note that when I tested the above code, it worked normally)我决定修改它,以满足我提到的需求,就像这样(注意当我测试上面的代码时,它工作正常)

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

public class Enemy_Movement : MonoBehaviour
{
    public float speed = 0.1f;
    public Rigidbody2D rb;
    public Vector3 movement;
    public Collider2D EnemyCollider;
    public Collider2D DirtCollider;
    public Collider2D CeilingCollider;

    // Start is called before the first frame update
    void Start()
    {
        rb.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        movement = new Vector3(Input.GetAxis("Horizontal"), 1f, Input.GetAxis("Vertical"));

        if (EnemyCollider.IsTouching(DirtCollider))
        {
            moveCharacterUp();
        }

        if (EnemyCollider.IsTouching(CeilingCollider))
        {
            moveCharacterDown();
        }
    }
    void moveCharacterUp(Vector3 direction)
    {
        Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
        rb.MovePosition(transform.position + (offset * speed * Time.deltaTime));
    }

    void moveCharacterDown(Vector3 direction)
    {
        Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
        rb.MovePosition(transform.position - (offset * speed * Time.deltaTime));
    }

However, when I try to run it, I get the following message但是,当我尝试运行它时,我收到以下消息

Assets\Enemy_Movement.cs(32,13): error CS7036: There is no argument given that corresponds to the required formal parameter 'direction' of 'Enemy_Movement.moveCharacterDown(Vector3)' Assets\Enemy_Movement.cs(32,13):错误 CS7036:没有给出与“Enemy_Movement.moveCharacterDown(Vector3)”所需的形参“方向”相对应的参数

I know that I might have done an idiotic mistake, but unfortunately, I have searched everywhere but I could not find a proper solution.我知道我可能犯了一个愚蠢的错误,但不幸的是,我到处搜索,但找不到合适的解决方案。 (And yea, it looks hideous, but it is my first project). (是的,它看起来很可怕,但这是我的第一个项目)。 Any help would be appreciated!任何帮助,将不胜感激! Thanks!谢谢!

Your moveCharacterDown method takes a Vector3 argument, but you're trying to call it without arguments, hence the error.您的moveCharacterDown方法采用Vector3参数,但您尝试在没有 arguments 的情况下调用它,因此出现错误。 Having said that, after reading through the code, this argument is not used, so the easiest solution would be to remove it:话虽如此,通读代码后,并没有使用此参数,因此最简单的解决方案是将其删除:

void moveCharacterUp() // Here
{
    Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
    rb.MovePosition(transform.position + (offset * speed * Time.deltaTime));
}

void moveCharacterDown() // And here
{
    Vector3 offset = new Vector3(movement.x + transform.position.x, movement.y + transform.position.y, movement.z + transform.position.z);
    rb.MovePosition(transform.position - (offset * speed * Time.deltaTime));
}

Your problem is how you call the moveCharacterUp and moveCharacterDown methods.您的问题是如何调用moveCharacterUpmoveCharacterDown方法。 Both methods expect a Vector3 as a parameter, but you use them without a parameter.这两种方法都需要Vector3作为参数,但您使用它们时没有参数。

Since you don't seem to use the direction parameter in the methods anyway, remove them and it should fix the problem.由于您似乎无论如何都没有在方法中使用方向参数,因此删除它们应该可以解决问题。

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

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