- Home /
how can i lock y position of a player when its being dragged?
i want to be able to drag my player over X axis and Z axis, like if it were sliding over the floor, but when i move it on the Z axis, because of the camera's position (orthographic and the ground looking like this a rhombus) when i drag the player through X axis it slides well over the ground, but on X axis it also goes uppwards, moving in a diagonal line that its center is the center of the ground.
here is my script:
using UnityEngine; using System.Collections;
public class drag : MonoBehaviour {
public Player player;
float distance = 10;
void OnTouchDrag(){
Vector3 mousePosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.z, distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
transform.position = objPosition;
}
void FixedUpdate()
{
if (player.transform.position.y != 0)
{
transform.position = new Vector3(0, 0, 0);
print(transform.position.y);
}
}
}
here is an image of the scene before: https://snag.gy/4hyMWV.jpg here is when i move it over the Z axis: https://snag.gy/xe8jNb.jpg and here is when i move it over the X axis: https://snag.gy/VR0TWH.jpg and again, when i move it over the X axis towards the other side: https://snag.gy/S94DaV.jpg
It seems its for some reason moving on the Y axis too, but i cant lock Y axis if im dragging the player :( please, i need help pretty urgent :( thank you a lot by the way.
Answer by Cherno · Jun 19, 2017 at 01:42 AM
Just set transform.position.y to 0 after all other movement has been applied.
I'm not sure when OnTouchDrag() is being called during a frame so you might have to set the y position in LateUpdate (or at the end of OnTouchDrag()).
Your answer
![](https://koobas.hobune.stream/wayback/20220612124435im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Lock player position till loading is finished 2 Answers
lock GUITexture on the screen at specific co - ordinates 3 Answers
Locking cursor without changing position 1 Answer
transform.position as variable to lock object 0 Answers
Forcing a rotation point 2 Answers