HOKAY, so here’s my issue. In the following code, I’m having an issue where I don’t want to use the smooth camera (mostly because I hate the look of it), so I found some nice code for a camera that “slides” into place whenever you turn. It really looks quite nice when you’re turning. Unfortunately, whenever you stop moving, the camera still attempts to slide into place, even though there’s nowhere to slide. This causes it to slowly flip-the-hell-out, shooting from side to side. It also has the issue that when you first get into car, it’s always facing to the side, rather than just automatically setting behind the car. Halp plz!
public class CarCamera : MonoBehaviour
{
public Transform target = null;
public float height = 1f;
public float positionDamping = 3f;
public float velocityDamping = 3f;
public float distance = 4f;
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 / 70.0f);
camera.fieldOfView = Mathf.Lerp(55, 72, speedFactor);
float currentDistance = Mathf.Lerp(7.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);
}
}
heh, yeah, setting iskinematic is equivalent to reattaching the camera. regarding the car's motor fix, you need be sure that rigidbodies have proper weights in your gameobject. One thing that's commonly ignored is that the MASS parameter is in kilograms, and if all your rigidbodies have 1 mass they actually weigh 1 kilogram! Of course you need to properly set all vehicle values to be sure that the engine has no trobule stabilizing. So you can try to set the wheels to, say, 2.5 mass, and the vehicle to 100 mass (docs suggest to avoid mass differences of more than 100 units).
– roamcelUhm, sorry but YOU brought up that name in the code of your question. That's what you posted: using UnityEngine; using System.Collections; public class ScriptableObjectName : ScriptableObject { public int VariableNeeded; } Note that you named your class "ScriptableObjectName". If that's not your name, you have to replace it with your actual class type name. That's just plain OOP.
– Bunny83