- Home /
Carcamera script raycasting fail ?
Can anyone point out why the Unity car tutorial carcamera raycasting fails please ?
using UnityEngine;
using System.Collections;
public class CarCamera : MonoBehaviour
{
public Transform target = null;
public float height = 1f;
public float positionDamping = 3f;
public float velocityDamping = 3;
public float distance = 50f;
public LayerMask ignoreLayers = -1;
private RaycastHit hit = new RaycastHit();
private Vector3 prevVelocity = Vector3.zero;
private LayerMask raycastLayers = -1;
private Vector3 currentVelocity = Vector3.zero;
void Start()
{
raycastLayers = ~ignoreLayers;
}
void FixedUpdate()
{
currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
currentVelocity.y = 0;
prevVelocity = currentVelocity;
}
void LateUpdate()
{
float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 170.0f);
camera.fieldOfView = Mathf.Lerp(55, 72, speedFactor);
float currentDistance = Mathf.Lerp(45.5f, 6.5f, speedFactor);
currentVelocity = currentVelocity.normalized;
Vector3 newTargetPosition = target.position + Vector3.up * height;
Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
newPosition.y = newTargetPosition.y;
Vector3 targetDirection = newPosition - newTargetPosition;
if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
newPosition = hit.point;
transform.position = newPosition;
transform.LookAt(newTargetPosition);
}
}
I am wondering why the raycasting doesn't stop the camera going through the terrain or other objects on this script ,anyone have any ideas ,I presume because its the Unity car tut it works for everyone else ,I have been stuck with the same issue for months now no matter what camera scripts I try ,please can anyone tell me whats wrong as I dont want to have to make all race tracks flat lol.
I'm guessing it's because your setting the camera too close to the collision point. Try: newPosition = hit.point + hit.normal;
Camera still goes underground,I am completely stumped with this,can I ask does it work for you ? Im getting a 1 in a million error maybe :(
A lot of people suggest this approach ins$$anonymous$$d musing Terrain sample height ?
height = Terrain.activeTerrain.SampleHeight(transform.position) + Terrain.activeTerrain.transform.position.y;
Would it be this obvious and forget raycasting ,which just doesnt seem to work for me anyway ?
Is there one Unity Dev on here that could at least tell me if this is possible or is it not hence the Unity Car Tutorial camera will always go through the floor ,I have serched and tried fixing this for six months and I just cannot seem to get a working solution ?
I actually emailed the Unity $$anonymous$$m and guess what ,they sent me back here lol ,Is Camera versus Terrain just not possible ,I cannot seem to find one tutorial that mentions this anywhere ? Are all unity games destined for the camera to drop through the floor ,please please someone tell me if it is at least possible so I dont waste any more time trying to achieve the impossible .
Everything is possible, EVERYTHING. Nothing is impossible, it just depends on how long you are willing to spend on doing anything. So, yes it is possible. Can you describe in more detail as to what happens, and it what situations you arn't getting the result you are looking for?
Answer by javanoob · Mar 24, 2013 at 03:12 AM
Got it :) used the terrain height command ,obvious really lol ,I am such a Javanoob :p
Your answer
Follow this Question
Related Questions
Unity Car Tutorial Error 0 Answers
UNEXPECTED TOKEN ) 1 Answer
Unity Car tutorial Brake 1 Answer
Unity Car Tutorial Error 0 Answers
Question about rigidbody velocity 1 Answer