#2D Top-Down Movement

1 messages · Page 1 of 1 (latest)

mild quail
floral scarab
#

movement.x is like velocity.x right?

#

If the movement isn’t super small, I think it’s safe to use > .5

mild quail
#

i think when the player is moving full speed they are at 1

#

you can see in this video!

#

the second and third values on the bottom left are horizontal and vertical

floral scarab
#

Also I think it’s float precision again

#

if movement > 0.01 might be too small

mild quail
#

Okay so maybe I'll try my old solution but with 0.5

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

public class PlayerMovement : MonoBehaviour
{
    public float movementSpeed;
    public Rigidbody2D rigidBody;
    private Vector2 moveDirection;
    public float speedModifier;
    public Animator animator;
    public int directionFacing; // 0-7 (N, NE, E...)
    public float dirToSet = 1;
    public float lastX = 1;
    public float lastY = 1;


    void Update()
    {
        ProcessInputs();

        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0.0f);

        animator.SetFloat("Horizontal",movement.x);
        animator.SetFloat("Vertical",movement.y);
        animator.SetFloat("Magnitude",movement.magnitude);

        animator.SetBool("IsIdle",rigidBody.velocity.x == 0 && rigidBody.velocity.y==0);

        // dirToSet = Direction(movement.x,movement.y) == 0 ? dirToSet : Direction(movement.x,movement.y);

        // animator.SetFloat("Direction",dirToSetA/8);
        
    }

    void FixedUpdate()
    {
        Move();
    }

    void ProcessInputs()
    {
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveY = Input.GetAxisRaw("Vertical");

        moveDirection = new Vector2(moveX, moveY).normalized;
    }

    void Move() 
    {
        float trueSpeed = (float)(movementSpeed * (Input.GetKey("left shift") ? 1.5 : 1));
        animator.speed = (float)(Input.GetKey("left shift") ? 1.5 : 1);
        rigidBody.velocity = new Vector2(moveDirection.x * trueSpeed, moveDirection.y * trueSpeed);
    }

    // public float Direction(float x, float y) 
    // {
    //     return ((((Bmult(x>0,y==0))* 1) + (Bmult(x>0,y>0))* 2) + ((Bmult(x==0,y>0))* 3) + ((Bmult(x<0,y>0))* 4) +((Bmult(x<0,y==0))* 5) + ((Bmult(x<0,y<0))* 6) + ((Bmult(x==0,y<0))* 7) +((Bmult(x>0,y<0))* 8));
    // }
    // 
    // public int Bmult(bool x, bool y)
    // {
    //     return (x ? 1 : 0)*(y ? 1 : 0);
    // }
}
#

this is my current code for the player movement plus what it sends to the animator

#

I will add those limits and let you know how that goes

#
lastX = (float)(movement.x < 0.5 && movement.x > -0.5 ? 0.5*Math.Sign(movement.x) : movement.x);
lastY = (float)(movement.y < 0.5 && movement.y > -0.5 ? 0.5*Math.Sign(movement.y) : movement.y);

animator.SetFloat("Horizontal",lastX);
animator.SetFloat("Vertical",lastY);
#

so this makes the minimum of each 0.5/-0.5

#

or it should.. :/

#

somehow, I have no idea how, it sets it to zero still

floral scarab
#

Unless the player can do a .4 movement it’ll detect movement and shouldn’t think it has to face either right or left on idle

#

Don’t try making the minimum value .5, but try making the detection a minimum of .5 if that makes sense

mild quail
#

so like this?

#
if(movement.x < -0.5 || movement.x > 0.5) {
  animator.SetFloat("Horizontal",movement.x);
}
floral scarab
mild quail
#

caused some new jank but it seems to store the direction a litttttlee bit better

floral scarab
mild quail
floral scarab
#

Ahh ok

floral scarab
# mild quail

Seems like now it can’t tell the cardinal directions but seems to work although it defaults to up on idle

mild quail
#

side note do you like my sign interaction animation lol

#

i wonder if it would help for me to move the positions of where the animations start closer to the middle? i doubt it though

floral scarab
mild quail
#

i think so too, the black pixels on the corners are a lil jank

#

yeah moving them didn't seem to work :/

#

it's not working because when the vertical is locked to 0.5 minimum, it can't be 0, so there will always be some vertical movement, so he moves at a diagonal

#

if i were to make it so it could be zero it would basically put me back to square one

floral scarab
#

Aww rip

#

Hmm what if you revert vertical back to what you had earlier but keep horizontal to fix the right facing problem

#

Actually idk