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

Not sure how I integrate this page's script

Discussion in 'Editor & General Support' started by junctionboss, Oct 24, 2014.

  1. junctionboss

    junctionboss

    Joined:
    May 11, 2014
    Posts:
    227
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Apologize if I do not understand your question correct, but if you mean to copy the source code and paste it into your editor, than you should try using FireFox. The code is not well formed, but in different lines. Using IE the text is pasted as one line.
     
  3. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Still not sure if I got the point of your question, but I converted the JS code to C# and I can "compile" it in Unity without compiler errors. (Obviously there's a runtime error, because the target is not assigned, and also the defined private variables must be initialized with proper values), but here's the converted code:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. public class MainCameraScript : MonoBehaviour {
    6.  
    7.     private Transform target;
    8.     private float distance = 0.0f;
    9.     private float zoomSpeed = 1.0f;
    10.     private float distanceOffset = 0.0f;
    11.     private float xSpeed = 1.0f;
    12.     private float ySpeed = 1.0f;
    13.     private float x = 0.0f;
    14.     private float y = 0.0f;
    15.     private float yMinLimit = -20;
    16.     private float yMaxLimit = 80;
    17.  
    18.  
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.  
    23.         Vector3 relativePos = transform.position - target.position;
    24.         RaycastHit hit;
    25.  
    26.         if (Physics.Raycast(target.position, relativePos, out hit, distance + 0.5f))
    27.         {
    28.             Debug.DrawLine(target.position,hit.point);
    29.             distanceOffset = distance - hit.distance + 0.8f;
    30.             distanceOffset = Mathf.Clamp(distanceOffset, 0.0f, distance);
    31.         }
    32.         else
    33.         {
    34.             distanceOffset = 0.0f;
    35.         }
    36.  
    37.     }
    38.  
    39.     void LateUpdate ()
    40.     {
    41.  
    42.         // view zoom
    43.         distance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
    44.  
    45.         if (distance < 1.0)
    46.         {
    47.             distance = 1.0f; // max zoom
    48.         }
    49.         if (distance > 6.0)
    50.         {
    51.             distance = 6.0f; // min zoom
    52.         }
    53.  
    54.  
    55.         if (target)
    56.         {
    57.             x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    58.             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    59.  
    60.             y = ClampAngle(y, yMinLimit, yMaxLimit);
    61.  
    62.             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
    63.             Vector3 position = new Vector3(0.0f, 0.0f, -distance + distanceOffset) + target.position;
    64.  
    65.             transform.rotation = rotation;
    66.             transform.position = rotation * position;
    67.         } //<- if (target)
    68.  
    69.     } //<- void LateUpdate ()
    70.  
    71.    // Took this from another thread:
    72.     static float ClampAngle (float angle, float min, float max)
    73.     {
    74.         if (angle < -360)
    75.         {
    76.             angle += 360;
    77.         }
    78.         if (angle > 360)
    79.         {
    80.             angle -= 360;
    81.         }
    82.  
    83.         return Mathf.Clamp (angle, min, max);
    84.     }
    85.  
    86. }
    87.  
     
  4. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    1)
    NullReferenceException means that there's no instance assigned to the variable (PivotBasedCameraRig in this case, I guess).

    2)
    I only tried to make the code compilable. The target must be assigned at some time (otherwise you will also get a NullReferecneException at runtime).

    3)
    All the variables declared as private fields for the class must be initialized with values that are valid for your game. I have no idea if the initial values I choosed work properly for your situation, so you must adjust them if necessary.
     
  5. junctionboss

    junctionboss

    Joined:
    May 11, 2014
    Posts:
    227
    ARe you saying I need to alter pivotbasedcamerarig.cs ? Im confused why that even comes up error, or is it something that the script I edited depends on ?

    BY target, you mean the 3rd person charater that is assigned to camera in question ? ( by draggint it into var. field I presume?)

    #3 makes total senese, its the other ones Im not real sure about, and which are stumping me atm.

    PS.--I tried something that is helping a lot to reduce camera clipping through alls: ' protect camera from clipping through walls' script in unity 3d script section (add) , so I fooled around with some settings but while it did resolve some areas very well, Im stil having trouble with terrible clipping going through certain very low ceiling tunnels, as this picture show in image attached.

    Any idea what setting needs adjusted to stop clipping in these very tight spots ?





    thx
    jb
     

    Attached Files:

    Last edited: Nov 1, 2014
  6. junctionboss

    junctionboss

    Joined:
    May 11, 2014
    Posts:
    227
  7. junctionboss

    junctionboss

    Joined:
    May 11, 2014
    Posts:
    227