I’m not 100% sure your question. Are you just asking how you would start this code to transform your Image?
Also how do you want the program to start the transformation. The user clicks a button somehwere? or the user clicks this actual Image object?
The Follow code assumes you want to have the transformation happen when you click the Image, and this script is attached to that Image:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class BookMarkMovement : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public Vector3 pointB;
// We need to have a blank Down Handler because Unity won't
// Send Click up events if we don't handle the down
public void OnPointerDown(PointerEventData eventData)
{
}
public void OnPointerUp(PointerEventData eventData)
{
// Your code didn't show where you got PointA from.. PointB was set by the editor
StartCoroutine(this.transform, PointA, pointB, 3.0f);
}
//Alternately if Some Object outside of this needs to start the transformation
//They can hook up to this function
public void StartMoveObject(Vector3 PointA,float time)
{
StartCoroutine(this.transform, PointA, pointB, time);
}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i = 0.0f;
var rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
}
This code is inside of image. I want is, when i clicked the image, it will go up, then when i clicked it again, it will now go down, back to its position.
Ok I modidfied the code a bit. This script should be attached to your bookmark:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class BookMarkMovement : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
//set the up and down position, as well as time to slide in the editor;
public Vector3 UpPosition;
public Vector3 DownPosition;
public float slideTime;
bool isDown; // are we in the up or down position
bool isMoving; // ignore Mouse clicks if we are in the middle of a slide;
void Start()
{
isDown = true;
isMoving = false;
}
// We need to have a blank Down Handler because Unity won't
// Send Click up events if we don't handle the down
public void OnPointerDown(PointerEventData eventData)
{
}
public void OnPointerUp(PointerEventData eventData)
{
if (isMoving)
return;
StartCoroutine(MoveObject());
}
IEnumerator MoveObject()
{
float currentTime = 0.0f;
currentTime += Time.deltaTime;
isMoving = true;
float lerpTime;
while (currentTime < slideTime)
{
// make sure we don't shoot past
if (currentTime > slideTime)
currentTime = slideTime;
// normalize our currentime into a 0-1 range for the Lerp
lerpTime = currentTime / slideTime;
if (isDown)
transform.position = Vector3.Lerp(DownPosition, UpPosition, lerptime);
else
transform.position = Vector3.Lerp(UpPosition, DownPosition, lerpTime);
yield return null;
}
isMoving = false;
isDown = !isDown;
}
}
The book marks needs to be fed an up and down position.
It keeps track of its own status, and moves up or down as necessary
it also ignores any clicks while its in the middle of moving.
I also changed your time from a consant 3.0f, to a slideTime variable you can adjust
I noticed I had a syntax error , one of the lerptime should be lerpTime, but I assume you fixed that.
Are you setting slideTime in the editor?
Try adding this code to check if your positions you set are correct. Depending on your camera, canvas, etc sometimes the positions can get modified by some internal scale. Highlight the bookmark in your editor so you can see its transform and start the game with the following code added. Watch where its positions are set.
// In Start add this line at the end of the function
StartCoroutine(CheckPositions());
// then add this co-routine to the script
IEnumerator CheckPositions()
{
float currentTime = 0.0f;
isMoving = true;
Debug.Log("Moving Object to the Down Position");
transform.position = DownPosition;
Debug.Log("Waiting 3 seconds");
while (currentTime < 3.0f)
{
currentTime += Time.deltaTime;
yield return null;
}
Debug.Log("Moving Object to the Up Postion");
transform.position = UpPosition;
isMoving = false;
}