using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
Vector2 movementInput;
public float moveSpeed = 1f;
public float collisionOffset = 0.05f;
Rigidbody2D rb;
public ContactFilter2D movementFilter;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if(movementInput != Vector2.zero){
int count = rb.Cast(
movementInput,
movementFilter,
castCollisions,
moveSpeed * Time.fixedDeltaTime + collisionOffset);
if(count == 0)
{
rb.MovePosition(rb.position * moveSpeed * Time.fixedDeltaTime);
}
}
}
void OnMove(InputValue movementValue)
{
movementInput = movementValue.Get<Vector2>();
}
}
im like half way through some random youtube guide so dont just criticize it but like tell what’s really wrong in there
Sounds like it is time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
How to report your problem productively in the Unity3D forums:
This is the bare minimum of information to report:
- what you want
- what you tried
- what you expected to happen
- what actually happened, log output, variable values, and especially any errors you see
- links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
@Kurt-Dekker is right, you really need to explain what you expect to happen and what actually happens.
That said, I feel something is wrong with this line:
rb.MovePosition(rb.position * moveSpeed * Time.fixedDeltaTime);
You provably want it to be
rb.MovePosition(rb.position + movementInput * moveSpeed * Time.fixedDeltaTime);