hello, i ame quite new to programming and am slowly learning.
So i have been following this tuturial to create a top down rpg like game.
at the end of this video everything worked fine accept for the fact that my player remains locked in place.
the animations and their directions work fime but the character wont actualy move.
after some reseach i think it has something to do with the line:
public void Move()
{
myRigidBody.velocity = direction.normalized * speed;
}
but after allot of trying i stil cant find a way to fix it,
hopefully someone can help me.
did you set “speed” to a value other than 0?
can we see the rest of your code (with code tags Using code tags properly )
character script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class character : MonoBehaviour {
[SerializeField]
protected float speed;
private Animator anim;
protected Vector2 direction;
protected virtual void update(){
HandleLayers ();
}
private Rigidbody2D myRigidBody;
public bool is_walking
{
get{
return direction.x != 0 || direction.y != 0;
}
}
protected virtual void Start ()
{
myRigidBody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator> ();
}
protected virtual void Update ()
{
HandleLayers ();
}
private void fixedUpdate()
{
Move();
}
public void Move()
{
myRigidBody.velocity = direction.normalized * speed;
}
public void HandleLayers ()
{
if (is_walking)
{
ActivateLayer("WalkingLayer");
anim.SetFloat ("x", direction.x);
anim.SetFloat ("y", direction.y);
}
else
{
ActivateLayer("IdleLayer");
}
}
public void AnimateMovement(Vector2 direction){
}
public void ActivateLayer (string layerName)
{
for (int i = 0; i < anim.layerCount; i++)
{
anim.SetLayerWeight (i, 0);
}
anim.SetLayerWeight (anim.GetLayerIndex (layerName), 1);
}
}
the player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : character {
[SerializeField]
private Stat health;
[SerializeField]
private Stat power;
private float initialHealth = 100;
private float initialPower = 50;
protected override void Start()
{
health.Initialize (initialHealth,initialHealth);
power.Initialize (initialPower,initialPower);
base.Start();
}
protected override void Update ()
{
GetInput ();
base.Update ();
}
private void GetInput()
{
direction = Vector2.zero;
if (Input.GetKey(KeyCode.W)){
direction += Vector2.up;
}
if (Input.GetKey(KeyCode.A)){
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.S)){
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.D)){
direction += Vector2.right;
}
}
}
there it is