Hello forum!
(this is my first thread)
I don’t understand why this code don’t works in a .cs file:
transform.eulerAngles = Vector3(transform.rotation.x, transform.rotation.y, zRotation);
thanks
Hello forum!
(this is my first thread)
I don’t understand why this code don’t works in a .cs file:
transform.eulerAngles = Vector3(transform.rotation.x, transform.rotation.y, zRotation);
thanks
You forgot the “new” keyword.
Also, are you sure you didn’t mean this?
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, zRotation);
Thanks a lot Smag
now I’m facing a new problem:
I have a car and a track with some parabolic curves (90°)
I need a camera that follow the car and rotate (z axis) when the car is on the parabolic curve
this code taken from CarTutorial works fine
I added the code to rotate z axis of the camera, however the camera goes out of the car’s center
here’s my code:
using UnityEngine;
using System.Collections;
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()
{
if (target.root.rigidbody.velocity.magnitude > 0.1) {
float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 70.0f);
camera.fieldOfView = Mathf.Lerp(31, 41, 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);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, target.eulerAngles.z);
}
}
}
what I’m missing?
thanks
Is the camera supposed to stay exactly in the same position exactly behind the car? If so, it might be easier to parent the camera to the car and just vary its distance with the car’s speed. If you want a more sophisticated following behaviour then can you describe exactly how you want it to work?
Yes, I need the camera to stay exactly in the same position exactly behind the car’s center even if the car will rotate around it’s z axis. This line transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, target.eulerAngles.z);
do the work but camera doesn’t stay behind car’s center (example: in a parabolic “left curve” the camera goes too much left, too much low and off the track).