#ok

1 messages · Page 1 of 1 (latest)

tardy musk
#

wait imma send

#

first of all this is the code

wanton estuary
golden bearBOT
tardy musk
#

and this is the distance he stops at

#

and i need him to basically smooch the edge of the statue

tardy musk
# wanton estuary !code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed;

    private bool isMoving;

    private Vector2 input;

    private Animator animator;

    public LayerMask solidObjectLayer;

    private void Awake()
    {
        animator = GetComponent<Animator>();
    }

    private void Update()
    {
        if (!isMoving)
        {
            input.x = Input.GetAxisRaw("Horizontal");
            input.y = Input.GetAxisRaw("Vertical");

            Debug.Log("This is input.x" + input.x);
            Debug.Log("This is input.y" + input.y);
            if (input.x != 0) input.y = 0;

            if (input != Vector2.zero)
            {

                animator.SetFloat("moveX", input.x);
                animator.SetFloat("moveY", input.y);

                var targetPos = transform.position;
                targetPos.x += input.x;
                targetPos.y += input.y;

                if(isWalkable(targetPos))

                StartCoroutine(Move(targetPos));
            }
        }

        animator.SetBool("isMoving", isMoving);

    }

    IEnumerator Move(Vector3 targetPos)
    {
        isMoving = true;
        while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
        {
            transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
            yield return null;
        }
        transform.position = targetPos;

        isMoving = false;
    }

    private bool isWalkable(Vector3 targetPos)
    {
        if(Physics2D.OverlapCircle(targetPos, 0.2f, solidObjectLayer) != null)
        {
            return false;
        }
        return true;

    }

}

tardy musk
wanton estuary
#

Read the bot

tardy musk
wanton estuary
tardy musk
wanton estuary
sly crown
#

@dry onyx hope this helps

tardy musk
#

i tried getaxis without raw but it kinda fked everything up

dry onyx
#

perfect

#

discord mobile has been strange lately

#

is your character dynamic or kinematic

tardy musk
tardy musk
dry onyx
#

if it is dynamic, then you can actually overshoot the distance, and physics sim will automatically try to depenetrate if you are slightly off

wanton estuary
dry onyx
#

oh, then no physics2D is really needed at all

wanton estuary
dry onyx
#

if everything snaps to a grid, then not physics2D is needed. eg pokemon gen 1

#

if not, then you will need colliders for free movement

tardy musk
wanton estuary
dry onyx
tardy musk
wanton estuary
#

you're checking an adjacent location and then tweening to a specific spot

dry onyx
#

then you don’t need physics2D

wanton estuary
#

that's grid based

dry onyx
#

yeah

#

grid based is a lot simplerp

tardy musk
#

ohhh

#

but then how can i fix it

dry onyx
#

physics2D solves forces and shit

#

and velocity

wanton estuary
#

the grid cell is occupied

#

so you don't move there

#

that's how it works

tardy musk
wanton estuary
#

Those are your two options. Use a grid or don't

dry onyx
#

the moment you allow moving off the grid, shit gets much more complicated

#

you have to actually use physics queries, instead of just checking what is in the next grid space

tardy musk
#

hmmmm is it hard to set up?

dry onyx
#

it’s not hard, but it is a LOT more involved than pokemon gen1 style motion

#

so you really need to decide wtf you want to do before you start

tardy musk
tardy musk
dry onyx
#

in physics2D, to check if a space is open to move, you need to cast shapes, work with vectors, and do math

#

in a grid, you just need to check the cell to see if it is occupied or not

#

a grid requires a “custom” physics engine, which is extremely simple to make.
Physics2D requires you to work with the physics engine, and do a lot more math

tardy musk
#

hmmm now i see, so my only option to make the physics of this game work if i use grid is to cope or to place the solidObjects smartly

dry onyx
#

so i recommend you take the question of whether or not you should allow the player to move freely off of the grid very seriously

#

it is not a small little thing

#

big deal

tardy musk
dry onyx
#

you write it

#

it’s extremely simple to make

tardy musk
#

the grid i can

dry onyx
#

there is so much to know about Physics2D. I picked up info from a lot of places.

tardy musk
dry onyx
#

since I was a teenager lol

#

many many years

tardy musk
#

daaamn

#

i just started (i am a teenager)

dry onyx
#

learning physics2D took me a few months, because not everything is fully documented

#

there are a lot of random checkboxes and shit that are critical

#

i recommend you start simple with grid based movement. Just code an array for your world, and work with it.

tardy musk
dry onyx
#

looking at your game, grid looks better

tardy musk
dry onyx
#

if everything in your game is very grid-locked, giving the player free motion introduces jank and makes it slippery

#

start with some tutorial projects first

tardy musk
dry onyx
#

learn while you are in control of the situation

tardy musk
tardy musk