I’m writing my own SmoothFollow camera script because the default one doesn’t work. It has the wrong orientation, and changing transform.forward to transform.right makes it even worse. Anyway.
Right now I have it to where the camera follows and LookAt’s the car, but when the car turns, I want the camera to get behind the car, smoothly.
Here’s some pictures to illustrate. When the car has no rotation, the camera is effectively behind the car, but only because of the lack of rotation. When I turn, I want to smoothly transform the camera from its position to a new position behind the camera.
I’ve tried lots of things. Making the camera a child of the car isn’t what I’m looking for, because then you can’t see side to side rotation because the camera is stiffly behind the car. I’ve tried using the car’s transform.right in all sorts of ways, but that doesn’t seem to get me anywhere.
Here’s the script.
void LateUpdate ()
{
Vector3 currentPos = targetCar.transform.position;
float currentHeight = this.transform.position.y;
//wantedHeight should be the height of the car's position plus some distance
float wantedHeight = targetCar.transform.position.y + ht;
//lerp from currentHeight to wantedHeight
currentHeight = Mathf.Lerp(currentHeight,wantedHeight,damp * Time.deltaTime);
//add a distance between the car's position and the camera's desired position, in the x coordinate because
//of how the x/z axes on the cars are reversed
Vector3 wantedPos = new Vector3(currentPos.x - dist,currentHeight,currentPos.z);
//finally, set the camera's position to its desired position
this.transform.position = wantedPos;
this.transform.LookAt(targetCar.transform);
}
Yeah, but it has problems as well. Looks at the side of the car instead of the back, because of how the car’s x and z axes are switched. Would try to play around with it, but its use of velocity confuses me.
But now that you reminded me, its functionality is kinda what I’m trying to mimic.
Dumb question, have you tried using the Standard Assets Smooth Follow script? I believe it does exactly what you are looking for.
edit…
Ah, I see what your getting at. I was developing camera scripts a while back. Here is what I came up with in a nutshell.
I wanted a camera that would staticly stay at a distance, but still do the same stuff that the standard smooth follow camera is. To do this, I had to force the distance. Simply add these two lines to the end of the LateUpdate in the standard SmoothFollow script:
Well see, by default SmoothFollow uses Vector3.forward, but because of how my car’s axes are switched, I need to use Vector3.right (or I guess transform.right), but when I change it, it just kinda…spastically rotates around the car.
Putting that at the end doesn’t seem to do anything. It would work if I could just get the camera behind the car inside of on its right.
I think I understand what you’re doing, maybe. I tried multiplying the car’s rotation by its transform.right, but that didn’t work out. Would doing something like that fix this?
Hmmm… Put an empty game object on the car, make it’s forward face the car’s forward and then have the SmoothFollow script follow it, instead of the car’s orientation.
Ok, so just to make sure I am getting this, the car is facing the wrong axis once you put it in unity? So the side faces the front? Have you tried rotating it in your modeling software to face the proper direction and then re-importing to unity?
Yeah, I would rotate it in the modeling software, but I’m not the one who modeled it, and I’ve set up everything else to work with its orientation, so I’d have to change all of that.
A scripting alternative is to put the model on an empty game object and have that object control everything, not the model. That way no matter how the car was modeled, it would not affect the actual in game scripting.
I should probably remember that, I have orientation problems all the time. Not unsolvable ones, but ya know, stuff doesn’t go in the direction intended. Thanks again.
Hey as long as you can get it fixed Just thought it could be the issue since I have done that a few times. Just the other day I had a bird flying at me sideways and it was caused by his orientation in blender. Just something to keep in mind for next time. Good luck.