Hey everyone,
If you can help me out with some hints on how to solve my problem, that would be awesome! I have tried many things but I can’t get the camera to follow a rolling ball through a loop on a track.
The problem happens when the ball starts to go up the steepest part of the loop. Smoothfollow.js sets a distance in the Z direction from the ball to the camera, but this Z direction is global, and so the camera stays too far away from the ball and doesn’t “follow” it upwards into the loop.
I already have a raycasthit straight down from the camera to see how high above the ground it is, and that works great. But I can’t seem to figure out how to maintain the distance between the camera and the ball along the Z axis… Look on the 5th line from the bottom of Smoothfollow.js, I tried setting the “height” to the transform (local) forward, but this makes the ball spin uncontrollably.
// transform.position -= currentRotation * transform.forward * distance; // This make the camera spin uncontrollably around the Y axis.
Thanks in advance!! I could also use a pre-made script for a racecar that can go through loops, or maybe a free-space flight script. I can’t seem to find either.
Smoothfollow.js
/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.
There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.
*/
// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function FixedUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// transform.position -= currentRotation * transform.forward * distance; // This make the camera spin uncontrollably around the Y axis.
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
_CameraViewHandler.cs
using UnityEngine;
using System.Collections;
public class _CameraViewHandler : MonoBehaviour
{
public GameObject ball = null;
public float distToCheckForHit = 1.9f;
private float WantedPosition = 0.0f;
// Quaternion angleStart = Quaternion.identity;
// float htStart = 0f;
// Use this for initialization
void Start ()
{
// angleStart = transform.rotation;
// SmoothFollow sf = (SmoothFollow)GetComponent(typeof(SmoothFollow));
// htStart = sf.height;
}
// Update is called once per frame
void FixedUpdate ()
{
Vector3 dirRayDown = -transform.InverseTransformDirection(Vector3.up);
RaycastHit hitDown;
if( Physics.Raycast( transform.position, dirRayDown, out hitDown, distToCheckForHit ) )
{
SmoothFollow sf = (SmoothFollow)GetComponent(typeof(SmoothFollow));
Debug.Log( sf.height + ", " + hitDown.distance + ", " + distToCheckForHit );
// sf.height = htStart - ( hitDown.distance - distToCheckForHit );
transform.localPosition = hitDown.point + (transform.InverseTransformDirection(Vector3.up) * sf.height);
}
else
{
// Debug.Log( sf.height + ", " + hitDown.distance + ", " + distToCheckForHit );
}
// transform.LookAt(ball.transform.position);
}
public void ChangeView( float _distance, float _height, float _heightDamping )
{
//SmoothFollow sf = GetComponent<SmoothFollow>();
SmoothFollow sf = (SmoothFollow)GetComponent(typeof(SmoothFollow));
sf.distance = _distance;
sf.height = _height;
sf.heightDamping = _heightDamping;
}
}