Why does my cube not move?
I thinking I'm doing something simple here, that I understand, but it don't work. And I've been trying for days.
I have a button with a letter in a cube. When the button is clicked it changes color and the cube sinks in to the wall.
I've tried a tons of different code variants - placing it in different places in the script, using bools, ieumerators , .moveToward .translate etc. It changes color - but it wont move. I'm not a pro at all but I know that atleast some of my tries should work so I'm thinking its something else. Probarbly something pretty simple that I forgot and can't find. I do wan't the cube to move slowly into the wall - In this last try, showed in the script. I just tried to change the position, just to see if it would happen - but no. It printes Klick (click in swedish) so the bool and if statement works.
I'm doing it this way, and not with animation, because when this button work I want to duplicate it and do the whole alphabet placing letters on different positions on the x and y axis. My plan is to use the same script on them all and not do 21 different animations.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AnimateButton : MonoBehaviour
{
// Letterboxes
public Material newMat1;
public Material newMat2;
public float speed = 0.5f;
public float currentX;
public float currentY;
public Vector3 target;
public GameObject cube;
public GameController2 foundALetter;
public Text buttonText;
public string buttonTextToString;
public bool isClicked = false;
public Button button;
// Start is called before the first frame update
void Start()
{
currentX = gameObject.transform.position.x;
currentY = gameObject.transform.position.y;
target = new Vector3(currentX, currentY, -42);
}
// Update is called once per frame
void Update()
{
if (isClicked == true)
print("klick");
{
transform.position = new Vector3(currentX, currentY, -42f);
isClicked = false;
// StartCoroutine(moveTo(new Vector3(currentX, currentY, -42f)));
}
}
public void AnimateTheButton()
{
isClicked = true;
// transform.localPosition= Vector3.MoveTowards(transform.position, target, Time.deltaTime * speed);
//StartCoroutine(moveTo(target));
//aAnim.SetBool("isPressed", true);
if (foundALetter.foundLetter == true)
{
cube.GetComponent<MeshRenderer>().material = newMat1;
}
else if (foundALetter.foundLetter == false)
{
cube.GetComponent<MeshRenderer>().material = newMat2;
}
}
/*IEnumerator moveTo(Vector3 pos)
{
transform.position = Vector3.MoveTowards(transform.position, pos, speed * Time.deltaTime);
yield return null;
} */
}