- Home /
Vector3 wont move to right position
I tried to make a ui image move to a certain position if its clicked and then move back to its original position if its clicked again but when i click it, It goes to the completely wrong position. Anyone know the solution?Heres the script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ObjectiveScript : MonoBehaviour {
public GameObject objectives;
public Transform end;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
bool move;
int movs;
void Update () {
if (move == true){
Vector3 targetPosition = end.TransformPoint(new Vector3(0, 0, 0));
objectives.transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
}
public void Movement(){
movs += 1;
if (movs%2 == 0) {
move = false;
} else {
move = true;
}
}}
If you're using a UI image, the transform is not what you see in the editor since UI elements use Rect Transforms (which is a different system).
You can reference the Rect Transform by adding: using UnityEngine.UI at the beginning of your script.
Hope this helps!
No sorry this didnt help. I have using unity.UI at the beginning of my script but thanks anyway.
I meant using that at the beginning, then write the code to move the Rect Transform ins$$anonymous$$d of the Transform...
What do you mean use it at the beginning? Sorry Im a noob at unity.
I edited my script to show you where i think the using unity.ui goes