- Home /
Zoom Camera when it hits a wall (edited)
Hello!
I'd like my camera to zoom in when it hits a wall so that the camera won't go through the wall. How would this be accomplished with RaycastHit?
Something like tag all the walls as 'Wall' and when the RaycastHit.point hits a 'Wall'-tagged object it zooms forward. I have no idea how to code it though.
EDIT:
using UnityEngine;
using System.Collections;
public class CarCameraScript : MonoBehaviour
{
public Transform car;
public float distance = 6.4f;
public float height = 1.4f;
public float rotationDamping = 3.0f;
public float heightDamping = 10.0f;
public float zoomRatio = 0.5f;
public float defaultFOV = 60f;
private Vector3 rotationVector;
void LateUpdate ()
{
float wantedAngle = rotationVector.y;
float wantedHeight = car.position.y + height;
float myAngle = transform.eulerAngles.y;
float myHeight = transform.position.y;
myAngle = Mathf.LerpAngle (myAngle, wantedAngle, rotationDamping * Time.deltaTime);
myHeight = Mathf.Lerp (myHeight, wantedHeight, heightDamping * Time.deltaTime);
Quaternion currentRotation = Quaternion.Euler (0, myAngle, 0);
transform.position = car.position;
transform.position -= currentRotation * Vector3.forward * distance;
Vector3 temp = transform.position; //temporary variable so Unity doesn't complain
temp.y = myHeight;
transform.position = temp;
transform.LookAt (car);
CompensateForWalls (rotationVector, ref temp);
}
void FixedUpdate ()
{
Vector3 localVelocity = car.InverseTransformDirection (car.rigidbody.velocity);
if (localVelocity.z < -0.5f) {
Vector3 temp = rotationVector; //because temporary variables seem to be removed after a closing bracket "}" we can use the same variable name multiple times.
temp.y = car.eulerAngles.y + 180;
rotationVector = temp;
} else {
Vector3 temp = rotationVector;
temp.y = car.eulerAngles.y;
rotationVector = temp;
}
float acc = car.rigidbody.velocity.magnitude;
camera.fieldOfView = defaultFOV + acc * zoomRatio * Time.deltaTime; //he removed * Time.deltaTime but it works better if you leave it like this.
if (CarTouchControls.reverseOn == true || CarTouchControls.currentSpeed < 0) {
rotationDamping = 0f;
} else {
rotationDamping = 3.0f;
}
}
void OnDrawGizmos ()
{
}
private void CompensateForWalls (Vector3 fromObject, ref Vector3 toTarget)
{
Debug.DrawLine (fromObject, toTarget, Color.green);
RaycastHit wallHit = new RaycastHit ();
if (Physics.Linecast (fromObject, toTarget, out wallHit)) {
Debug.DrawRay (wallHit.point, Vector3.left, Color.red);
toTarget = new Vector3 (wallHit.point.x, toTarget.y, wallHit.point.z);
}
}
}
This is not working. Can you tell if I am getting close?
CompensateForWalls() is from a different tutorial than everything else.
$$anonymous$$y advice would be to ask this on the forums,
It is a area that is better for discussion, this one is more awnsers
Answer by Cherno · Mar 24, 2015 at 01:43 PM
The Standard Assets package has a camera script that handles this kind of situations.
If you want to write it yourself, jsut cast a ray from the object the camera is following towards the camera, and if it hits anything, move teh camera to the hit.point.
I am really inexperienced with OnDrawGizmos and RaycastHit.
void OnDrawGizmos() {
RaycastHit hit;
Gizmos.color = Color.green;
Gizmos.DrawRay (car, transform.position);
}
How to include the hit variable and move the camera?
I suggest you read up on a few tutorials. No one here will code the whole thing for you. If you hit a specific roadblock, ask again and I'll gladly help you.
Check the Unity API for Physics.Raycast, there's an example that explains the basics.
I know this is many years late, but I wanted to say thank you. I been raycasting from the camera with no luck, it never occurred to me raycasting from the player. This is so simple, but wanted you to know, thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
RaycastHit and Camera through walls 1 Answer
Distribute terrain in zones 3 Answers
RE: RaycastHit and camera through walls 0 Answers