Meerfall the dewott
Meerfall the dewott

Reputation: 219

What is making the player character fall sharply

I'm currently getting an issue when my player sprite falls off the land. When I press the 'right arrow' to move horizontally, the sprite suddenly accelerates downwards really fast. I think it is something with the left and right movement but I am unsure if it is the culprit and what the better solution is.

The main portion that I think is causing this issue is this line:

controller.velocity = controller.velocity + new Vector2(horizontal * speed, controller.velocity.y);

I copied this code from a unity forum and at the time it made sense but I believe it is a culprit. If it is, please could you educate on the better solution.

Please let me know if I should include anything else!

Project settings

The y gravity for the project is set to '-9.81'

Player Sprite Object (picture 1)

enter image description here

(picture 2)

enter image description here

Player class (attached to player sprite)

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

public class Player_script : MonoBehaviour
{
    Animator animator = null;
    Rigidbody2D controller = null;
    SpriteRenderer sprite_renderer = null;
    bool walking = false;
    bool jumping = false;
    int speed = 2;
    int jumpSpeed = 200;
    const int RIGHT = 1;
    const int LEFT = 0;
    int direction = RIGHT;

    // Start is called before the first frame update
    void Start()
    {
        animator = gameObject.GetComponent<Animator>();
        controller = gameObject.GetComponent<Rigidbody2D>();
        sprite_renderer = gameObject.GetComponent<SpriteRenderer>();
        controller.velocity = Vector3.down * speed;
        //Physics.gravity = new Vector3(0, -11.0f, 0);
        //controller.AddForce(Vector3.down * 2);
    }

    // Update is called once per frame
    void Update()
    {
        animate();
        //fall
        //controller.AddForce(Physics.gravity + (new Vector3(0,-150) * 3), ForceMode2D.Force);
    }

    void FixedUpdate()
    {
        //controller.AddForce(Physics.gravity + (new Vector3(0, -150) * 3), ForceMode2D.Force);
        //controller.AddForce(Vector3.down * 2);

        float horizontal = Input.GetAxis("Horizontal");
        float veritical = Input.GetAxis("Vertical");

        if (horizontal > 0f)
        {
            controller.velocity = controller.velocity + new Vector2(horizontal * speed, controller.velocity.y);
            walking = true;
            if (direction != RIGHT)
            {
                flipPlayer();
            }
        }
        else if (horizontal < 0f)
        {
            controller.velocity = controller.velocity + new Vector2((horizontal * speed), controller.velocity.y);
            walking = true;
            if (direction != LEFT)
            {
                flipPlayer();
            }
        }
        else
        {
            walking = false;
        }

        //jump
        if (isGrounded() && veritical > 0f)
        {
            controller.velocity = controller.velocity + new Vector2(0, veritical * jumpSpeed);
            walking = false;
            jumping = true;
            Debug.Log("Jumping");
        }
        else
        {
            jumping = false;
            Debug.Log("Not Jumping");
        }

    }

    void animate()
    {
        if (walking)
        {
            animator.Play("player_animation");
        }
        else
        {
            animator.Play("Idle");
        }
    }

    void flipPlayer() {
        if(direction == RIGHT)
        {
            direction = LEFT;
            sprite_renderer.flipX = true;
        }
        else
        {
            direction = RIGHT;
            sprite_renderer.flipX = false;
        }
    }

    void Jump() { }

    bool isGrounded()
    {
        float raycastDistance = 0.5f;
        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, raycastDistance);
        if (hit.collider != null && hit.transform.gameObject.layer == LayerMask.NameToLayer("landscapes")) { 
            return true;
        }
        return false;
    }
}

Upvotes: 1

Views: 50

Answers (1)

WaterKat
WaterKat

Reputation: 81

So the problem you are experiencing is in fact due to the line you were quoting

controller.velocity = controller.velocity + new Vector2(horizontal * speed, controller.velocity.y);

In the Y direction, you are adding the controllers velocity to itself and therefore its Y velocity becomes exponential.

If you wish to override your characters velocity, you should use

controller.velocity = new Vector2(horizontal * speed, controller.velocity.y);

If you wish to add a force to your current velocity, you could do so through code with

controller.velocity = controller.velocity + new Vector2(horizontal * speed * Time.deltaTime, 0);

or by using Rigibody.AddForce(Vector3) with

controller.AddForce(new Vector2(horizontal*speed,0));

Upvotes: 1

Related Questions