Search Unity

2nd y Game Designer, First Unity Game and needs help

Discussion in 'Scripting' started by CrashKonijn, Mar 4, 2010.

  1. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Hello,

    I'm Peter (18 ) and I'm currently in the seccond year of Game Design in Groningen (Netherlands, if my English sucks; that why). Since Unity is given free our teacher figured out that we should start using the Unity engine for our 3D projects. In the previous project we had to create a 3D character and let it walk in the Unity engine, this (of course) wasn't that hard to accomplish.

    In our new project we need to make a 3D museum about a certain art-style. (we've chosen pop-art).

    As our school almost only requires that we're good designers and the other students don't like scripting anyway so there we also have almost no support for scripting. So all the museums will almost certainly have camera's that go through walls etc.

    As i'm also very interested in scripting and I want to make a good "engine" for our 3D museum. First of all I wanted to make sure that my camera wont go behind walls, witch of course happens fast when you're inside a building. I already managed to fix this problem with this script:

    http://forum.unity3d.com/viewtopic....start=30&sid=2963ce83ec3f5062809d4f713f417814

    A modified the script and changed the following:
    - The script originally only moves the camera behind the target when pressing a button or moving the mouse, now it will move behind the target also when moving nothing.
    - When you pressed the left and right mouse btn it was supposed to walk ore something but the character would only be sliding in one direction. So I've managed to get rid of that part.

    As our museum is also going to have a couple of paintings I want to try if I can make the following:

    - When getting close to a painting the words "Press space to watch closer" should appear in the screen.
    - When pressing space the camera should move smoothly to a position exactly in front of the painting, when pressing space again it should move back to the character.

    Of course I wont expect from you guys to just give me such a script because I do have to learn something of it. I have in my head how it probably should work so ill explain it here.

    - First I have to make a trigger witch I can place close to an painting, this trigger has to trigger a function witch lets the words appear in my screen. This function also has to check if I press the space btn.
    - When the space btn is pressed it needs to changes the target of the camera in the "WowCamera.cs" to the painting.
    - When the camera is pointed at the painting it still has to check if the space btn is pressed but then it has to change the target of the camera in the "WowCamera.cs" back to the character.

    At least, I hope that the camera will smoothly move to the new target by it self and that I won't have to modify that part.

    I really hope that someone is interested in helping me out, because my teacher are not able to :-(

    Greetings,
    Peter aka Crash-Konijn


    WowCamera.cs

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class WowCamera : MonoBehaviour
    7. {
    8.     public Transform target;
    9.    
    10.     public float targetHeight = 1.7f;
    11.     public float distance = 5.0f;
    12.     public float offsetFromWall = 0.1f;
    13.  
    14.     public float maxDistance = 20;
    15.     public float minDistance = .6f;
    16.  
    17.     public float xSpeed = 200.0f;
    18.     public float ySpeed = 200.0f;
    19.    public float targetSpeed = 5.0f;
    20.    
    21.    
    22.     public int yMinLimit = -80;
    23.     public int yMaxLimit = 80;
    24.  
    25.     public int zoomRate = 40;
    26.  
    27.     public float rotationDampening = 3.0f;
    28.     public float zoomDampening = 5.0f;
    29.    
    30.     public LayerMask collisionLayers = -1;
    31.  
    32.     private float xDeg = 0.0f;
    33.     private float yDeg = 0.0f;
    34.     private float currentDistance;
    35.     private float desiredDistance;
    36.     private float correctedDistance;
    37.  
    38.     void Start ()
    39.     {
    40.         Vector3 angles = transform.eulerAngles;
    41.         xDeg = angles.x;
    42.         yDeg = angles.y;
    43.  
    44.         currentDistance = distance;
    45.         desiredDistance = distance;
    46.         correctedDistance = distance;
    47.  
    48.         // Make the rigid body not change rotation
    49.         if (rigidbody)
    50.             rigidbody.freezeRotation = true;
    51.     }
    52.    
    53.    
    54.    void Update()
    55.    {
    56.       Vector3 vTargetOffset;
    57.        
    58.        // Don't do anything if target is not defined
    59.         if (!target)
    60.             return;
    61.  
    62.         // If either mouse buttons are down, let the mouse govern camera position
    63.         if (Input.GetMouseButton(0))
    64.         {
    65.             xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
    66.             yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
    67.         }
    68.       //Reset the camera angle and Rotate the Target Around the World!
    69.       else if (Input.GetMouseButton(1))
    70.       {
    71.       float targetRotationAngle = target.eulerAngles.y;
    72.       float currentRotationAngle = transform.eulerAngles.y;
    73.       xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);    
    74.       target.transform.Rotate(0,Input.GetAxis ("Mouse X") * xSpeed * 0.02f,0);
    75.       xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
    76.       }
    77.      
    78.      
    79.         // otherwise, ease behind the target if any of the directional keys are pressed
    80.         else
    81.         {
    82.             float targetRotationAngle = target.eulerAngles.y;
    83.             float currentRotationAngle = transform.eulerAngles.y;
    84.             xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
    85.         }
    86.  
    87.         yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit);
    88.  
    89.      
    90.         // set camera rotation
    91.         Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0);
    92.  
    93.         // calculate the desired distance
    94.         desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
    95.         desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
    96.         correctedDistance = desiredDistance;
    97.  
    98.         // calculate desired camera position
    99.         vTargetOffset = new Vector3 (0, -targetHeight, 0);
    100.         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
    101.  
    102.         // check for collision using the true target's desired registration point as set by user using height
    103.         RaycastHit collisionHit;
    104.         Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);
    105.  
    106.         // if there was a collision, correct the camera position and calculate the corrected distance
    107.         bool isCorrected = false;
    108.         if (Physics.Linecast (trueTargetPosition, position, out collisionHit, collisionLayers.value))
    109.         {
    110.             // calculate the distance from the original estimated position to the collision location,
    111.             // subtracting out a safety "offset" distance from the object we hit.  The offset will help
    112.             // keep the camera from being right on top of the surface we hit, which usually shows up as
    113.             // the surface geometry getting partially clipped by the camera's front clipping plane.
    114.             correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall;
    115.             isCorrected = true;
    116.         }
    117.  
    118.         // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
    119.         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
    120.  
    121.       // keep within legal limits
    122.         currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);
    123.  
    124.         // recalculate position based on the new currentDistance
    125.         position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
    126.        
    127.         transform.rotation = rotation;
    128.         transform.position = position;
    129.    
    130.       //Move the Player with left  right button press together
    131.       /*if(Input.GetMouseButton(1)&Input.GetMouseButton(0))
    132.       {
    133.       float targetRotationAngle = target.eulerAngles.y;
    134.       float currentRotationAngle = transform.eulerAngles.y;
    135.       xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);            
    136.       target.transform.Rotate(0,Input.GetAxis ("Mouse X") * xSpeed  * 0.02f,0);
    137.       xDeg += Input.GetAxis ("Mouse X") * targetSpeed * 0.02f;
    138.       target.transform.Translate(Vector3.forward * targetSpeed * Time.deltaTime);
    139.       }*/
    140.    }
    141.    
    142.     /**
    143.      * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    144.      */
    145.     void LateUpdate ()
    146.     {
    147.     }
    148.  
    149.     private static float ClampAngle (float angle, float min, float max)
    150.     {
    151.         if (angle < -360)
    152.             angle += 360;
    153.         if (angle > 360)
    154.             angle -= 360;
    155.         return Mathf.Clamp (angle, min, max);
    156.     }
    157.        
    158. }
    ThirdPersonWalker.js
    Code (csharp):
    1. private var speed = 0.0;
    2. var speedRun = 12.0;
    3. var speedWalk = 6.0;
    4. var jumpSpeed = 2.0;
    5. var gravity = 20.0;
    6.  
    7. private var moveDirection = Vector3.zero;
    8. private var grounded : boolean = false;
    9.  
    10. function FixedUpdate() {
    11.     if (grounded) {
    12.         // We are grounded, so recalculate movedirection directly from axes
    13.         //transform.position.z = 0;
    14.         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    15.         moveDirection = transform.TransformDirection(moveDirection);
    16.         moveDirection *= speed;
    17.        
    18.         if (Input.GetButton ("Jump")) {
    19.             moveDirection.y = jumpSpeed;
    20.             animation.Play ("jump");
    21.         }
    22.         else if (Input.GetButton ("Vertical") || Input.GetButton ("Horizontal"))
    23.         {
    24.             if (Input.GetButton ("Run"))
    25.             {
    26.                 speed = speedRun;
    27.                 animation.CrossFade ("run");
    28.             }
    29.             else {
    30.                 speed = speedWalk;
    31.                 animation.CrossFade ("walk");
    32.             }
    33.         }
    34.         else if (Input.GetButton ("Custom"))
    35.             {
    36.                 animation.CrossFade ("custom");
    37.             }
    38.         else {
    39.             animation.CrossFade ("idle");
    40.         }
    41.     }
    42.  
    43.     // Apply gravity
    44.     moveDirection.y -= gravity * Time.deltaTime;
    45.    
    46.     // Move the controller
    47.     var controller : CharacterController = GetComponent(CharacterController);
    48.     var flags = controller.Move(moveDirection * Time.deltaTime);
    49.     grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    50. }
    51.  
    52. @script RequireComponent(CharacterController)
     
  2. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Oke I've been trying continuously since I posted this topic and I've started with a couple of things.

    First I wanted to test if my suggestion was true (about the camara going smoothly to the new target). So I changed this in my WowControler.cs

    Old:
    Code (csharp):
    1.  
    2. // The basic variable for the target
    3. public Transform target;
    4.  
    5. // This was in the beginning of the script
    6. if (!target){
    7.    return;
    8. }
    New:
    Code (csharp):
    1.  
    2. // I added these new variables and assignd the target to targetStart en the new target to targetNew. I left target empty.
    3. public Transform targetStart;
    4. public Transform targetNew;
    5. public Transform target;
    6.  
    7. // If target was empty
    8. if (!target){
    9.             target = targetStart; // Assign the original target
    10.             if(!target){ // If target is still empty, the targetStart was empty to so it doesnt have anything to follow.
    11.                 return;
    12.             }
    13.         }
    14.        
    15. // This is suppose to become the space btn but I don't know how to assign the space btn so I used this. (Yes, this is the mousebtn-combo that I cut out earlier)
    16. if(Input.GetMouseButton(1)&Input.GetMouseButton(0)){
    17.             target = targetNew; // Change the target to the new target.
    18.         }
    This did work, it did change the camara to follow another target in my scene but it snapt there as I pressed the 2 mouse btn's. It is a bit ugly but its not the main problem, Ill try to fix that later.

    Then I found this tutorial:
    http://answers.unity3d.com/questions/32/how-do-i-call-a-function-in-another-gameobjects-script

    So I went trying to call a function from another script, linked to another object but then I ran into some problems... When I tried to use the script it gave this error.

    Code (csharp):
    1. Assets/Standard Assets/Scripts/MoveToTrigger.cs(12,3): error CS8025: Parsing error
    I've tried some other things but Unity kept giving me errors...

    This is my MoveToTrigger.cs
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class MoveToTrigger : MonoBehaviour
    4. {
    5.   public WowCamera other;
    6.  
    7.   void Update()
    8.   {
    9.  
    10.   }
    11.  
    12.   if(Input.GetMouseButton(1)&Input.GetMouseButton(0)){
    13.         other.NewTarget();
    14.     }
    15.  
    16. }
    17.  
    I Also added this function to the WowCamera.cs
    Code (csharp):
    1. public void NewTarget () {
    2.         target = targetNew;
    3.     }
    And this is what my total WowCamera.cs looks like now:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class WowCamera : MonoBehaviour
    7. {
    8.     public Transform targetStart;
    9.     public Transform targetNew;
    10.     public Transform target;
    11.    
    12.     public float targetHeight = 1.7f;
    13.     public float distance = 5.0f;
    14.     public float offsetFromWall = 0.1f;
    15.  
    16.     public float maxDistance = 20;
    17.     public float minDistance = .6f;
    18.  
    19.     public float xSpeed = 200.0f;
    20.     public float ySpeed = 200.0f;
    21.    public float targetSpeed = 5.0f;
    22.    
    23.    
    24.     public int yMinLimit = -80;
    25.     public int yMaxLimit = 80;
    26.  
    27.     public int zoomRate = 40;
    28.  
    29.     public float rotationDampening = 3.0f;
    30.     public float zoomDampening = 5.0f;
    31.    
    32.     public LayerMask collisionLayers = -1;
    33.  
    34.     private float xDeg = 0.0f;
    35.     private float yDeg = 0.0f;
    36.     private float currentDistance;
    37.     private float desiredDistance;
    38.     private float correctedDistance;
    39.    
    40.     private float painting;
    41.  
    42.     void Start ()
    43.     {
    44.         Vector3 angles = transform.eulerAngles;
    45.         xDeg = angles.x;
    46.         yDeg = angles.y;
    47.  
    48.         currentDistance = distance;
    49.         desiredDistance = distance;
    50.         correctedDistance = distance;
    51.  
    52.         // Make the rigid body not change rotation
    53.         if (rigidbody)
    54.             rigidbody.freezeRotation = true;
    55.     }
    56.    
    57.    
    58.    void Update()
    59.    {
    60.       Vector3 vTargetOffset;
    61.        
    62.        // Don't do anything if target is not defined
    63.         if (!target){
    64.             target = targetStart;
    65.             if(!target){
    66.                 return;
    67.             }
    68.         }
    69.        
    70.         /*if(Input.GetMouseButton(1)&Input.GetMouseButton(0)){
    71.             target = targetNew;
    72.         }*/
    73.        
    74.         // If either mouse buttons are down, let the mouse govern camera position
    75.         if (Input.GetMouseButton(0))
    76.         {
    77.             xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
    78.             yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
    79.         }
    80.       //Reset the camera angle and Rotate the Target Around the World!
    81.       else if (Input.GetMouseButton(1))
    82.       {
    83.       float targetRotationAngle = target.eulerAngles.y;
    84.       float currentRotationAngle = transform.eulerAngles.y;
    85.       xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);    
    86.       target.transform.Rotate(0,Input.GetAxis ("Mouse X") * xSpeed * 0.02f,0);
    87.       xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
    88.       }
    89.      
    90.      
    91.         // otherwise, ease behind the target if any of the directional keys are pressed
    92.         else
    93.         {
    94.             float targetRotationAngle = target.eulerAngles.y;
    95.             float currentRotationAngle = transform.eulerAngles.y;
    96.             xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
    97.         }
    98.  
    99.         yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit);
    100.  
    101.      
    102.         // set camera rotation
    103.         Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0);
    104.  
    105.         // calculate the desired distance
    106.         desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
    107.         desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
    108.         correctedDistance = desiredDistance;
    109.  
    110.         // calculate desired camera position
    111.         vTargetOffset = new Vector3 (0, -targetHeight, 0);
    112.         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
    113.  
    114.         // check for collision using the true target's desired registration point as set by user using height
    115.         RaycastHit collisionHit;
    116.         Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);
    117.  
    118.         // if there was a collision, correct the camera position and calculate the corrected distance
    119.         bool isCorrected = false;
    120.         if (Physics.Linecast (trueTargetPosition, position, out collisionHit, collisionLayers.value))
    121.         {
    122.             // calculate the distance from the original estimated position to the collision location,
    123.             // subtracting out a safety "offset" distance from the object we hit.  The offset will help
    124.             // keep the camera from being right on top of the surface we hit, which usually shows up as
    125.             // the surface geometry getting partially clipped by the camera's front clipping plane.
    126.             correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall;
    127.             isCorrected = true;
    128.         }
    129.  
    130.         // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
    131.         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
    132.  
    133.       // keep within legal limits
    134.         currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);
    135.  
    136.         // recalculate position based on the new currentDistance
    137.         position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
    138.        
    139.         transform.rotation = rotation;
    140.         transform.position = position;
    141.    
    142.       //Move the Player with left  right button press together
    143.       /*if(Input.GetMouseButton(1)&Input.GetMouseButton(0))
    144.       {
    145.       float targetRotationAngle = target.eulerAngles.y;
    146.       float currentRotationAngle = transform.eulerAngles.y;
    147.       xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);            
    148.       target.transform.Rotate(0,Input.GetAxis ("Mouse X") * xSpeed  * 0.02f,0);
    149.       xDeg += Input.GetAxis ("Mouse X") * targetSpeed * 0.02f;
    150.       target.transform.Translate(Vector3.forward * targetSpeed * Time.deltaTime);
    151.       }*/
    152.    }
    153.    
    154.     /**
    155.      * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    156.      */
    157.     void LateUpdate ()
    158.     {
    159.     }
    160.  
    161.     public float ClampAngle (float angle, float min, float max)
    162.     {
    163.         if (angle < -360)
    164.             angle += 360;
    165.         if (angle > 360)
    166.             angle -= 360;
    167.         return Mathf.Clamp (angle, min, max);
    168.     }
    169.    
    170.     public void NewTarget () {
    171.         target = targetNew;
    172.     }
    173.        
    174. }
    I realy hope you guys can help me out!
     
  3. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Oke fixed it somehow after trying a bit more ^^

    The new MoveToTrigger.cs is now this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveToTrigger : MonoBehaviour
    5. {
    6.   public WowCamera other;
    7.  
    8.   void Update()
    9.   {
    10.     if(Input.GetMouseButton(1)&Input.GetMouseButton(0)){
    11.         other.NewTarget();
    12.     }
    13.   }
    14.  
    15.  
    16. }
    17.  
    The function in WowMovement.cs now is this:
    Code (csharp):
    1. public void NewTarget () {
    2.         target = targetNew;
    3.     }
    so the next step for me will be trying to get a function in MoveToTrigger.cs to start when i collide with a trigger I suppose :)

    Sorry for the spamming btw, Just want to keep you guys up to date ;-)

    UPDATE:

    Oke I got the trigger part working now ^^ Lets start showing some message saying that they need to press the space btn.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveToTrigger : MonoBehaviour
    5. {
    6.   public WowCamera other;
    7.  
    8.   void Update()
    9.   {
    10.     if(Input.GetMouseButton(1)&Input.GetMouseButton(0)){
    11.         other.NewTarget();
    12.     }
    13.   }
    14.  
    15.   void OnTriggerEnter (Collider collision) {
    16.       other.NewTarget();
    17.    }
    18. }
    19.  
     
  4. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Goodmorning everyone,

    I've been trying to find a tutorial about creating text witch c# but I still haven't found one... Does anybody have a good tutorial for c#?

    Greetings,
    Peter aka Crash-Konijn
     
  5. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Oke I would really appreciate it if someone could help me out now...

    I've tried like hundreds of examples for text GUI but I really don't understand how to make this work...

    These are the errors I get:
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.GUISkin.BuildStyleCache ()
    3. UnityEngine.GUISkin.OnValidate ()
    4. UnityEngine.ScriptableObject:.ctor()
    5. UnityEngine.ScriptableObject:.ctor()
    6. UnityEngine.GUISkin:.ctor()
    7. GUISkinEditor:CreateNewGUISkin()
    Code (csharp):
    1. Assets/Standard Assets/Scripts/MoveToTrigger.cs(23,28): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
    This is my MoveToTrigger.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Text;
    5. using System.IO;
    6.  
    7. public class MoveToTrigger : MonoBehaviour
    8. {
    9.   public WowCamera other;
    10.   String textTest;
    11.  
    12.   void Update() {
    13.     if(Input.GetMouseButton(1)&Input.GetMouseButton(0)){
    14.         other.NewTarget();
    15.     }
    16.   }
    17.  
    18.   void OnTriggerEnter (Collider collision) {
    19.    }
    20.    
    21.     void OnGUI () {
    22.         //GUI.Box (Rect (10,10,100,90), "Loader Menu");
    23.         GUI.Label (Rect (0,0,100,50), "This is the text string for a Label Control");
    24.     }
    25. }
    26.  
     
  6. ESB

    ESB

    Joined:
    Mar 2, 2010
    Posts:
    71
    I think you need
    Code (csharp):
    1. new Rect(0,0,100,50)
    in the Label call?
     
  7. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    I'm sorry I'm not that good in names like "label call"... I realy have no idea which part that is sorry if you could explain that to me I would be realy thankful.

    I did manage to get a btn working but again when I try tot change GUI.Butten to GUI.Label for example I again get an error... And a button is ofcourse not what I want



    Code (csharp):
    1. void OnGUI () {
    2.         //GUI.Box (Rect (10,10,100,90), "Loader Menu");
    3.         //GUI.Label (Rect (0,0,100,50), "This is the text string for a Label Control");
    4.         if (GUI.Button(new Rect(5, 5, 100, 50), "Button")) {
    5.            //Do something
    6.         }
    7.    }
    This is the error I get when changing GUI.Button to GUI.Label

    Code (csharp):
    1. Assets/Standard Assets/Scripts/MoveToTrigger.cs(25,9): error CS0029: Cannot implicitly convert type `void' to `bool'
    2.  
     
  8. ESB

    ESB

    Joined:
    Mar 2, 2010
    Posts:
    71
    Oh, I meant in the line where you got the error. So the call to Label
    Code (csharp):
    1. GUI.Label (Rect (0,0,100,50), "This is the text string for a Label Control");
    needs, in C#, to be:
    Code (csharp):
    1. GUI.Label (new Rect (0,0,100,50), "This is the text string for a Label Control");
    EDIT: Oh, and the void/bool error is because you have the call in an if/statement in your latest example, and the GUI.Label(...) call just does stuff (a "void" function), it doesn't return "true" or "false".
     
  9. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Thanks! That did work :-D

    Oke, so the next step for me will be make sure that the character hits the trigger that the words appear and then when they hit space to change the target of the camera. Ill keep you guys up to date! ^^

    EDIT:

    Oke next problem, I only need to show the words when its triggered, so I tried it with some different ways:

    Just placing the GUI.Label function in the OnTriggerEnter function:
    This seems to work but then when I walk into the trigger it gave me this error:
    Code (csharp):
    1. ArgumentException: You can only call GUI functions from inside OnGUI.
    2. UnityEngine.GUIUtility.CheckOnGUI ()
    3. UnityEngine.GUI.Label (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style)
    4. UnityEngine.GUI.Label (Rect position, System.String text)
    5. MoveToTrigger.OnTriggerEnter (UnityEngine.Collider collision)   (at Assets/Standard Assets/Scripts/MoveToTrigger.cs:21)
    6.  
    Script:
    Code (csharp):
    1. void OnTriggerEnter (Collider collision) {
    2.     //other.NewTarget();
    3.         GUI.Label (new Rect (0,0,100,50), "textString");
    4.    }
    Then I tried the same but then with the void OnGUI in the OnTriggerEnter function. (Then the GUI.Label part is IN a void OnGUI () right?)

    Code (csharp):
    1. void OnTriggerEnter (Collider collision) {
    2.     //other.NewTarget();
    3. void OnGUI () {
    4.         GUI.Label (new Rect (0,0,100,50), "textString");
    5.    }
    6.    }
    error:
    Code (csharp):
    1. Assets/Standard Assets/Scripts/MoveToTrigger.cs(21,20): error CS1002: Expecting `;'
    2.  
    And finaly I tried this; I made a variable named triggered with the value 0. Then I wanted to change triggered to 1 in the OnTriggerEnter and check in OnGUI if triggered was 1 and then show the words. But this also gave me an error...
    Code (csharp):
    1. Assets/Standard Assets/Scripts/MoveToTrigger.cs(24,17): error CS0029: Cannot implicitly convert type `float' to `bool'
    2.  
    Code (csharp):
    1.   private float triggered = 0;
    2.  
    3. void OnTriggerEnter (Collider collision) {
    4.     //other.NewTarget();
    5.     triggered = 1;
    6.    }
    7.    
    8.    void OnGUI () {
    9.         if(triggered = 1){
    10.         GUI.Label (new Rect (0,0,100,50), "This is the text string for a Label Control");
    11.         }
    12.    }
     
  10. Yorick2

    Yorick2

    Joined:
    Jan 24, 2009
    Posts:
    297
    in c# you can't say:
    if(triggered = 1)

    you need to change that to:

    if(triggered == 1)

    Good luck!
     
  11. Yorick2

    Yorick2

    Joined:
    Jan 24, 2009
    Posts:
    297
    also it is better to use a bool then a float if you just have 2 values (true or false)

    Code (csharp):
    1.  
    2. private bool triggered = false;
    3.  
    4. void OnTriggerEnter (Collider collision) {
    5.      //other.NewTarget();
    6.      triggered = true;
    7.    }
    8.    
    9.    void OnGUI () {
    10.          if(triggered == true){
    11.          GUI.Label (new Rect (0,0,100,50), "This is the text string for a Label Control");
    12.          }
    13.    }
    14.  
     
  12. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Thanks! I've also made the words to disappear when you leave the trigger :-D But nog I'm trying to give a variable (The new target for the camera) to the NewTarget() in the WowCamera.cs. But I get this error.

    Ofcourse I again tried a couple of different things and searched on the internet for the error but I still don't know how to fix this.

    Greetings,
    Peter aka Crash-Konijn

    The Error:
    Code (csharp):
    1. Assets/Standard Assets/Scripts/WowCamera.cs(170,41): error CS1001: Identifier expected
    2.  
    In the MoveToTrigger.cs
    Code (csharp):
    1. public Transform targetNew;
    2.  
    3. void OnTriggerEnter (Collider collision) {
    4.     other.NewTarget(targetNew);
    5.     triggered = true;
    6.    }
    In the WowCamera.cs
    Code (csharp):
    1. public void NewTarget (targetNew) {
    2.         target = targetNew;
    3.     }
     
  13. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Oke it seems that I've fixed that problem now, but the now I get this error:

    Error:
    Code (csharp):
    1. Assets/Standard Assets/Scripts/MoveToTrigger.cs(15,15): error CS1501: No overload for method `NewTarget' takes `0' arguments
    2.  
    WowCamera.cs
    Code (csharp):
    1. public void NewTarget (Transform test) {
    2.         target = test;
    3.         triggered = true;
    4.     }
    5.    
    6.     public void OldTarget (Transform test) {
    7.         target = targetStart;
    8.         triggered = false;
    9.     }
    MoveToTrigger.cs
    Code (csharp):
    1. if(triggered == true){
    2.         if (Input.GetKeyDown ("space")) {
    3.         other.NewTarget(targetNew);
    4.         }
    5.         else if (Input.GetKeyUp ("space")) {
    6.         other.OldTarget(targetNew);
    7.         }
    8.     }
    EDIT:

    Oke I found the problem... I still had this somewhere in my MoveToTrigger.cs:

    Code (csharp):
    1. other.NewTarget();
     
  14. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Oke so already got it working the when you keep pressing space the target of the camera would change to the new target. When you release space it would go back to the character.

    Now I'm trying to give the new target from MoveToTrigger.cs to the NewTarget() in WowCamera.cs. When I already had the targetNew already assigned it dit work but now when I'm trying to use target it gets from MoveToTrigger.cs it does nothing...

    I realy hope that someone could help me out with this!

    Greetings,
    Peter aka Crash-Konijn

    MoveToTrigger.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Text;
    5. using System.IO;
    6.  
    7. public class MoveToTrigger : MonoBehaviour
    8. {
    9.   public WowCamera other;
    10.   public Transform targetNew;
    11.  private bool triggered = false;
    12.  
    13.   void Update() {  
    14.      
    15.     if(triggered == true){
    16.         if (Input.GetKeyDown ("space")) {
    17.         other.NewTarget(targetNew);
    18.         }
    19.         else if (Input.GetKeyUp ("space")) {
    20.         other.OldTarget();
    21.         }
    22.     }
    23.  
    24.   }
    25.  
    26.   void OnTriggerEnter (Collider collision) {
    27.     //other.NewTarget(targetNew);
    28.     triggered = true;
    29.    }
    30.    
    31.    void OnTriggerExit (Collider collision) {
    32.     //other.OldTarget();
    33.     triggered = false;
    34.    }
    35.    
    36.    void OnGUI () {
    37.          if(triggered == true){
    38.          GUI.Label (new Rect (0,0,100,50), "Press space to fiew this object.");
    39.          }
    40.    }
    41. }
    42.  
    WowCamera.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class WowCamera : MonoBehaviour
    7. {
    8.     public Transform targetStart;
    9.     public Transform targetNew;
    10.     public Transform target;
    11.    
    12.     public float targetHeight = 1.7f;
    13.     public float distance = 5.0f;
    14.     public float offsetFromWall = 0.1f;
    15.  
    16.     public float maxDistance = 20;
    17.     public float minDistance = .6f;
    18.  
    19.     public float xSpeed = 200.0f;
    20.     public float ySpeed = 200.0f;
    21.    public float targetSpeed = 5.0f;
    22.    
    23.    
    24.     public int yMinLimit = -80;
    25.     public int yMaxLimit = 80;
    26.  
    27.     public int zoomRate = 40;
    28.  
    29.     public float rotationDampening = 3.0f;
    30.     public float zoomDampening = 5.0f;
    31.    
    32.     public LayerMask collisionLayers = -1;
    33.  
    34.     private float xDeg = 0.0f;
    35.     private float yDeg = 0.0f;
    36.     private float currentDistance;
    37.     private float desiredDistance;
    38.     private float correctedDistance;
    39.     private bool triggered = false;
    40.  
    41.     void Start ()
    42.     {
    43.         Vector3 angles = transform.eulerAngles;
    44.         xDeg = angles.x;
    45.         yDeg = angles.y;
    46.  
    47.         currentDistance = distance;
    48.         desiredDistance = distance;
    49.         correctedDistance = distance;
    50.  
    51.         // Make the rigid body not change rotation
    52.         if (rigidbody)
    53.             rigidbody.freezeRotation = true;
    54.     }
    55.    
    56.    
    57.    void Update()
    58.    {
    59.       Vector3 vTargetOffset;
    60.        
    61.        // Don't do anything if target is not defined
    62.         if (!target){
    63.             target = targetStart;
    64.             if(!target){
    65.                 return;
    66.             }
    67.         }
    68.        
    69.         /*if(Input.GetMouseButton(1)&Input.GetMouseButton(0)){
    70.             target = targetNew;
    71.         }*/
    72.        
    73.         // If either mouse buttons are down, let the mouse govern camera position
    74.         if (Input.GetMouseButton(0))
    75.         {
    76.             xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
    77.             yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
    78.         }
    79.       //Reset the camera angle and Rotate the Target Around the World!
    80.       else if (Input.GetMouseButton(1))
    81.       {
    82.       float targetRotationAngle = target.eulerAngles.y;
    83.       float currentRotationAngle = transform.eulerAngles.y;
    84.       xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);    
    85.       target.transform.Rotate(0,Input.GetAxis ("Mouse X") * xSpeed * 0.02f,0);
    86.       xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
    87.       }
    88.      
    89.      
    90.         // otherwise, ease behind the target if any of the directional keys are pressed
    91.         else
    92.         {
    93.             float targetRotationAngle = target.eulerAngles.y;
    94.             float currentRotationAngle = transform.eulerAngles.y;
    95.             xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
    96.         }
    97.  
    98.         yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit);
    99.  
    100.      
    101.         // set camera rotation
    102.         Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0);
    103.  
    104.         // calculate the desired distance
    105.         desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
    106.         desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
    107.         correctedDistance = desiredDistance;
    108.  
    109.         // calculate desired camera position
    110.         vTargetOffset = new Vector3 (0, -targetHeight, 0);
    111.         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
    112.  
    113.         // check for collision using the true target's desired registration point as set by user using height
    114.         RaycastHit collisionHit;
    115.         Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);
    116.  
    117.         // if there was a collision, correct the camera position and calculate the corrected distance
    118.         bool isCorrected = false;
    119.         if (Physics.Linecast (trueTargetPosition, position, out collisionHit, collisionLayers.value))
    120.         {
    121.             // calculate the distance from the original estimated position to the collision location,
    122.             // subtracting out a safety "offset" distance from the object we hit.  The offset will help
    123.             // keep the camera from being right on top of the surface we hit, which usually shows up as
    124.             // the surface geometry getting partially clipped by the camera's front clipping plane.
    125.             correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall;
    126.             isCorrected = true;
    127.         }
    128.  
    129.         // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
    130.         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
    131.  
    132.       // keep within legal limits
    133.         currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);
    134.  
    135.         // recalculate position based on the new currentDistance
    136.         position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
    137.        
    138.         transform.rotation = rotation;
    139.         transform.position = position;
    140.      
    141.      
    142.    }
    143.    
    144.     /**
    145.      * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    146.      */
    147.     void LateUpdate ()
    148.     {
    149.     }
    150.  
    151.     public float ClampAngle (float angle, float min, float max)
    152.     {
    153.         if (angle < -360)
    154.             angle += 360;
    155.         if (angle > 360)
    156.             angle -= 360;
    157.         return Mathf.Clamp (angle, min, max);
    158.     }
    159.    
    160.     public void NewTarget (Transform test) {
    161.         target = test;
    162.         triggered = true;
    163.     }
    164.    
    165.     public void OldTarget () {
    166.         target = targetStart;
    167.         triggered = false;
    168.     }
    169.        
    170. }
     
  15. Yorick2

    Yorick2

    Joined:
    Jan 24, 2009
    Posts:
    297
    did you assign the Camera and Target in the inspector for the trigger? (did you drag them on?)
     
  16. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Yes I did...

    I have 3 Crates in my scene:

    Crate1 - I use this crate to test the collision.
    Crate2 - This is the trigger and has the MoveToTrigger.cs assigned to it. targetNew has Crate3 assigned to it.
    Crate3 - This is the crate witch the camera should turn to when I keep space pressed when I'm in the trigger.
     
  17. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    EDIT:

    Oke my fault, I went over everything again and I gave Crate1 a target instead of Crate2 (witch is the trigger)

    The things I wanted to make for my museum work right now :-D the only thing that's still a problem is that the camera moves to the new target instantly, does anyone know how I could possibly fix this problem?