I have gone back over a few parts of the class and for some reason I cannot move my player left and right. No issues when it came to up/down. I have looked at the colliders with no luck. Let me know what I can post to help with this
If you can post a screenshot of your code and that should help me solve your issue
Hi Rick,
Welcome to our community!
Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you already tried to add Debug.Logs to your code to see what is going on during runtime? And are there any error messages in your console?
@Christopher_Powell, no screenshots, please. Code as formatted text would be great. This way, we will be able to copy and paste lines if necessary. With screenshots, we would have to type everything, which will very likely introduce typos. Reading long texts on screenshots is also exhausting for many people.
Oh yeah my bad, must have just been a habit to say something like that, I also don’t mind reading through them, but from now on I’ll try to remember to ask for formatting.
I did some compares and I did not see anything out of place. The Debug Logs looked good
The code for the PlayerMovement.cs is
public class PlayerMovement : MonoBehaviour
{
Vector2 moveInput;
Rigidbody2D myRigidBody;
[SerializeField] float runSpeed = 10f;
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
Run();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidBody.velocity.y);
myRigidBody.velocity = playerVelocity;
}
}
Before I put in the code to limit the movement from going up and down (floating) that worked fine. I just can not get the player to move left and right.
Just for my sanity, do you have this line of code at the top of your script:
using UnityEngine.InputSystem;
Just because that’s a common mistake, also I believe in Tilevania Rick sets up an input system, id also make sure you have that set up as well.
Also, if you just want simple movement left and right without the input system here is a code sample I made:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float moveSpeed = 5f;
void Update()
{
float moveInput = Input.GetAxis("Horizontal");
transform.position += new Vector3(moveInput * moveSpeed * Time.deltaTime, 0f, 0f);
}
}
You can take note from that, however I recommend making sure your input system works, as I believe Rick has a video where he sets this up
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
Yes, I triple checked the import statements. I also went back through the videos a few times to make sure I did not miss anything and even re-did a few steps. I will try your code to see it that at least gets my player moving.
So I tried your code and my player is moving left and right with no issues. So now I have to see what I did wrong with the input system.
Alright, that’s nice, hopefully I can find something to, I have tilevania on my old laptop, so maybe I can power up and compare a little more
A small bug that happens sometimes,
When you play the game and there is an option to have the game in focused, unfocused, and maximized, sometimes I’ve noticed that if the game isn’t in focus the input system is glitchy, just a thought, if you haven’t already id just try having the game in focused mode
Just tried the focus mode with no luck. I might once again remove all input system elements and go back over the video(s) again. Not sure what I would have missed.
Can you put a screenshot the input settings just in case there is something small going on there, I feel like something is going over my head, hopefully either of us can figure this out
I have to walk away for a few but will keep beating my head on this.
Wow, I looked through and you have the same stuff as Rick, must be something really small, I would now check to see if you have the proper version, however I’m not able to see what exactly is going wrong, maybe @Nina or @bixarrio or another problem solver would be able to see it, I’m still going to try to figure this out
Ran your code and used your settings (that I can see here) and everything works fine. Can you show what your player’s layout looks like? In the hierarchy.
So I did this in my code
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
Debug.Log("Velocity " + myRigidbody.velocity);
}
When I click the up and down arrow keys I see:
(note, I get a -1.0) as well
But when I click the left and right arrow keys I only see:
So for some reason I am getting 0 velocity on the left and right arrows.