- Home /
How to reset orbit camera distance?
Hey, I'm attempted to use one of the Orbit Camera scripts from the wiki. I modified the script a little to my needs I'm encountering an annoyance that I tried to fix, but I guess I'm doing it the wrong way.
Basically, I'm trying to make the script store the distance(between the camera and the player) it had before the raycast occurred in a variable, and then set the distance back to that variable.
if (Physics.Linecast(target.position, transform.position, out hit))
{
// resetCam was given a value of 'false' at Start().
resetCam = true;
if (resetCam)
oldDist = distance;
distance -= hit.distance;
}
else
{
if (resetCam)
{
distance = oldDist;
resetCam = false;
}
}
EDIT: If you need me to provide you with more parts of the source code, please just ask.
Please don't give me the full code. I just need an explanation, or just a direction to follow, although SOME code would be welcome. If you could, please explain why my modification wasn't working, and what I did wrong.
Thank you, SeeSharp.
I don't know how the code is being called. If it is called (and successfully hits something) more than once before 'oldDist' is used, then 'oldDist' will be overwritten by the current distance and therefore not available on line 13 when you need it. This might work:
if (!resetCam)
oldDist = distance;
distance -= hit.distance;
resetCam = true;
If you have a reference to the player(target) and reference to the camera(this), why no just do some vector math to get the distance between the two ins$$anonymous$$d of Calling Physics.Linecast(). Just do something like: Vector3 dist = (target.transform.position - transform.position).magnitude
If you have a large number of objects with this script it will increase performance.
Thank you Ed. I actually saw that method somewhere else and tried using it, but it didn't really go so well. Still, thank you for contributing to my question.
Answer by SeeSharp · Apr 17, 2014 at 09:42 PM
As robertbu commented:
I don't know how the code is being called. If it is called (and successfully hits something) more than once before 'oldDist' is used, then 'oldDist' will be overwritten by the current distance and therefore not available on line 13 when you need it. This might work:
if (!resetCam)
oldDist = distance;
distance -= hit.distance;
resetCam = true;
Your answer
Follow this Question
Related Questions
Need help on making player move towards gameobject, if clicked 1 Answer
Move Character to touched Position 2D (Without RigidBody and Animated Movement) 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Raycast only works right next(practically on top of) object 0 Answers