简体   繁体   English

如何让效应器在我的角色统一2D中工作

[英]How to get effectors working on my character unity 2d

I'm trying to use a buoyancy effector in my platformer however for some reason due to my controller script that's attached to my player makes the effector does not work. 我试图在平台器中使用浮力效应器,但是由于某些原因,由于连接到播放器的控制器脚本使效应器无法工作。 Below is my controller script. 以下是我的控制器脚本。

controller.cs controller.cs

using UnityEngine;
using System.Collections;

public class controller : MonoBehaviour
{

    public float topSpeed = 15f;
    bool facingRight = true;

    bool grounded = false;

    public Transform groundCheck;

    float groundRadius = 0.2f;

    GameObject Player, Player2;
    int characterselect;

    public float jumpForce = 700f;


    public LayerMask whatIsGround;

    void Start()
    {
        characterselect = 1;
        Player = GameObject.Find("Player");
        Player2 = GameObject.Find("Player2");


    }

    void FixedUpdate()
    {

        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

        float move = Input.GetAxis("Horizontal");

        GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
        if (move > 0 && !facingRight) //if facing not right then use the flip function
            flip();
        else if (move < 0 && facingRight)
            flip();


    }

    void Update()
    {

        if(grounded&& Input.GetKeyDown(KeyCode.Space))
        {

            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
        }
        {


        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            topSpeed = topSpeed * 2;
        }
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            topSpeed = 15;
        }

    }

    void flip()
    {

        facingRight = ! facingRight;

        Vector3 theScale = transform.localScale;

        theScale.x *= -1;

        transform.localScale = theScale;


    }

}

When I disable this script it works fine. 当我禁用此脚本时,它可以正常工作。 Just wondering how I can make the effector work with my current controller script. 只是想知道如何使效果器与当前的控制器脚本一起工作。

It's this line: 是这行:

GetComponent<Rigidbody2D>().velocity = new Vector2(
    move * topSpeed,
    GetComponent<Rigidbody2D>().velocity.y);

By setting the velocity, you are overriding whatever velocity is added by the physics engine—including from buoyancy effectors. 通过设置速度,您将覆盖物理引擎添加的任何速度,包括浮力效应器产生的速度。 If you want to keep the physics from the buoyancy effector, do some kind of sum instead. 如果要使物理不受浮力效应器的影响,请执行某种总和。 There are plenty of ways to implement this, depending on exactly what behavior you want. 有多种方法可以实现此目的,具体取决于您想要的行为。

I would also recommend that you store the Rigidbody2D as a variable to save time for both you and the computer: 我还建议您将Rigidbody2D存储为变量,以节省您和计算机的时间:

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour
{
    // ...
    Rigidbody2D rbody2D;

    void Start() {
        // ...
        rbody2D = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate() {
        // One simple example of adding to the velocity.
        // Adds velocity in the x-axis such that the new x velocity
        //   is about equal to topSpeed in the direction we are trying to move.
        // Note that "Vector2.right * deltaVelX" = "new Vector2(deltaVelX, 0)".
        float move = Input.GetAxis("Horizontal");
        float deltaVelX = (move * topSpeed) - rbody2D.velocity.x;
        rbody2D.velocity += Vector2.right * deltaVelX;
    }
}

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

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