Search Unity

A few simple questions

Discussion in 'Scripting' started by Krispy, Jun 14, 2005.

  1. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Ok, one dumb, one not so dumb (but related).

    What's the syntax for accessing an object, and thus it's properties and variables, in C#?

    How do i find out what an object is hitting? I'm trying to use Collider.collider but it apparently needs an object refrence, how do I set that up?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    In C# you have to derive your script class from MonoBehaviour.

    What object do you want to access?
    For example if you want to access the transfrom you can use:

    Code (csharp):
    1.  
    2. transform.position = Vector3.up;
    3.  
    If you want to get an arbitrary component including scripted components you can use:
    Code (csharp):
    1.  
    2. ScriptClassName comp = (ScriptClassName)GetComponent (typeof (ScriptClassName));
    3.  

    Whenever an object hits another object OnCollisionEnter/OnCollisionStay/OnCollisionExit messages are sent to the rigid bodies that collide.
    file:///Applications/Unity/Documentation/ScriptReference/MonoBehaviour.html#OnCollisionEnter

    When you override these methods you can use the Collision class to get more information about what you collide with.
    file:///Applications/Unity/Documentation/ScriptReference/Collision.html
     
  3. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Ok, that's where I get confused. First off i'm using a RigidBody set to "Trigger" so i have to use the Trigger specific methods for stuff. I'm trying to access ANY object. I want to know what the syntax is. I know in Actionscript it's (for absolute): _root.OBJECT.VARIABLE for instance _root.player.lives

    I tried using Collision.collider, but the console told me it needed an "object refrence" that's why these 2 questions are related (maybe).
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    In unity you work quite a bit different with objects than in flash.

    In unity you usually don't access objects by name.
    Instead you attach a script to the object and then access it's components.

    If you want to access a completely seperate object from a script you can declare a variable that references the other object. Then you can assign it in the inspector.

    Code (csharp):
    1.  
    2. var otherRigidbody : Rigidbody;
    3. function Update () {
    4.    otherRigidbody.AddForce (Vector3.up);
    5. }
    6.  
    When you attach this script to any object, the "otherRigidbody" variable will show up in the inspector. You then just drag&drop the rigid body - you want to AddForce on - on the "otherRigidbody" variable in the inspector.


    It would be helpful to know more about what you are trying to do exactly (Just post the entire script you want to use),
    then i can tell you more about how to best do that in unity.
     
  5. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Ok, Here's the script in it's entirety. It's working as I want it to, but it'd be nice for it to know what it hits.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Boomerang : MonoBehaviour{
    6.     public float speed = 7;
    7.     public float returnSpeed = 14;
    8.     public GameObject target;
    9.     public GameObject parent;
    10.     bool fired = false;
    11.     bool hit = false;
    12.     public float distanceToParent = 0;
    13.     public float distanceToTarget = 0;
    14.     public float range = 20;
    15.     public Vector3 start = new Vector3 (0,0,0);
    16.    
    17.     void hitTarget() {
    18.         hit = true;
    19.     }
    20.     void FixedUpdate (){
    21.         if(fired){
    22.             // I Can't Figure Out Hit Detection Stuff follows//
    23.             distanceToParent = Vector3.Distance(parent.transform.position, transform.position);
    24.             distanceToTarget = Vector3.Distance(target.transform.position, transform.position);
    25.            
    26.             if(distanceToTarget < 1){
    27.                 hitTarget();
    28.                 //hit = true;
    29.             }
    30.             if(distanceToParent < 2  hit){
    31.                 hit = false;
    32.                 fired = false;
    33.             }
    34.  
    35.             //ENDS//
    36.            
    37.            
    38.             if (distanceToParent >= range){
    39.                 hit = true;
    40.             }
    41.            
    42.             if (hit){
    43.                 Vector3 difference = parent.transform.position - transform.position;
    44.                     Quaternion rotation = Quaternion.LookRotation(difference, Vector3.up);
    45.                     transform.rotation = rotation;
    46.                 rigidbody.AddForce ( Vector3.Normalize(difference) * returnSpeed, ForceMode.VelocityChange);
    47.             }
    48.             else {
    49.                 Vector3 difference = target.transform.position - transform.position;
    50.                     Quaternion rotation = Quaternion.LookRotation(difference, Vector3.up);
    51.                     transform.rotation = rotation;
    52.                 rigidbody.AddForce ( Vector3.Normalize(difference) * speed, ForceMode.VelocityChange);
    53.             }
    54.         }
    55.         else {
    56.             if ( Input.GetButton("Fire1")){
    57.                 fired = true;
    58.                 start = transform.position;
    59.             }
    60.             transform.position = Vector3.Lerp (transform.position, parent.transform.position, 1);
    61.             transform.rotation = Quaternion.Lerp (transform.rotation, parent.transform.rotation, 1);
    62.         }
    63.  
    64.  
    65.  
    66.        
    67.     }
    68.     /*void OnCollisionEnter() {
    69.         if(BoxCollider.collider == target || BoxCollider.rigidbody == target){
    70.             hit = true;
    71.         }
    72.         else if (BoxCollider.collider == target || BoxCollider.rigidbody == target){
    73.             if(hit){
    74.                 hit = false;
    75.             }
    76.         }
    77.     }
    78.    
    79.     void OnTriggerEnter (){
    80.         if(distance > 1){
    81.             hit = true;
    82.         }
    83.     }*/
    84.    
    85. }
    Also: Comment on ANYTHING in this code. It's my very firse script with Unity.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Take a look at the OnCollisionEnter documentation.
    You can find it in the MonoBehaviour script reference.

    The Collision class gives you all the information about a collision. What you have collided with, impact forces and contact points.

    Code (csharp):
    1.  
    2. void OnCollisionEnter(Collision collision) {
    3.      if (collision.other.gameObject == target)
    4.      {
    5.         hit = true;
    6.      }
    7. }
    8.  
    The script looks fine. A few comments:

    * You might want to have references to other objects as references to the components directly. Eg. target could be a Transfrom instead of GameObject.
    This would make your code simpler. You can still drag the game object on to the target variable in the inspector. It will just assign the variable to be the transform of the game object

    * With version 1.0.1 (Beta version is coming out today or tomorrow) you can simplify a lot of the code by using the new Transform interface. We have added a lot of functions to make rotations easier. eg.

    Vector3 difference = parent.transform.position - transform.position;
    Quaternion rotation = Quaternion.LookRotation(difference, Vector3.up);
    transform.rotation = rotation;

    is simplified to:
    transform.LookAt (parent.transform.position);