2D Rougelike Tutorial Part 10-Player Does Not Move
I finished the scripting on part 10 of the tutorial. However, when I tested the game in the game view, nobody moves, including the player. I was able to make the player move in part 9. However, I can't make the player move right now.
[CODE]
using UnityEngine; using System.Collections; using System;
public class Enemy : MovingObject { public int playerDamage;
private Animator animator;
private Transform target;
private bool skipMove;
protected override void Start()
{
animator = GetComponent<Animator>();
target = GameObject.FindGameObjectWithTag("Player").transform;
base.Start();
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
if (skipMove)
{
skipMove = false;
return;
}
base.AttemptMove<T>(xDir, yDir);
skipMove = true;
}
public void MoveEnemy()
{
int xDir = 0;
int yDir = 0;
if (Mathf.Abs(target.position.x - transform.position.x) < float.Epsilon)
yDir = target.position.y > transform.position.y ? 1 : -1;
else
xDir = target.position.x > transform.position.x ? 1 : -1;
AttemptMove<Player>(xDir, yDir);
}
protected override void OnCantMove<T>(T component)
{
Player hitPlayer = component as Player;
hitPlayer.LoseFood(playerDamage);
}
}[/CODE]
Is the player not supposed to move, or is it just my scripts fault?
Answer by greatness · Mar 27, 2016 at 05:24 PM
Answer: Attach a 2D Rigidbody component to the Player. Player had a 2D Box Collider but not a 2D Rigidbody.
This is the player script:
Player [code=CSharp]using UnityEngine; using System.Collections; using System; using UnityEngine.Scene$$anonymous$$anagement;
public class Player : $$anonymous$$ovingObject { public int wallDamage = 1; public int pointsPerFood = 20; public int pointsPerSoda = 10; public float LevelDelay = 1f;
private Animator animator;
private int food;
// Use this for initialization
protected override void Start () {
animator = GetComponent<Animator>();
food = A_Game_$$anonymous$$anager.instance.playerFoodPoints;
base.Start();
}
void Update()
{
if (!A_Game_$$anonymous$$anager.instance.playerTurns) return;
int horizontal = 0;
int vertical = 0;
horizontal = (int)(Input.GetAxisRaw("Horizontal"));
vertical = (int) (Input.GetAxisRaw("Vertical"));
if (horizontal != 0)
vertical = 0;
if (horizontal != 0 || vertical != 0)
Attempt$$anonymous$$ove<Wall>(horizontal, vertical);
}
private void OnDisable()
{
A_Game_$$anonymous$$anager.instance.playerFoodPoints = food;
}
protected override void Attempt$$anonymous$$ove<T>(int xDir, int yDir)
{
food -= 10;
base.Attempt$$anonymous$$ove<T>(xDir, yDir);
CheckIfGameOver();
A_Game_$$anonymous$$anager.instance.playerTurns = false;
}
private void CheckIfGameOver()
{
if (food <= 0)
A_Game_$$anonymous$$anager.instance.GameOver();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Exit"))
{
Invoke("Restart", LevelDelay);
enabled = false;
}
if (other.CompareTag("Food"))
{
food += pointsPerFood;
Destroy(other.gameObject);
}
else if (other.CompareTag("Soda"))
{
food += pointsPerSoda;
Destroy(other.gameObject);
}
}
// Update is called once per frame
protected override void OnCant$$anonymous$$ove<T>(T component)
{
Wall hitWall = component as Wall;
hitWall.DamageWall(wallDamage);
animator.SetTrigger("PlayerChop");
}
private void Restart()
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex);
}
public void LoseFood(int loss)
{
animator.SetTrigger("PlayerHit");
food -= loss;
CheckIfGameOver();
}
}[/code]
Your answer
Follow this Question
Related Questions
2D Rougelike Tutorial-Part 11 Player Does Not Move 0 Answers
How to make a Inventory Hotbar 0 Answers
UFO Tutorial, after pickup vid, my player ignores the wall collisions 0 Answers
Why won't my player move left? 0 Answers
roll the ball - player not moving. Script seems fine,Roll a ball not rolling, should I add speed ? 1 Answer