- Home /
Any ideas as to why I get this error 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;
private void Awake()
{
camera.transform.parent = target;
}
private void FixedUpdate()
{
Vector3 wantedPosition = target.TransformPoint(0, height, -distance);
// check to see if there is anything behind the target
RaycastHit hit;
Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward);
// cast the bumper ray out from rear and check to see if there is anything behind
if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
&& hit.transform != target) // ignore ray-casts that hit the user. DR
{
// clamp wanted position to hit position
wantedPosition.x = hit.point.x;
wantedPosition.z = hit.point.z;
wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
}
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
Vector3 lookPosition = target.TransformPoint(targetLookAtOffset);
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);
}
}
this is a combination of two scripts ,im trying to mix the Car Camera script from the Unity Car tutorial with a Terrain and object aware script I found on unity wiki ,but I get this error .Any ideas please ?
Assets/Scripts/CSharpScripts/CarCamera.cs(57,23): error CS1547: Keyword `void' cannot be used in this context
Ideally I would just love to make the smoothfollowcamerawithbumper script follow the car with some side to side movement like the first script ,if that makes sense (you turn left right you see the sides of the car while turning ) ins$$anonymous$$d of a fixed rigidly to the back view ..Probably something obvious im missing :P
I added as an answer because my brain is truly fried after abouy two months trying to sort this out :(
Answer by T27M · Dec 09, 2012 at 01:32 AM
You missed a closing bracket on the first FixedUpdate(). You also have FixedUpdate() twice which I think is not possible, unless you meant that to be Update().
Answer by Bunny83 · Dec 09, 2012 at 01:33 AM
Sure, Your FixedUpdate function is missing it's closing bracket. So the compiler sees that you declared your Start function inside another function but that's not possible.
Next thing is you have two FixedUpdate functions. That's also not possible. just merge them into one by taking the content of one of them and paste it inside the other.
Btw, what's the point of merging two scripts? The great thing about components is that you can keep them simple and just attach them to the object you want. If they do different things, keep them seperated, but that's up to you.
I want the soothfollow camera off the car camera script with the terrain aware function of the other script ,if i try to attach both to the camera the car camera makes the second script non functional ,thats why im trying (very unsuccesfully lol) to merge the two
im practically begging now lol this is just evading me ,yet I believe it should be simple :(
Could anyone tell me if its at least possible ,what im trying or am I goona be banging my head against a brick wall for even longer lol ? please :)
Answer by javanoob · Dec 10, 2012 at 04:10 AM
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;
private float bumperDistanceCheck = 2.5f; // length of bumper ray
private float bumperCameraHeight = 1.0f; // adjust camera height while bumping
private float damping = 5.0f;
private Vector3 bumperRayOffset; // allows offset of the bumper ray from target origin
private Vector3 targetLookAtOffset; // allows offsetting of camera lookAt, very useful for low bumper heights
private bool smoothRotation = true;
private float rotationDamping = 10.0f;
void Start()
{
raycastLayers = ~ignoreLayers;
}
void FixedUpdate()
{
currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
currentVelocity.y = 0;
prevVelocity = currentVelocity;
Vector3 wantedPosition = target.TransformPoint(0, height, -distance);
// check to see if there is anything behind the target
RaycastHit hit;
Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward);
// cast the bumper ray out from rear and check to see if there is anything behind
if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
&& hit.transform != target) // ignore ray-casts that hit the user. DR
{
// clamp wanted position to hit position
wantedPosition.x = hit.point.x;
wantedPosition.z = hit.point.z;
wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
}
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
Vector3 lookPosition = target.TransformPoint(targetLookAtOffset);
if (smoothRotation)
{
Quaternion wantedRotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else
transform.rotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
}
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);
}
}
heres my mix up of the two scripts but i am totally losing the terrain collison thing now :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Distant terrain level of detail question. 0 Answers
Multiple Cars not working 1 Answer
Best way to Camera Pick multiple objects and move them together 2 Answers
camera collision visibility (c#) 2 Answers