Search Unity

camera slips down when following player!!

Discussion in '2D' started by freedom667, Dec 20, 2015.

  1. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    I tried many scripts but nothing works. I will go crazy. what can i do for this?? when my player holding to rope main camera slips down.

    Please take a look: https://sendvid.com/mziuem3u

    Follow Script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var cameraTarget : GameObject; // Inspector> Assign the Camera Target NON è il target della camera ma la posizione che vuole raggiungere la camera
    4.  
    5. var smoothTime : float = 0.1;              // Delay in seconds to follow Player
    6. var cameraFollowX : boolean = true;        // Inspector> if is checked -> The Camera will follow X position of cameraTarget
    7. var cameraFollowY : boolean = true;        // Inspector> if is checked -> The Camera will follow Y position of cameraTarget
    8. var cameraFollowHeight : boolean = false;  // if true the Camera Y Position = cameraHeight
    9. var cameraHeight : float = 2.5;            // cameraHeight
    10. var velocity : Vector2;
    11. var offset : float;
    12. private var thisTransform : Transform;  
    13.  
    14. function Start ()
    15. {
    16.    thisTransform = transform;
    17. }
    18.  
    19. function Update ()
    20. {
    21.  
    22. if (cameraFollowX) // if cameraFollowX = true = Inspector is checked
    23. {
    24.    thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x + offset, velocity.x, smoothTime);
    25. }
    26.  
    27. if (cameraFollowY) // if cameraFollowY = true = Inspector is checked
    28. {
    29.    thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
    30. }
    31.  
    32. if (!cameraFollowY && cameraFollowHeight)     // if cameraFollowY = false = Inspector is unchecked AND cameraFollowHeight = true = Inspector is checked
    33. {
    34.    GetComponent.<Camera>().transform.position.y = cameraHeight; // The Camera Y position = cameraHeight
    35. }
    36.  
    37. }
     
  2. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    anybody help?? Unity employees please.
     
  3. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    heey this is important. please
     
  4. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Maybe you can send me a zip with part of your project in it. Just looking at the code sample you posted, I can't see anything wrong.
     
  5. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    http://www.filedropper.com/deneme

    Wait 1 minute hanging on rope.
     
  6. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Cool. I'll check it out on Tuesday when I get back from vacation. In the meantime, if anybody else wants to take a look at it, feel free.
     
    theANMATOR2b likes this.
  7. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    what did you do??
     
  8. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Hi. My flights got cancelled so I'm delayed getting home. Hopefully I can look at it in a few days. Thanks
     
  9. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    soo. i found a solution but i have to convert JS to C# but i couldn't do this. can you do it for me?? the script is above.
     
  10. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.  
    6.     public GameObject cameraTarget; // Inspector> Assign the Camera Target NON è il target della camera ma la posizione che vuole raggiungere la camera
    7.     float smoothTime = 0.1f;              // Delay in seconds to follow Player
    8.     bool cameraFollowX = true;        // Inspector> if is checked -> The Camera will follow X position of cameraTarget
    9.     bool cameraFollowY = true;        // Inspector> if is checked -> The Camera will follow Y position of cameraTarget
    10.     bool cameraFollowHeight = false;  // if true the Camera Y Position = cameraHeight
    11.     float cameraHeight = 2.5f;            // cameraHeight
    12.     Vector2 velocity;
    13.     float offset;
    14.     private Transform thisTransform;
    15.  
    16.     void Start ()
    17.     {
    18.         thisTransform = transform;
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.  
    24.         if (cameraFollowX) { // if cameraFollowX = true = Inspector is checked
    25.             thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x + offset, ref velocity.x, smoothTime);
    26.         }
    27.  
    28.         if (cameraFollowY) { // if cameraFollowY = true = Inspector is checked
    29.             thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, ref velocity.y, smoothTime);
    30.         }
    31.  
    32.         if (!cameraFollowY && cameraFollowHeight) {     // if cameraFollowY = false = Inspector is unchecked AND cameraFollowHeight = true = Inspector is checked
    33.             Camera.main.transform.position.y = cameraHeight; // The Camera Y position = cameraHeight
    34.         }
    35.     }
    36. }
     
  11. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    it doesn't work. gives error this in 25,29,33

    error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
     
  12. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Wherever you are trying to set a new transform, you should use a Vector3. For example, you would change this code..
    Code (CSharp):
    1. thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x + offset, ref velocity.x, smoothTime);
    2.  
    ...to instead look like this...

    Code (CSharp):
    1. float myX = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x + offset, ref velocity.x, smoothTime);
    2. Vector3 myV = new Vector3(myX,thisTransform.position.y,thisTransform.position.z);
    3. thisTransform.position = myV;
     
  13. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    others? 29th and 33th line?
     
  14. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Wherever you are trying to set a new transform, you should use a Vector3.
    29th line... are you trying to set a new transform? YES... so use a Vector3.
    33rd line... are you trying to set a new transform? YES... so use a Vector3.
     
  15. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    i used vector 3 but Y-axis doesn't work i dont understand. nevermind 33rd line.
     
  16. Hussy030

    Hussy030

    Joined:
    Jan 7, 2016
    Posts:
    1
    Useful Coding Ideas.. Working Good interesting..