Question by
Benbojangles · Apr 10, 2017 at 06:54 PM ·
c#buggamemovement scriptenemy ai
Vertical Movement Is Wierd
So, I have some code to make an enemy go up and down between two defined walls, but it is very jittery. The code works for the horizontal movement, and it is almost the same. Could someone help me fix it?
using UnityEngine; using System.Collections;
public class Enemy1MoveVertival : MonoBehaviour {
public float walkSpeed = 1.0f; // Walkspeed
public float wallUp = 0.0f; // Define wallUp
public float wallDown = 5.0f; // Define wallDown
float walkingDirection = 1.0f;
Vector2 walkAmount;
float originalY; // Original float value
// Use this for initialization
void Start () {
wallUp = transform.position.y - 2.5f;
wallDown = transform.position.y + 2.5f;
}
// Update is called once per frame
void Update () {
walkAmount.y = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.y >= wallUp) {
walkingDirection = -1.0f;
} else if (walkingDirection < 0.0f && transform.position.y <= wallDown) {
walkingDirection = 1.0f;
}
transform.Translate(walkAmount);
}
}
Comment