I'm new to unity and I'm trying to follow this tutorial: https://www.youtube.com/watch?v=7iYWpzL9GkM&t=1600s
But when I do the code at 42:00 and compile my character wont move at all. I wont to believe I did everything exactly as the video explains but I cant figure out how to fix it. Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine;
//Take and handle input and movement from the character
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 1f;
public float collisionOffset = 0.05f;
public ContactFilter2D movementFilter;
Vector2 movementInput;
Rigidbody2D rb;
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate(){
//If movement input is not 0, try to move
if(movementInput != Vector2.zero){
bool success = TryMove(movementInput);
if(!success){
success = TryMove(new Vector2(movementInput.x, 0));
}
if(!success){
success = TryMove(new Vector2(0, movementInput.y));
}
}
}
private bool TryMove(Vector2 direccion){
//Check for Potencial Collisions
int count = rb.Cast(
direccion, // X and Y values between -1 and 1 that represent the direction from the body to look for collision
movementFilter, // The setting that determines where a collision can occur on such as layers to collide with
castCollisions, // Lists of collisions to store the found collisions into after the Cast is finished
moveSpeed = Time.fixedDeltaTime collisionOffset); // The amount to cast equal to the movement plus an offset
if(count == 0){
rb.MovePosition(rb.position * direccion * moveSpeed * Time.fixedDeltaTime);
return true;
} else{
return false;
}
}
void OnMove(InputValue movementValue){
movementInput = movementValue.Get<Vector2>();
}
}
If anyone can help I really appreciate it.
Input System: version 1.3.0 Visual Studio Code Editor: version 1.2.5 Test Framework: version 1.1.33 Unity: version 2021.3.7f1
CodePudding user response:
- Your should change direccion to direction in TryMove.
- You should make sure you have all your values in the inspector assigned to the according thing. eg. rb should be assigned to your character and your character should have the rigidBody attached to it.
Hope this helps, tell me if it still doesn't work.