1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

Shark Attack!

Discussion in 'Scripting' started by stuntpea, Sep 3, 2009.

  1. stuntpea

    stuntpea

    Joined:
    Jun 30, 2009
    Messages:
    22
    Hi all,
    I'm a bit stuck debugging a script. It's actually hurting my brain.

    I've got a shark moving in a circle and I've used a bit of script from the robot (badguy) script in the Lurpz tutorial and using a capsule collider to trigger it. It worked the 1st time by bumping my character backwards and removing health points, but had a script error. Now after restarting Unity the game won't run because of the error. See below.
    assets/scripts/SharkAttack.js (43,59):BCE0044: expecting :, found ';'

    I can't figure it out as I've tried putting in a : which obviously doesn't work.
    Below is my script. Any help debugging it would be most appreciated. I'm a bit of a coding noob being an artist by trade.

    var damage = 1;

    var target : Transform;

    // Cache a reference to the controller
    private var characterController : CharacterController;
    characterController = GetComponent(CharacterController);

    // Cache a link to LevelStatus state machine script:
    var levelStateMachine : LevelStatus;

    function Start ()
    {
    levelStateMachine = GameObject.Find("/Level").GetComponent(LevelStatus);

    if (!levelStateMachine)
    {
    Debug.Log("Shark: ERROR! NO LEVEL STATUS SCRIPT FOUND.");
    }

    if (!target)
    target = GameObject.FindWithTag("Player").transform;
    }

    function OnTriggerEnter (col : Collider)
    {
    var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);
    if (controller != null)
    print ("enter1");

    // Keep looking if we are hitting our target
    // If we are, knock them out of the way dealing damage
    //var pos = transform.TransformPoint(punchPosition);
    //if(Time.time > 0.3 (pos - target.position).magnitude < punchRadius)
    {
    // deal damage
    SendMessage("ApplyDamage", damage);
    // knock the player back and to the side
    var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
    slamDirection.y = 0;
    slamDirection.z = 1;
    if (slamDirection.x >= 0)
    slamDirection.x = 1;
    else
    slamDirection.x = -1;
    target.SendMessage("Slam", transform.TransformDirection(slamDirection));
    lastPunchTime = Time.time;
    }
    }

    Here's a bit of an old link to the game I'm working on if it helps.

    http://www.vj23.com/pages/test/test.html

    Thanks again for any help. :)
     
  2. stuntpea

    stuntpea

    Joined:
    Jun 30, 2009
    Messages:
    22
    WooHoo! I've fixed it! :D

    I reckon this script will be really useful for landing on spikes, hot lava and all things that can damage the player. Or maybe comment out the damage bit, mess round with the slam direction and you could turn it into bouncepads or something.
    Here's the code below:

    var damage = 1;

    var target : Transform;

    var levelStateMachine : LevelStatus;

    function Start ()
    {
    levelStateMachine = GameObject.Find("/Level").GetComponent(LevelStatus);

    if (!levelStateMachine)
    {
    Debug.Log("Shark: ERROR! NO LEVEL STATUS SCRIPT FOUND.");
    }

    if (!target)
    target = GameObject.FindWithTag("Player").transform;
    }

    function OnTriggerEnter (col : Collider)
    {
    var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);
    if (controller != null) ;
    print ("enter1");

    controller.SendMessage("ApplyDamage",damage);
    // knock the player back and to the side
    var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
    slamDirection.y = 0;
    slamDirection.z = 1;
    if (slamDirection.x >= 0)
    slamDirection.x = 1;
    else
    slamDirection.x = -1;
    target.SendMessage("Slam", transform.TransformDirection(slamDirection));
    }
     
  3. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Messages:
    952
    Hello,

    I'm just curious :p what was the error, couldn't see it yesterday when you posted :p