- Home /
Can anyone explain what the camera is doing?
I'm creating a scene where I can grab parts for building a character and just as I finished how I want my camera set up I noticed something weird. The object I have grabbed when pulled towards the camera, splits in 2. Can anyone explain why its doing this or how to fix it?
Here is a GIF I made of it happening. https://gyazo.com/d8fa629b5555dbe36db607a55a3e888e
This script is on every object I want to move with the mouse
Here is the specific code that deals with camera movement by scrollwheel
public Vector3 diff;
public float distanceWanted;
diff = Camera.main.transform.position - transform.position;
float curCameraDistance = Input.GetAxis("Mouse ScrollWheel");
transform.position = GetMouseWorldPos() + diff.normalized * distanceWanted;
if (curCameraDistance < 0f)
{
distanceWanted++;
}
else if (curCameraDistance > 0f)
{
distanceWanted--;
}
Answer by $$anonymous$$ · Sep 18, 2019 at 08:03 AM
Hey guys so I ended up solving this... It seems I am too persistent. I just ended up redoing my code (4 different ways) and got to this point. Here's the code if you were interested. Just a reminder to anyone who uses this. This script is on each GameObject you want to move.
//Set for base starting distance (Will be changed using scrollwheel)
public float distanceWanted = 30f;
//While the mouse is held down
private void OnMouseDrag()
{
//For scrolling
float curCameraDistance = Input.GetAxis("Mouse ScrollWheel");
if (curCameraDistance > 0f)
{
distanceWanted++;
}
else if (curCameraDistance < 0f)
{
distanceWanted--;
}
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceWanted);
Vector3 objPos = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPos;
}
Answer by Arano · Sep 18, 2019 at 08:21 AM
not 100% sure this is happening but it looks like you are going over "0" with your movement, effectively your block is trying to move from 1 to -1 each frame because you keep moving it 'towards' the camera and overshooting the 0 mark. u might want add some boundaries to your script to stop this from happening, have the movement stop when 'minDistance' is reached.
0 is the camera position (so this might not be 0,0,0) but explaining it with 1 , -1 and 0 will make most sense i think.
I tested that and the object was still about 10 unity units from the near plane of the camera when it happened. I think your on the right path though that it might somehow have been trying to move the object in between 2 points for some reason.