#ok
1 messages · Page 1 of 1 (latest)
!code
📃 Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
and this is the distance he stops at
and i need him to basically smooch the edge of the statue
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;
}
}
Link it
wdym link it
So it looks like you're doing grid based movement. You input a direction and you tween to a spot a fixed distance away if that spot is clear.
do you have a solution/better variant
Do you not want grid based movement
@dry onyx hope this helps
i tried getaxis without raw but it kinda fked everything up
perfect
discord mobile has been strange lately
is your character dynamic or kinematic
i mean i want my character to move like a retro pixel game char but i also want the physics to be logical
dynamic
if it is dynamic, then you can actually overshoot the distance, and physics sim will automatically try to depenetrate if you are slightly off
Neither, it's grid based and the grid cell is occupied so it's behaving as expected
oh, then no physics2D is really needed at all
So do you want grid-based movement or not
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
wait im confused on what u mean by grid based. so my movement relies on code and the dynamic setting in rigidbody doesnt actually do anything
You literally are not using a rigidbody at all
can a character stand in the middle of two cells
oh ye thats the movement i have
no
you're checking an adjacent location and then tweening to a specific spot
then you don’t need physics2D
that's grid based
It is fixed
the grid cell is occupied
so you don't move there
that's how it works
is there an alternative to it so that i can move between 2 spaces?
Yes, there's not using a grid-based movement system
Those are your two options. Use a grid or don't
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
hmmmm is it hard to set up?
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
i love this comparison 😭
this is my first project ever i just want to make a lil pixel game
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
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
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
is there a online tut u recommend
there is so much to know about Physics2D. I picked up info from a lot of places.
how long have you been programming for
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.
hmmm i guess i'll stick to grid for my first game and then learn 2dphysics
looking at your game, grid looks better
see that's how much of a beginner i am, idek what a array IS yet
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
yes thats true, not knowing much about programming i guess it's safer to go out of the way of things that can cause issues with my game and maybe even glitches
learn while you are in control of the situation
yes i am doing that currently i am on the 4th step (collisions)
thank you for the help and advice btw i am really grateful for it