How to translate GameObject dependng of screen width ?
Hi, I am beginer in Unity and i don't know why my script don't work ? My GameObject is UI image :
using UnityEngine;
using System.Collections;
public class Translate : MonoBehaviour {
public float vecteur;
public float movespeed;
void Start()
{
vecteur = Screen.width * 1 / 3;
}
// Update is called once per frame
void Update () {
//Right
if (Input.GetKeyDown("d"))
{
transform.Translate(new Vector3(vecteur, 0, 0) * movespeed * Time.deltaTime);
}
//Left
if (Input.GetKeyDown("q"))
{
transform.Translate(new Vector3(-vecteur, 0, 0) * movespeed * Time.deltaTime);
}
}
}
When I press ''d'' the GameObject go anywhere on the x axe. Help me, please, thanks !
Comment
When I press ''d'' the GameObject go anywhere on the x axe. Help me, please, thanks !
Do you mean the GameObject doesn't move on the x axis?
Start by logging some variables to see what is going on. Before line 20, add:
print( "vecteur: " + vecteur);
print( "movespeed: " + movespeed);
print( "x: " + transform.x);
That should give you a clue.