Hello there! I am having trouble with smooth movement for a 2D prefab object. In the example below, I am looking at a bullseye. The script is for a dot that moves between the rings of the bullseye. Ring1 is the innermost ring, and by pressing a button the dot should slowly move from ring1 to ring2. I am not sure why it is not moving. I am new to C#. Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour
{
[Header("Configuration")]
[SerializeField] bool player1;
[Header("Controls")]
[SerializeField] float rawSpeed = 1f;
[Header("Ring Positions")]
[SerializeField] float ring1;
[SerializeField] float ring2;
[SerializeField] float ring3;
[SerializeField] float ring4;
bool controlsLocked;
// Update is called once per frame
void Update()
{
Control();
}
void Control()
{
if (!controlsLocked)
{
if (player1)
{
if (transform.localPosition.y == ring1)
{
if (Input.GetAxis("P1_Horizontal") < -0.1)
{
controlsLocked = true;
Vector2 startPosition = transform.localPosition;
Vector2 targetPosition = new Vector2(transform.localPosition.x, ring2);
float calculatedSpeed = rawSpeed * Time.deltaTime;
transform.localPosition = Vector2.Lerp(startPosition, targetPosition, calculatedSpeed);
controlsLocked = false;
}
}
}
}
}
}