- Home /
Problem is not reproducible or outdated
Why is my y-axis bugging?
Hi, all!
I'm currently working on an assignment from my university and can't figure this following thing out.
In my project, I have a sphere that follows my mouse. When I touch a cube, it's selected and I want to perform transformations on it. When I hold the left mouse click, I want to move the cube.
The following script comes from the update() in my sphere:
Vector3 spherePosition = transform.position;
// Modify position
if (Input.GetMouseButton(0))
{
Debug.Log("Current Sphere position: " + spherePosition);
selected[0].transform.position = spherePosition;
}
The "selected" is a Cube-element.
Now the issue is that, although I'm not moving the mouse, the y-axis is acting weird every other frame. I jumps to much lower values so that I sometimes loose it in the camera fram when I let go from the left mouse click:
I could not help myself with googling so thanks in advance for any tips!
Can you post the code where you are doing the screen space (mouse coordinates) transform to world space (the 3d space)?
Oh, maybe this could be the issue! Our tutor told us to check the script but till now I didn't know, why. :D
The current update-function from the followMouse script is the following:
void Update()
{
Vector3 pos = Input.mousePosition;
pos.z = 1;
transform.position = Camera.main.ScreenToWorldPoint(pos);
}
The camera however is not on (0, 0, 0) which is why we have a little disorted view on the games coordinate system. However, we want the sphere to actually move on the x- and y-axis from the camera-view and point into the space (z-axis). We got told that we should take a look at the "ScreenToWorldPoint"- and "LookAt"-function but this didn't help me yet.
Thanks in advance for any help!
Check first that you don't have duplicate "touch" component in scene. You can modify your debug line as follows:
Debug.Log("Current Sphere position: " + spherePosition + " Instance ID: " + this.GetInstanceID());
If you get different instance id numbers then you have "extra" component in the scene and need to remove it.
Answer by MarekRimal · Dec 03, 2021 at 02:17 PM
Then you have to provide the code where u are changing the sphere position. You said that it should follow the mouse, where is the code responsible for that?