Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Camera zoom when collided with object

Discussion in 'Scripting' started by Bricksmashr10, Oct 5, 2023 at 1:48 AM.

  1. Bricksmashr10

    Bricksmashr10

    Joined:
    Thursday
    Posts:
    2
    Hello everyone, Do you know how in most 3D 3rd person video games the camera won't clip through a wall or floor, instead just zoom?
    I'm having trouble programming that. I have NO script and would like one and all the threads I've seen are really complex and I don't quite know what is going on.
    I try to look it up and all I get is zoom on command touch screen!
    I just want the zoom script, not the camera movement, I have that sorted. it revolves around having a parented object at the target rotating taking the camera with it.

    thank you in advance!:)
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    421
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraZoomBlock : MonoBehaviour
    4. {
    5.     float idealDistance;
    6.    
    7.     void Start()
    8.     {
    9.         idealDistance=Vector3.Distance(transform.parent.position,transform.position);
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         Vector3 target=transform.parent.position+transform.parent.up;
    15.         if (Physics.Raycast(target,transform.position-target,out RaycastHit hit,idealDistance))
    16.             transform.position=hit.point;
    17.         else
    18.             transform.position=Vector3.MoveTowards(transform.position,target+(transform.position-target).normalized*idealDistance,20*Time.deltaTime);
    19.     }
    20. }
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
    You don't program this anymore!

    Use Cinemachine. Find a tutorial that explains Cinemachine 3rd person follow camera with proper wall evasion.
    I think the 3rd Person Starter Asset on the store (made by Unity) also has camera collider evasion.
     
    Kurt-Dekker and Nad_B like this.
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    270
    I really don't understand why Unity does not advertise the awesome Cinemachine tool enough! It's one of the (rare) tools that really works very well and should become the standard in all Unity templates and documentation.
     
    Chubzdoomer and Kurt-Dekker like this.
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    421
    I think they're gonna show it at the next Superbowl.
     
    CodeSmile, Kurt-Dekker and Nad_B like this.
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    270
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,283
    In addition to being regularly featured at the Superbowl, there is also a dedicated forum:

    https://forum.unity.com/forums/cinemachine.136/

    If you absolutely insist on making your own camera controller, it is Really Hard(tm) to get it right.

    The simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

    Code (csharp):
    1. private Vector3 WhereMyCameraIsLocated;
    2. private Vector3 WhatMyCameraIsLookingAt;
    3.  
    4. void LateUpdate()
    5. {
    6.   cam.transform.position = WhereMyCameraIsLocated;
    7.   cam.transform.LookAt( WhatMyCameraIsLookingAt);
    8. }
    Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.

    Does nothing for collision, obviously.
     
    Nad_B likes this.
  8. Bricksmashr10

    Bricksmashr10

    Joined:
    Thursday
    Posts:
    2
    thanks everyone, I believe I can figure it out from here :D
     
  9. faUnity

    faUnity

    Joined:
    Aug 7, 2023
    Posts:
    13
    This depends on what you want to achieve...

    Personally i like violons and not LookAt(). It snaps in certain circumstances.