Object appears to glitch as it moves whenever the camera follows it.
Okay so this Question will seem a bit to big tackle, but I figured I'd ask for some help anyway.
So I am making a simple kart racing, and right now I'm I've got a good set on the physics of the car. Instead of using real physics, It relies on raycasting to influence its' movement simply as follows.
" By firing a raycast down from each corner of the vehicle a certain distance (hoverheight), and if it detects a hit, it adds an upward force."
It took a bit of brain work, but I think it I got the cars' controller script to work properly. The real problem is when it interacts with the camera.Now for the camera, I used a script I made based on this smooth follow script that I found on the UnifyCommunity website.
now, when the camera views the player without the script, the player acts normally; smooth movement, no problems. However when the camera as the script, and when you move the player in game mode, that's when things get a bit problematic. As the camera views the player move around, the player glitches a bit. Not enough to break the game of course, though just enough for it to be noticed.
If anyone has any ideas, questions, or suggestions, be sure to reply or comment back on the matter. Just be sure to do some research and think over the question thoroughly. The coding for the scripts I'm using will be provided in this question for you to look over, in case you like to test out this scenerio for yourself. I'll also provide a link to the tutorial I used for making my character script, If your interested in making your own simple Kart racer. Think of said tutorial as a basis for more your own gameplay mechanics. Again, your words and support are well appreciated.
Thank you
Character Script
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour
{
Rigidbody body;
float deadZone = 0.1f;
public float m_groundedDrag = 3f;
public float maxVelocity = 50;
public float hoverForce = 1000;
public float gravityForce = 1000f;
public float hoverHeight = 1.5f;
public GameObject[] hoverPoints;
public float forwardAcceleration = 8000f;
public float reverseAcceleration = 4000f;
float thrust = 0f;
public float turnStrength = 1000f;
float turnValue = 0f;
public ParticleSystem[] dustTrails = new ParticleSystem[2];
int layerMask;
void Start()
{
body = GetComponent<Rigidbody>();
body.centerOfMass = Vector3.down;
layerMask = 1 << LayerMask.NameToLayer("Vehicle");
layerMask = ~layerMask;
}
// Uncomment this to see a visual indication of the raycast hit points in the editor window
// void OnDrawGizmos()
// {
//
// RaycastHit hit;
// for (int i = 0; i < hoverPoints.Length; i++)
// {
// var hoverPoint = hoverPoints [i];
// if (Physics.Raycast(hoverPoint.transform.position,
// -Vector3.up, out hit,
// hoverHeight,
// layerMask))
// {
// Gizmos.color = Color.blue;
// Gizmos.DrawLine(hoverPoint.transform.position, hit.point);
// Gizmos.DrawSphere(hit.point, 0.5f);
// } else
// {
// Gizmos.color = Color.red;
// Gizmos.DrawLine(hoverPoint.transform.position,
// hoverPoint.transform.position - Vector3.up * hoverHeight);
// }
// }
// }
void Update()
{
// Main Thrust
thrust = 0.0f;
float acceleration = Input.GetAxis("Vertical");
if (acceleration > deadZone)
thrust = acceleration * forwardAcceleration;
else if (acceleration < -deadZone)
thrust = acceleration * reverseAcceleration;
// Turning
turnValue = 0.0f;
float turnAxis = Input.GetAxis("Horizontal");
if (Mathf.Abs(turnAxis) > deadZone)
turnValue = turnAxis;
}
void FixedUpdate()
{
// Do hover/bounce force
RaycastHit hit;
bool grounded = false;
for (int i = 0; i < hoverPoints.Length; i++)
{
var hoverPoint = hoverPoints [i];
if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out hit,hoverHeight, layerMask))
{
body.AddForceAtPosition(Vector3.up * hoverForce* (1.0f - (hit.distance / hoverHeight)), hoverPoint.transform.position);
grounded = true;
}
else
{
// Self levelling - returns the vehicle to horizontal when not grounded and simulates gravity
if (transform.position.y > hoverPoint.transform.position.y)
{
body.AddForceAtPosition(hoverPoint.transform.up * gravityForce, hoverPoint.transform.position);
}
else
{
body.AddForceAtPosition(hoverPoint.transform.up * -gravityForce, hoverPoint.transform.position);
}
}
}
var emissionRate = 0;
if(grounded)
{
body.drag = m_groundedDrag;
emissionRate = 10;
}
else
{
body.drag = 0.1f;
thrust /= 100f;
turnValue /= 100f;
}
// Handle Forward and Reverse forces
if (Mathf.Abs(thrust) > 0)
body.AddForce(transform.forward * thrust);
// Handle Turn forces
if (turnValue > 0)
{
body.AddRelativeTorque(Vector3.up * turnValue * turnStrength);
} else if (turnValue < 0)
{
body.AddRelativeTorque(Vector3.up * turnValue * turnStrength);
}
// Limit max velocity
if(body.velocity.sqrMagnitude > (body.velocity.normalized * maxVelocity).sqrMagnitude)
{
body.velocity = body.velocity.normalized * maxVelocity;
}
}
}
Camera Controller
using UnityEngine;
using System.Collections;
public class CameraSmoothFollow : MonoBehaviour {
public Transform target;
// Use this for initialization
public float distance;
public float height;
public float damping;
public bool smoothRotation;
public bool followBehind;
public float rotationDamping;
// Update is called once per frame
void Update () {
Vector3 wantedPosition;
if(followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}
}