- Home /
My Player doesn't move anymore. I didn't change a thing in the script and it did work before. What's wrong?
I'm making a 2D game and so far everything worked. But all of a sudden, today, my Player wouldn't move anymore. I didn't change anything that the player had. Here is 'Player.cs': using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
[RequireComponent(typeof(PlayerMovement))]
[RequireComponent(typeof(Animator))]
public class Player : MonoBehaviour
{
[SerializeField]
private PlayerMovement pm;
private Animator anim;
private SpriteRenderer rend;
private PlayerCombat pc;
public int health = 5;
public int exp = 0;
public int currentEnemyCount;
public float speed;
public float walkSpeed = 5f;
public float runSpeed = 10f;
public float jumpSpeed = 7f;
public bool isGrounded;
void Start()
{
pm = GetComponent<PlayerMovement>();
anim = GetComponent<Animator>();
rend = GetComponentInChildren<SpriteRenderer>();
pc = GetComponentInChildren<PlayerCombat>();
Physics.IgnoreLayerCollision(10, 10, true);
}
void Update()
{
//Movement speed:
if(Input.GetKey(KeyCode.LeftShift))
{
speed = runSpeed;
}
else
{
speed = walkSpeed;
}
if(GameObject.Find("DialogManager").GetComponent<DialogManager>().GetActiveSelf() == false)
{
Movement();
//Jumping:
if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
Debug.Log("Jumping...");
pm.Jump(jumpSpeed);
}
}
//Escape to main menu:
if(Input.GetKeyDown(KeyCode.Escape))
{
//Make sure main menu is always scene number 0!!!
SceneManager.LoadScene(0);
}
}
void Movement()
{
float xMov = Input.GetAxis("Horizontal");
float zMov = Input.GetAxis("Vertical");
if(xMov != 0 || zMov != 0)
{
anim.SetBool("Moving", true);
}
else
{
anim.SetBool("Moving", false);
}
if(xMov < 0)
{
rend.flipX = true;
}
else if(xMov > 0)
{
rend.flipX = false;
Debug.Log("Moving right");
}
Vector2 movHorizontal = transform.right * xMov;
Vector2 movVertical = transform.forward * zMov;
Vector2 velocity = (movHorizontal + movVertical) * speed;
pm.MoveHorizontal(velocity);
}
public void ApplyDamage(int damage)
{
health -= damage;
if(health <= 0)
{
KillYourself();
}
else if(health == 1)
{
rend.color = Color.red;
}
else
{
Respawn();
}
}
public void GainExperiencePoints(int points)
{
exp += points;
}
public void CountEnemy()
{
currentEnemyCount--;
if(currentEnemyCount <= 0)
{
CompleteLevel(-1);
}
}
public void CompleteLevel(int nextLevel)
{
Debug.Log("Level Complete!");
Scene currentScene = SceneManager.GetActiveScene();
if(nextLevel == -1)
{
nextLevel = currentScene.buildIndex + 1;
}
SceneManager.LoadScene(nextLevel);
}
void KillYourself()
{
Destroy(gameObject);
GameObject HUD = GameObject.Find("HUDManager");
HUD.GetComponent<HUDManager>().ActivateDeathScreen();
}
void Respawn()
{
transform.position = new Vector2(0, 1);
}
void OnCollisionEnter2D(Collision2D collision)
{
GameObject obj = collision.gameObject;
if(obj.tag == "KillZone")
{
ApplyDamage(1);
}
else if(obj.tag == "ThrowablePickup")
{
pc.currentAmmo++;
}
}
void OnCollisionStay2D(Collision2D collision)
{
GameObject obj = collision.gameObject;
if(obj.layer == 8)
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D collision)
{
GameObject obj = collision.gameObject;
if(obj.layer == 8)
{
isGrounded = false;
}
}
}
And here is 'PlayerMovement.cs':
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private Player player;
private Rigidbody2D rb;
void Start()
{
player = GetComponent<Player>();
rb = GetComponent<Rigidbody2D>();
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
}
public void MoveHorizontal(Vector2 _velocity)
{
rb.MovePosition(rb.position + _velocity * Time.fixedDeltaTime);
}
public void Jump(float _thrust)
{
rb.AddForce(new Vector2(0, _thrust));
}
}
This code worked for me before, but it suddenly didn't. And I also have a sword that glitches: it flashes in two spots and moves weird. I put a script on it to follow the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(PlayerCombat))]
public class PlayerWeapon : MonoBehaviour
{
public float weaponDamage = 10f;
public float offSetX;
public float offSetY;
private GameObject player;
void Start()
{
player = GameObject.FindWithTag("Player");
Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
void Update()
{
float xaxis = player.transform.position.x;
float yaxis = player.transform.position.y;
transform.position = new Vector2(xaxis + offSetX, yaxis + offSetY);
//0.35f, 0.03f
}
void OnCollisionEnter2D(Collision2D collision)
{
GameObject obj = collision.gameObject;
if(obj.tag == "Enemy")
{
Debug.Log("Hit enemy!");
obj.SendMessage("ApplyDamage", weaponDamage);
}
}
}
Answer by Jwizard93 · Jul 05, 2017 at 10:28 PM
Are you sure movement() is being called? Maybe that if statement with the DialogManager is returning true.
Also you could just make the sword a child of the player then it's location would always just be an offset as it is relative to the player.
But if I make the sword a child of the player, colliders register the collision as the parent as well. Right now, if an enemy hits the sword, the enemy gets hit, but if the enemy hits the player's body, the player is hit. If I were to change it, the sword also counts as the player's body.
That wasn't the case. It still didn't work. Inside $$anonymous$$ovement() I have a Debug.Log that is shown when GetAxis is returned above 0.
Your answer
Follow this Question
Related Questions
Problem between my player jump and player movement 1 Answer
Problem with 2D movment system C#. Keeps moving when no command is given 0 Answers
Using MoveTowards on button click 0 Answers
How do I stop momentum in 2D sidescroller? 1 Answer
Move player between 3 given points only by touching on the left/right side of the screen 1 Answer