Search Unity

on exit do not collide

Discussion in 'Scripting' started by eko1892, Apr 1, 2011.

  1. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    Hello, I am working on a script for enabling/disabling collision when player collides with a specific object in my scene (which is currently water) I am getting few errors and I am not sure what I am doing wrong :/ Any helps would be appreciated.Thanks. Here's the script;


    Code (csharp):
    1. private var WaterCollision1MC : boolean = false;
    2. var destination : Transform;
    3.  
    4. function OnTriggerEnter(other : Collider) {
    5.     if ((other.tag == "Player")  (WaterCollision1MC = false)){
    6.     //other.transform.position = destination.position;
    7.     yield WaitForSeconds (3.0f);
    8.     Application.LoadLevel(1);
    9.     WaterCollision1MC = true;
    10.     }
    11. }
    12.  
    13.  
    14. function OnTriggerExit(other : Collider) {
    15. if ((other.tag == "Player")  (WaterCollision1MC= false)){
    16. WaterCollision1MC = false;
    17.     }
    18. }
     

    Attached Files:

  2. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    No replies :((
     
  3. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    You do some assignments instead of comparsions:

    if ((other.tag == "Player") (WaterCollision1MC == true)){
    ..
    if ((other.tag == "Player") (WaterCollision1MC== false)){

    I marked the things missing with red.

    Also I think you can write it like this:


    if (other.tag == "Player" !WaterCollision1MC){

    if (other.tag == "Player" WaterCollision1MC){

    This should fix your errors (I am no JS expert).

    For your collision problem: Could you provide more details on what you want to do?
     
  4. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    Thank you for your reply Marrrk. As you said errors are gone now.

    Basically what I am trying to do is when my player collides with a specific object which is (water) it will be teleported to another scene this is working fine. The thing I am trying to do now is I want my teleport script to stop when player interacts with any other object in the environment. Do you have any ideas ? Many thanks
     
  5. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Like this?
    Or more like this?
     
  6. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    Yes TeleportScript is attached to my water's mesh right now and it will be disabled when player interacts with some other gameobject or even terrain.

    So more like the first one you've said :)

    Here's a current screenshot
    http://www.box.net/shared/p1sbxrantn
     
  7. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Well, you will need to check collisions from your player with everything else, either put a script on each object or simply put a script on your player. If the object you collided with is not your 'Water' deactivate your TeleporterScript.

    Something like:

    Code (csharp):
    1. void OnCollisionEnter(Collision collision)
    2. {
    3.   if(!collider.gameObject.GetComponent<TeleportScript>())
    4.   {
    5.     deactivate teleport script;
    6.   }
    7. }
     
  8. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    Thank you for your message, however I have tried to include this code on my player instead of putting a script for each object :) But I am getting few errors right now :/

    Code:

    function OnCollisionEnter(other : collider)
    {
    if(!collider.gameObject.GetComponent<Teleportscriptlvl1>())
    {
    deactivate Teleportscriptlvl1;
    }
    }



    Error message:

    Assets/MediaCityProject/MCScripts/Collision.js(3,60): BCE0043: Unexpected token: ).
     
  9. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    My code was c# not JS, as far as I know JS doesnt support generics like in C# so: GetComponent<Teleportscrip tlvl1>() will not work.

    You probably need to wriite it like this: GetComponent("Teleportscrip tlvl1")

    Also Iam not sure if its possible to write scriptnames with spaces inside the name.

    Oh.. and your script needs to know the GameObject which holds your Teleport Behaviour/Script or the Teleport Behaviour/Script, without this klnowledge deactivate will not work. Iam also not sure if deactivate is valid in JS, maybe Destroy(teleportScript) will do it.
     
  10. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    Ermm I see.. :// As I am not good at coding I still couldn't figure it out 100% :( but I will hopefully sort it out :) Thanks again for your help
     
  11. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    Hello again, I have tried something like this just to see whether the debug is gonna get triggered or not. I have put those into my teleport script instead of putting a seperate code for the player. Tought should do the same logic but may be I am wrong. Anyway my debug log is working when I standing on the water, however when I collide with the sphere around the water it does not displays any debug logs :// Thanks


    Code:

    var destination : Transform;


    function OnTriggerEnter(other : Collider) {
    if (other.tag == "Player")
    Debug.Log("On Stargate");
    yield WaitForSeconds (5.0f);
    Application.LoadLevel(1);
    }
    }

    function OnTriggerExit(other : Collider) {
    if (other.gameObject.tag == "SphereMC"){
    Debug.Log("On Terrain");
    }
    }


    Error:
    The referenced script on this Behaviour is missing!

    Please assign a target to the camera that has a ThirdPersonController script attached.
    UnityEngine.Debug:Log(Object)
    ThirdPersonCamera:Awake() (at Assets/MediaCityProject/Sources/Scripts/ThirdPersonCamera.js:58)
     
  12. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    "I have put those into my teleport script instead of putting a seperate code for the player" <- well this is the problem here, how should the teleporterscript know when the player collides with something?

    The teleporterscript is attached to your "Water" so it only will call OnTriggerEnter or OnTriggerExit when something hits the "Water".
     
  13. eko1892

    eko1892

    Joined:
    Mar 28, 2011
    Posts:
    139
    Ok not the right logic than. Sorry but I am still very confused because of this thing :(