Hello.
I am making a 2D Game (little bit Zelda like) and want my Character to walk immediately.
For the Game I only use Cubes to show the textures (pixel art). I got my character on y position 1, objects it should collide with also on 1 and ground to walk over on 0 so i can use simple collisions. My character is freezed on all rotations and y position and I also use really high drag and angular drag because I don’t want to move the character without player input or other scripts.
It works great except one thing: The start of movement is delayed. If I want to move my char it takes some time before he moves but I want to move instant…
Hope anyone can help me…
Btw: Is there a way to make the movement smoother without beginning with a whole new movement script?
mfg Superwayne
Here is the movementScript:
using UnityEngine;
using System.Collections;
public class CharMovement: MonoBehaviour {
public Texture2D[] walkFrames; //Containing 8 Textures I designed for moving my character
bool animationActive = false; //Showing wether a animation is already playing or not
public float animationPlayTime = 0.3f; //The time between the frames when moving
public float walkRange = 0.1f; //The speed the character walks with
string direction = "up"; //The direction the char is looking
//Checking for Inputs
void Update () {
if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0){
if(!animationActive){
StartCoroutine("WalkAnimation"); //Starting the movement animation if there is no active animation
animationActive = true;
}
//I seperated the direction methode from the move methode so there is no problem
//when walking in two direction at the same time
ChangeDirection();
Move ();
}
}
//Here the movement happens by changing the transform position directly but by a amount multiplied with
//Time.deltaTime, so it looks almost smooth in the game
void Move(){
if(Input.GetAxis("Vertical") == 1){
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + walkRange * Time.deltaTime);
}
if(Input.GetAxis("Vertical") == -1){
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - walkRange * Time.deltaTime);
}
if(Input.GetAxis("Horizontal") == 1){
transform.position = new Vector3(transform.position.x + walkRange * Time.deltaTime, transform.position.y, transform.position.z);
}
if(Input.GetAxis("Horizontal") == -1){
transform.position = new Vector3(transform.position.x - walkRange * Time.deltaTime, transform.position.y, transform.position.z);
}
}
//Changing the direction the char is looking
void ChangeDirection(){
if(Input.GetAxis("Vertical") == 1){
transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
}
else if(Input.GetAxis("Vertical") == -1){
transform.rotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);
}
else if(Input.GetAxis("Horizontal") == 1){
transform.rotation = Quaternion.Euler(0.0f, 90.0f, 0.0f);
}
else if(Input.GetAxis("Horizontal") == -1){
transform.rotation = Quaternion.Euler(0.0f, 270.0f, 0.0f);
}
}
//The animation Coroutine
IEnumerator WalkAnimation(){
for(int i = 0; i <= 7; i++){
if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0){
gameObject.renderer.material.mainTexture = walkFrames*;*
* yield return new WaitForSeconds (animationPlayTime);*
* if(i == 7){animationActive = false;}*
* }*
* else{*
* animationActive = false;*
* gameObject.renderer.material.mainTexture = walkFrames[7];*
* break;*
* }*
* }*
* }*
*}*
**