Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

System for returning camera to original position.

Hello.

I am currently in the process of making a camera collision system. I was able to implement the part of bringing the camera to the hit position from a couple raycasts each leading from the camera's near view frustum corners. Surprisingly though, I am actually having quite some trouble with bring the camera back to it's original position.

The issue with just returning it to the original position after it isn't detecting collisions anymore is that it will go back, and then instantly be behind the wall again and try to go forward again, rinse and repeat.

I tried to counter this by sending a raycast behind the camera to the position it's trying to go to, and have it just go the position it hits (or if it doesn't hit anything, then it can go back to the original position).

This works for the most part, except for one issue. When the camera is right next to the corner of a game object, it sort of breaks. The camera will go forward in front of the object, but once it has, it is in a position where it doesn't see anything behind it. So it will go back to the original position, but then it sees the corner again and tries to go forward again!

I've tried a couple different things to counter this, such as only going back if it doesn't hit anything again, but it hasn't worked.

I've been on this for a bit, and have about reached the peak of my problem solving abilities. I may just be thinking too hard, but I can't think of a better solution to fix this issue. If anyone has any suggestions, they would be greatly appreciated.

Visualization for my current system. The square is the camera, the circle is the player, and the lines are the rays:


Comments

  • jtok4jjtok4j Member

    Greetings,

    Looking at this portion: ". The camera will go forward in front of the object, but once it has, it is in a position where it doesn't see anything behind it. So it will go back to the original position, but then it sees the corner again and tries to go forward again!"

    How about not moving the camera to the front of the object, but nearly to the front of the object. Perhaps take the coords that the camera is moving towards, and shave off a little (subtract) from the final destination, so it's not exactly in front of the object? I'm not sure if this accomplishes your purpose or whether it moves the camera enough.

    Also, not sure if you simply have the camera moving back to it's original set in stone location or a generic reversal of the original movement forward?

     Keep on Creating! 


    Justin of JustinTime Studios (http://unity3d.expert) 

  • @jtok4j

    Thanks for the response! I appreciate it.

    That wouldn't exactly fix what I'm trying to do. Not at all your fault, I'll try and clarify my system a bit better.

    I have two raycasts: one from the camera to the player (I'll call this the collision ray), and one from the camera to where it would normally be (I'll call this the return ray). Also, when I say "where it would normally be", all that really changes is the z-axis. When there is a wall between the player and the camera, the camera will go forward in front of the wall. But when going back to it's "original position", all I really mean is moving the camera back on the z. My camera orbits the player, so I don't have a set in stone position. I only have a set in stone z-axis value.

    Now the return ray goes from my camera to where it should be (it's really just pos.x, pos.y, desiredZLocation). So if it's already desiredZLocation away from my player on the z, there won't be any ray. I'll try and illustrate this:

    Hopefully my terrible art skills show what I'm saying.

    Anyways, onto why it actually wouldn't work. It really just has to do with the camera being at the edge of the corner:

    So in the first one, the collision ray hits something. In the second, it goes in front of the hit. But now you can see the return raycast going back behind it. And since there isn't anything behind it, it goes back to it's original position in the third one. So basically, my system only works well half of the time.

    So even if I didn't go all the way to the hit and made an offset, it still would not see anything behind it. If you have any other suggestions, I would greatly appreciate them. And thank you again.

Sign In or Register to comment.