简体   繁体   中英

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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