And second script is similiar, only there is "Vector2.right"
difference
That's the problem right there.
You are moving the object left with ("Object").transform.Translate(Vector2.left * szybkosc);
then you have another script that is moving it right with ("Object").transform.Translate(Vector2.right * szybkosc);
.
What do you expect to happen? I think that the "Object" GameObject should remain still or even create a weird behavior. You can't be moving one Object to two opposite directions at the-same time.
Maybe the second script is supposed to be moving another GameObject GameObject.FindGameObjectWithTag("AnotherObject")
instead of the first GameObject the first script is already moving....
Not related to your problem but you should call GameObject.FindGameObjectWithTag
once in the Start function.
GameObject objToMove;
void Start()
{
objToMove = GameObject.FindGameObjectWithTag("Object");
}
void Update()
{
if (Input.GetMouseButton(0))
{
if (objToMove.transform.position.x > -1.9)
{
objToMove.transform.Translate(Vector2.left * szybkosc);
}
}
}
EDIT:
I'm making control system. I have two arrows - left arrow and right
arrow. When I touch and hold left arrow, object moves left, when I
touch and hold right arrow, object moves right.
It's good to mention what you are doing in your question. This is what EventSystems is used for. OnPointerClick
, OnDrag
and OnEndDrag
are used to make such things. You can find more about this here.
In fact, you need a Visual Joystick. Use Unity's CrossPlatformInputManager from the Asset store and that should do it. It will save you so much time of having to make your own with OnPointerClick
and OnDrag
.
The value from CrossPlatformInputManager.GetAxisRaw("Horizontal")
can then be used to move your Object left or right.