For context, I was watching a tutorial for a visual novel. Mine is a pixel art visual novel in a 1920x960 resolution. I’m bothered that my float values for my character sprite’s movements are in the hundreds float targetX = left ? -8 : 300 compared to his being in only single digit numbers float targetX = left ? -8 : 8. I had mine like those just to see the change.
Can someone explain why do I have to set those numbers that high? Thank you!
Here’s the script with the MoveCharacter function:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class CMD_DatabaseExtension_Examples : CMD_DatabaseExtension //Serve as an example for how to add future commands to the command system like function or argument for movement
{
new public static void Extend(CommandDatabase database) //All Commands must be STATIC
{
//Add action with no parameters
database.AddCommand("print", new Action(PrintDefaultMessage));
database.AddCommand("print_1p", new Action<string>(PrintUsermessage));
database.AddCommand("print_mp", new Action<string[]>(PrintLines));
//Add lambda with no parameters
database.AddCommand("lambda", new Action(() => { Debug.Log("Printing a default message to console from lambda command."); }));
database.AddCommand("lambda_1p", new Action<string>((arg) => { Debug.Log($"Log User Lambda Message: '{arg}'"); }));
database.AddCommand("lambda_mp", new Action<string[]>((args) => { Debug.Log(string.Join(", ", args)); }));
//Add coroutines with no parameters
database.AddCommand("process", new Func<IEnumerator>(SimpleProcess));
database.AddCommand("process_1p", new Func<string, IEnumerator>(LineProcess));
database.AddCommand("process_mp", new Func<string[], IEnumerator>(MultiLineProcess));
//Special Example
database.AddCommand("moveCharDemo", new Func<string, IEnumerator>(MoveCharacter));
}
private static void PrintDefaultMessage()
{
Debug.Log("Printing a default message to console.");
}
private static void PrintUsermessage(string message)
{
Debug.Log($"User Message: '{message}'");
}
private static void PrintLines(string[] lines)
{
int i = 1;
foreach (string line in lines)
{
Debug.Log($"{i++}. '{line}'");
}
}
private static IEnumerator SimpleProcess()
{
for (int i = 1; i <= 5; i++)
{
Debug.Log($"Process Running... [{i}]");
yield return new WaitForSeconds(1);
}
}
private static IEnumerator LineProcess(string data)
{
if (int.TryParse(data, out int num))
{
for (int i = 1; i <= num; i++)
{
Debug.Log($"Process Running... [{i}]");
yield return new WaitForSeconds(1);
}
}
}
private static IEnumerator MultiLineProcess(string[] data)
{
foreach (string line in data)
{
Debug.Log($"Process Message: '{line}'");
yield return new WaitForSeconds(0.5f);
}
}
private static IEnumerator MoveCharacter(string direction) //timestamp 29:00 ung function na to
{
Debug.Log($"Starting MoveCharacter Coroutine with direction: {direction}"); // Add this to log direction
bool left = direction.ToLower() == "left";
Transform character = GameObject.Find("Image").transform;
float moveSpeed = 400; //original 15
float targetX = left ? -8 : 300; //formerly -8 : 8
float currentX = character.position.x;
Debug.Log($"Moving character from {currentX} to {targetX}"); // Log target and current position
while (Mathf.Abs(targetX - currentX) > 0.1f) //original 0.1f
{
currentX = Mathf.MoveTowards(currentX, targetX, moveSpeed * Time.deltaTime);
character.position = new Vector3(currentX, character.position.y, character.position.z);
yield return null;
}
Debug.Log("MoveCharacter Coroutine finished."); // Log when coroutine finishes
}
}
Edit: Even with the moveSpeed float is set to 300. If it wouldn’t be an issue just as along as it’s running, please let me know. thanks!