The game I am working on features a top-down angle for movement. Anyway, I can’t seem to get the speed of movement to change at all - I’ve tried setting my speed value to 1, 5, 2000, none of which have had an impact on how fast the character moves. I wonder, what am I doing wrong?
As you can see in the code, the movement speed of the character is supposed to change based off of the tile under their feet, but as I can’t even get movement speed to change I’m not sure if this part of the code is working or not.
using UnityEngine;
using System.Collections;
public class CharacterMovementController : MonoBehaviour
{
public float speed = 5f; //determines the maximum movement speed of the character.
void Update()
{
Movement ();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Water1")
speed = 2f;
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name == "Water1")
speed = 5f;
}
void Movement()
{
if (Input.GetKey (KeyCode.A))
transform.Translate (Vector3.left * speed * Time.deltaTime);
if (Input.GetKey (KeyCode.W))
transform.Translate (Vector3.forward * speed * Time.deltaTime);
if (Input.GetKey (KeyCode.D))
transform.Translate (Vector3.right * speed * Time.deltaTime);
if (Input.GetKey (KeyCode.S))
transform.Translate (Vector3.back * speed * Time.deltaTime);
}
}