Search Unity

Sythax help.

Discussion in 'Scripting' started by Thaniels, Sep 2, 2014.

  1. Thaniels

    Thaniels

    Joined:
    Mar 31, 2014
    Posts:
    22
    Dear community,

    I am currently working on making my graduation project, I'm a designer in my last year, and as final project I wanted to make something to be truly proud off. So I wanted to design, write build and program the whole thing myself. With the help of friends I managed to get pretty far with programming in C#, I have a couple of things working in my level and it's getting along nicely. But there are things I keep walking in to, things that I can't solve or cannot find a case applicable to my problem on the forums. Then when I try to understand the information that is given in the problem I find myself getting lost in terms I do not understand or do not know how to apply them and cannot solve the problem on my own.

    I will not ask you to write my code for me, it's something I want to do. The code I am writing is not complicated, and I am coding some simple things, to portray the story of my game. I just need some help with understanding how some of my code works. I want to be able to solve these problems myself and not having to ask help with terribly simple things.

    The project is due in 1.5 month, halfway October, so I am not in a huge rush, but my question now is if some of you would be kind enough to help me?
     
    Last edited: Sep 2, 2014
  2. Thaniels

    Thaniels

    Joined:
    Mar 31, 2014
    Posts:
    22
    Here is a small example of what I mean, this piece of code is almost done.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TrapTrigger : MonoBehaviour {
    5.  
    6.     public GameObject playa;
    7.    
    8.        
    9.     // Use this for initialization
    10.     void Start () {
    11.         }
    12.     void OnCollisionEnter(Collision col) {
    13.             if (otherObj == "playa") {
    14.                 Destroy(Plane,.5f);
    15.             }
    16.         }
    17.         void OnCollisionExit(Collision col){
    18.                 if (Time.timeScale == 1.0F) {
    19.                         Time.timeScale = 0.7F;
    20.                     }
    21.                     else
    22.                     {
    23.                     Time.timeScale = 1.0F;
    24.                         Time.fixedDeltaTime = 0.02F * Time.timeScale;
    25.                 }
    26.         }
    27.  
    28.  
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.    
    33.     }
    34. }
    35.  
    I just don't know why otherObj is depicted red in my code, I know it's something that is not present in my code, just don't know what to put in that space, the same goes for col behind Collision. those are the small things I keep hanging up on, maybe a bit strange, but I really need to know.
     
  3. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    if it goes red then monodevelop detect a error, does unity throw a error when it compiles?
     
  4. Bekkk

    Bekkk

    Joined:
    Jul 28, 2012
    Posts:
    17
    well I would say that in OnCollisionEnter if you want to check if the colliding object is actually player, you need to check the collision object : col.gameObject and compare it to the playa game object - playa (without "" . With "" you would be checking some string. e.g. tag - col.gameObjet.tag == "player").

    In OnCollisionExit you should check which object actually left collider. So something like this : if(col.gameObject == playa) ..

    I hope it helps...
     
  5. Thaniels

    Thaniels

    Joined:
    Mar 31, 2014
    Posts:
    22
    ok, here is another one I need a little help with. I have this code which handles my events one by one to play in sequence after each other, at the moment I have two different classes that are added to the sequence at the moment but once again I do not know exactly what comes after a certain call within the code, hope some of you can help me with that.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Fall_Event_Trigger : MonoBehaviour {
    5.  
    6.     public Sequence_State m_sequenceState; //holder voor de huidige state
    7.     public float[] m_sequenceDuration; //array van de durations, deze moet even lang zijn als het aantal states in squence_state (5 dus)
    8.     public float current_duration; //houdt bij hoe lang de huidige state aan de gang is
    9.  
    10.     public TrapTrigger tt;
    11.     public ClimbCollision cc;
    12.  
    13.     public enum Sequence_State // enum type om deze specifieke sequence mee op te delen
    14.     {
    15.         Fall,
    16.         Climb,
    17.         End,
    18.     }
    19.     // Use this for initialization
    20.     void Start () {
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {// maak apparte class, maak een naam aan onder public class, dan naam toevoegen aan sequence state
    26.         switch (m_sequenceState)
    27.         { //anything that has to happen every frame during a specific state, call it here.
    28.         case Sequence_State.Fall:
    29.             // Do nothing
    30.             break;
    31.         case Sequence_State.Climb:
    32.             CountSequenceTime();
    33.             break;
    34.         case Sequence_State.End:
    35.             // do nothing, or destroy self or something
    36.             break;
    37.         }
    38.     }
    39.  
    40.     void CountSequenceTime()
    41.     {
    42.         current_duration += Time.deltaTime;
    43.         if(current_duration > m_sequenceDuration[(int)m_sequenceState])
    44.         {
    45.             current_duration = 0;
    46.             m_sequenceState++;
    47.             switch (m_sequenceState)
    48.             {   /// if there is anything that has to happen (or be called) ONCE at the start of the sequence, do it here
    49.             case Sequence_State.Fall:
    50.                 tt.();
    51.                 break;
    52.             case Sequence_State.Climb:
    53.                 cc.();
    54.                 break;
    55.             case Sequence_State.End:
    56.                
    57.                 break;
    58.             }
    59.  
    60.         }
    61.     }
    62.  
    63.     /*public void StartWithBirds() // public functie die door bijv een andere trigger script kan worden gecalled.
    64.     {
    65.         m_sequenceState = Sequence_State.Birds;
    66.  
    67.     }
    68.  
    69.     public void OnTriggerEnter(Collider col)
    70.     {
    71.         if (m_sequenceState == Sequence_State.Waiting) {
    72.             StartWithBirds();
    73.                 }
    74.         }
    75.  
    76. */
    77. }
    78.  
    I am talking about the two classes under void countsequence, called tt and cc, have no idea what I have to add behind that period. Can anyone help?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    How are you writing code that you don't understand? Are you copying this from somewhere? Are you using an external library? Does that library come with documentation?

    TrapTrigger ad ClimbCollision aren't part of the Unity API and you haven't really been clear about what they do and how that relates to what your'e trying to do (whatever it is you're trying to do) so it's nearly impossible to provide any guidance.
     
  7. Thaniels

    Thaniels

    Joined:
    Mar 31, 2014
    Posts:
    22
    I have some help from friends, but I do understand the code I am writing. I know what I have to do with the code, the problem is the small fixes that i have to preform to make the code work, those are the ones that sometimes have trouble with. for instance with this example I know I have to add a few lines of code to make this script recognize the other two and make them run together with this scripts. That I have to add a public void with a name I can then link to this class and put behind the tt. but the thing simply is that I have never had unity programming in our school, there some things I know and some things I don't and sadly some of the simpler ones I do not. That is basically where my problem lies.
     
  8. Thaniels

    Thaniels

    Joined:
    Mar 31, 2014
    Posts:
    22
    Hello fourm, I'm back with another little problem I have stumbled on during the creation of my Graduation project. At the moment I am trying to create swimmable water in my game, and I am almost there, except for one little thing. I have followed a tutorial on youtube telling me how to make the swimmable water work, this tutorial uses the charecter controller and character motor of the unity engine. I have written my own character controller for my game and here is also where the probelem arises. I am missing one value I need to pass to make the code work, but I can't figure out which it is. I'll post both my character controller and my swimmer code, and hopefully someone can help me :) (the character motor is still the same as unity uses).

    Character controller:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(CharacterController))]
    5. public class FirstPersonController : MonoBehaviour {
    6.    
    7.     public float runSpeed = 20.0f;
    8.     public float crchSpeed = 3.0f;
    9.     public float movementSpeed = 5.0f;
    10.     public float mouseSensivity = 5.0f;
    11.     public float jumpSpeed = 4.0f;
    12.     public bool grounded = false;
    13.     public float acceleration = 2.0f;
    14.     public float deceleration = 0.5f;
    15.  
    16.     private float curSpeed = 5.0f;
    17.     private float fallSpeed = 5.0f;
    18.    
    19.     private Transform tr;
    20.     private float dist; // distance to ground
    21.    
    22.     float verticalRotation = 0;
    23.     public float upDownRange = 60.0f;
    24.    
    25.     public float verticalVelocity = 0;
    26.    
    27.     CharacterController characterController;
    28.  
    29.     // Use this for initialization
    30.     void Start () {
    31.         Screen.lockCursor = true;
    32.         characterController = GetComponent<CharacterController>();
    33.         tr = transform;
    34.         CharacterController ch = GetComponent<CharacterController>();
    35.         dist = ch.height/2; // calculate distance to ground  
    36.     }
    37.  
    38.  
    39.     void FixedUpdate (){
    40.         float vScale = 1.0f;
    41.         float speed = movementSpeed;
    42.  
    43.         curSpeed += acceleration * Time.deltaTime;
    44.  
    45.         if (curSpeed > runSpeed)
    46.             curSpeed = runSpeed;
    47.  
    48.         fallSpeed -= deceleration * Time.deltaTime;
    49.  
    50.         if (fallSpeed < movementSpeed)
    51.             fallSpeed = movementSpeed;
    52.    
    53.         if (Input.GetKey("c"))
    54.         { // press C to crouch
    55.             vScale = 0.5f;
    56.             movementSpeed = crchSpeed; // slow down when crouching
    57.         }else{
    58.             movementSpeed = 5f;
    59.         }
    60.         if ((Input.GetKey(KeyCode.LeftShift)|| Input.GetKey(KeyCode.RightShift))&& characterController.isGrounded)
    61.         {
    62.             movementSpeed = curSpeed;
    63.         }else{
    64.             movementSpeed = 5f;
    65.         }
    66.  
    67.         float ultScale = tr.localScale.y; // crouch/stand up smoothly
    68.  
    69.         Vector3 tmpScale = tr.localScale;
    70.         Vector3 tmpPosition = tr.position;
    71.  
    72.         tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
    73.         tr.localScale = tmpScale;
    74.        
    75.         tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position
    76.         tr.position = tmpPosition;  
    77.     }
    78.     // Update is called once per frame
    79.     void Update () {  
    80.         //Rotation
    81.        
    82.         float rotLeftRight = Input.GetAxis("Mouse X")* mouseSensivity;  
    83.         transform.Rotate(0, rotLeftRight, 0);
    84.        
    85.         verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensivity;
    86.         verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
    87.         Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
    88.        
    89.         //movement
    90.        
    91.         float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
    92.         float sideSpeed = Input.GetAxis ("Horizontal") * movementSpeed;
    93.  
    94.         verticalVelocity += Physics.gravity.y * Time.deltaTime;
    95.        
    96.         //if(characterController.isGrounded && Input.GetButton ("Jump")) {            
    97.             //verticalVelocity = jumpSpeed;          
    98.         //}
    99.  
    100.        
    101.         Vector3 speed = new Vector3( sideSpeed, verticalVelocity, forwardSpeed );  
    102.  
    103.         speed = transform.rotation * speed;      
    104.  
    105.         characterController.Move ( speed * Time.deltaTime );        
    106.     }  
    107. }
    Swimcode:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Swimmer : MonoBehaviour {
    5.     FirstPersonController fpc;
    6.     CharacterController cc;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         RenderSettings.fog = false;
    11.         RenderSettings.fogColor = new Color (0.2f, 0.4f, 0.8f, 0.5f);
    12.         RenderSettings.fogDensity = 0.04f;
    13.  
    14.         fpc = gameObject.GetComponent<FirstPersonController> ();
    15.         cc = gameObject.GetComponent<CharacterController> ();
    16.     }
    17.     bool IsUnderwater()
    18.         {
    19.         return gameObject.transform.position.y < 14.5396;
    20.         }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         RenderSettings.fog = IsUnderwater ();
    25.  
    26.         if (IsUnderwater ())
    27.         {
    28.             fpc.runSpeed = 4;
    29.             fpc.movementSpeed = 2;
    30.             fpc.crchSpeed = 1;
    31.  
    32.             if (Input.GetKey( KeyCode.LeftShift))
    33.                 fpc.verticalVelocity(new Vector3(cc.velocity.x, 3, cc.velocity.z));
    34.             }
    35.             else
    36.             {
    37.             fpc.runSpeed = 10;
    38.             fpc.movementSpeed = 5;
    39.             fpc.crchSpeed = 3;
    40.         }
    41.     }
    42. }
    43.  
    Thanks in advance :D