1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Shark Attack!

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

  1. stuntpea

    stuntpea

    Joined:
    Jun 30, 2009
    Posts:
    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
    Posts:
    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
    Posts:
    984
    Hello,

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