[noob] I have a script that theoretically should manage movement of a 2D GameObject...
Before I get to the question, I am an ultra-level noob, and this is my first post.
I'm probably breaking about a million best practice rules and misunderstanding many basic principles. Any corrections, whether about the code or the question, would be greatly appreciated
I have this script, which I thought would move a gameobject one unit slowly enough to show an animation. I have a 16x32 pixel sprite attached to the GameObject and the pixel-units ratio is set to 16p to 1 unit.
However, I am getting a CS0161 on line 16 of the code. I googled the error code, but I was unable to figure exactly how the problem worked and how to fix it.
If someone wouldn't mind looking at this and explaining why this doesn't work I would greatly appreciate it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour {
//Animation Delay
public float animFrameDelay = .05f;
//Adds a simple(r) method of adding a delay
IEnumerator WaitFunc(float time)
{
yield return new WaitForSeconds(time);
}
//moves the object
IEnumerator ObjectMover(float x, float y, float z)
{
int i = 0;
while (i < 16)
{
Vector3 temp = new Vector3(x, y, z);
transform.position += temp;
WaitFunc(animFrameDelay);
i++;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
bool up = Input.GetKey(KeyCode.UpArrow);
bool down = Input.GetKey(KeyCode.DownArrow);
bool left = Input.GetKey(KeyCode.LeftArrow);
bool right = Input.GetKey(KeyCode.RightArrow);
float TileSize = .0625F;
if (up)
{
StartCoroutine(ObjectMover(0, TileSize, 0));
}
if (down)
{
StartCoroutine(ObjectMover(0, -TileSize, 0)); ;
}
if (left)
{
StartCoroutine(ObjectMover(-TileSize, 0, 0));
}
if (right)
{
StartCoroutine(ObjectMover(TileSize, 0, 0));
}
}
}
Answer by hooseeg · Jun 23, 2018 at 06:21 PM
Ok, this was a mess, sorry about the spaghetti everyone.
I wound up using Vector3.MoveTorwards and Time.DeltaTime