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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#] How To Reduce Distance Between Objects?

Discussion in 'Scripting' started by SwaggyMcChicken, Jan 9, 2016.

  1. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    I've developed a 3rd person camera controller similar to traditional RPG camera controls. The camera is placed a little bit behind the player and had the chance of colliding with walls and objects. I've done some research on how to fix it and a lot of people say "Make a raycast go from the player to the camera and reduce distance if there's a collision" which is a great starting point but I'm completely confused on how I'd go about reducing the distance. I've attempted to look up tutorials or other answers based on this but I cannot find anything. Does anyone know how? I'd appreciate it!
     
  2. Svarr

    Svarr

    Joined:
    Dec 20, 2015
    Posts:
    22
    From the top of my head I would say that you'd have to subtract the camera position with the difference of the positions of the parent and the raycast hit.
    In pseudo code it looks like this: (assuming you have the camera as a child of the player game object)
    Code (CSharp):
    1. public Camera myCam; // if this script is attached to the camera, you don't need this variable
    2. private Vector3 camOffset;
    3.  
    4. void Start()
    5. {
    6.     // store the initial camera position relative to the parent GO
    7.     camOffset = mycam.GetParent().transform.position - mycam.transform.position;
    8. }
    9.  
    10. void Update()
    11. {
    12.     if (raycast between camera and player gets a collision) // never used raycast...
    13.     {
    14.         Vector3 hit = collision;
    15.         Vector3 newCampos = camOffset - (mycam.GetParent().transform.position - hit);
    16.         myCam.transform.Translate(newCampos); // if that doesn't work, you probably have to add the property Space.World right after newCampos
    17.     }
    18. }
     
    SwaggyMcChicken likes this.
  3. Mutur01

    Mutur01

    Joined:
    Dec 28, 2015
    Posts:
    24
    You can use the function "OnCollisionEnter()" to detect collisions and "Vector3.MoveTowards()" ito get the camera closer to the player (this script should be attached to the camera object):

    Code (CSharp):
    1. public float speed;
    2. public transform character;
    3.  
    4. void OnCollisionObject(Collision col){
    5.  
    6. transform.position = Vector3.MoveTowards(transform.position, character.position, speed * Time.deltaTime);
    7.  
    8. }
    That should works. Have a good day !
     
    SwaggyMcChicken likes this.
  4. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108

    Thanks, I didn't realize that function existed!

    Thanks a lot man, I appreciate the help! Moments like these make me a little sad, because how was I supposed to figure this out on my own? Lol.