Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Taylord303 · Aug 03, 2014 at 02:21 PM · cameraraycastzoom

How to move the camera closer to the player while there is an object between them?

I'm trying to find a way to move the camera closer to the player while there is an object between the two, then move the camera back to the default distance from the player when the object is no longer in the way. I've translated the mouse orbit script to c#, and then adapted it to what I needed, so I'll post that here. My attempt at this camera effect that I'm trying is in there, DistanceAdjustment. If anyone could help me that would be great. Thanks. using UnityEngine; using System.Collections; [AddComponentMenu("Controls/CameraControls")] public class CameraControls : MonoBehaviour { public Transform player; public float distance = 1; public float xSpeed = 1; public float ySpeed = 1; public float yMin = -20; public float yMax = 80; private float x = 0.0f; private float y = 0.0f; void Start () { Vector3 angles = transform.eulerAngles; x = angles.x; y = angles.y; } void LateUpdate () { DistanceAdjustment(); if(player) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMin, yMax); Quaternion rotation = Quaternion.Euler(y,x,0); Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + player.position; transform.rotation = rotation; transform.position = position; } } void DistanceAdjustment () { RaycastHit rayHit; if(Physics.Raycast(transform.position, Vector3.forward, out rayHit, 15)) { if(rayHit.collider.tag == "Buildings") { distance -= rayHit.distance; } else { print("no hits"); } } } static float ClampAngle (float angle,float min, float max) { if(angle < -360) { angle += 360; } if(angle > 360) { angle -= 360; } return Mathf.Clamp (angle,min,max); } }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 03, 2014 at 03:21 PM

Here is a bit of addition to your code. Not sure this will be enough when you get among buildings. You may have to raycast multiple times to prevent clipping on some building, but anyway this gives you a start.

 using UnityEngine;
 using System.Collections;
 [AddComponentMenu("Controls/CameraControls")]
 public class CameraControls : MonoBehaviour {
 
 public Transform player;
 public float unobstructedDist = 8.0f;
 public float distSpeed = 1.25f;
 
 public float xSpeed = 90;
 public float ySpeed = 90;
 
 public float yMin = -20;
 public float yMax = 80;
 
 private float x = 0.0f;
 private float y = 0.0f;

 private float distance;
 private float desiredDist;
 
 
 void Start () {
     distance = unobstructedDist;
     desiredDist = unobstructedDist;
     Vector3 angles = transform.eulerAngles;
     x = angles.x;
     y = angles.y;
 }
 
 void LateUpdate () {
     DistanceAdjustment();
     distance = Mathf.Lerp (distance, desiredDist, Time.deltaTime * distSpeed);
     if(player) {
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
         
         y = ClampAngle(y, yMin, yMax);
         
         Quaternion rotation = Quaternion.Euler(y,x,0);
         Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + player.position;

         transform.rotation = rotation;
         transform.position = position;
     }
 }
 
 void DistanceAdjustment () {
     RaycastHit rayHit;
     if(Physics.Raycast(transform.position, transform.forward, out rayHit, 15)) {
         if(rayHit.collider.tag == "Buildings") {
             desiredDist = unobstructedDist - rayHit.distance;
         }
         else {
             desiredDist = unobstructedDist;
         }
     }
 }
 
 static float ClampAngle (float angle,float min, float max) {
     if(angle < -360) {
         angle += 360;
     }
     if(angle > 360) {
         angle -= 360;
     }
     return Mathf.Clamp (angle,min,max);
 }
 }

I've added 'unobstructedDist' and 'desiredDist' and a bit of code that Lerps between current distance (distance) and desiredDist. Note your Raycast was using Vector3.forward and needed to use transform.forward.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Raycast misses after camera zoom 2 Answers

Camera rotate to look at GameObject from Raycast 3 Answers

Keep Player + Party and/or Targeted Enemies in Camera Focus 1 Answer

Avoid/Minimize Sprites extrapolation in 2D while camera changes size 0 Answers

Raycast hitting objects to the left of my player 1 Answer


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