# moving cue after rotated

i have billiard cue and i want to move this cue forward and backward
by moving mouse up and down while holding key “s”.
i have this code work if i don’t rotate my object but the problem
my cue is rotated 270 in Y axis.

``````if(Input.GetKeyDown("s"))
{
tempVal = this.transform.localPosition.z - Camera.main.transform.position.z;
}

if (Input.GetKey("s"))
{
fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, tempVal));
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, fingerPos.y);
}
``````

And what happen if i use this snippet code, the direction of the cue forward and backward are correct if i move the mouse up and down the cue move backward-forward, but the cue position go far away from its first position and not seen in the main camera.
My question is how to make this cue stay close with its first position ?

Your cue z position are moved to fingerPos.y that have deepness equal tempVal.
That’s why you have to use cue origin position to make adjusment.

For example you have Transform transTemp that have equal position with your cue:

``````        if(Input.GetKeyDown("s"))
{
tempVal = this.transform.localPosition.z - Camera.main.transform.position.z;
fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, tempVal));
/* use this for recognizing origin position before we move it */
transTemp.position = fingerPos;
}

if (Input.GetKey("s"))
{
fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, tempVal));this.transform.localPosition = new Vector3(transTemp.localPosition.x + fingerPos.y, this.transform.localPosition.y, this.transform.localPosition.z);
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, transTemp.position.z + fingerPos.y)
}
``````