Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Abacab · Mar 24, 2015 at 12:42 PM · c#cameraraycasthitwall

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image El Maxo · Mar 24, 2015 at 02:04 PM 0
Share

$$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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Abacab · Mar 24, 2015 at 03:19 PM 0
Share

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?

avatar image sprawww · Mar 24, 2015 at 03:29 PM 0
Share

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.

avatar image Cherno · Mar 26, 2015 at 11:38 AM 0
Share

Check the Unity API for Physics.Raycast, there's an example that explains the basics.

avatar image ProperDev · Jan 20 at 06:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast hitting objects to the left of my player 1 Answer

RaycastHit and Camera through walls 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

RE: RaycastHit and camera through walls 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges