Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Enemy movement towards player

Discussion in 'Scripting' started by GamesOnAcid, Feb 11, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var player : Transform;
    4. var anim;
    5.  
    6. function Start()
    7. {
    8.     anim = GetComponent.<Animator>();
    9. }
    10.  
    11. function Update()
    12. {
    13.     transform.LookAt(player);
    14.     var distance = Vector3.Distance(transform.position, player.position);
    15.     if (distance <= 3)
    16.     {
    17.        
    18.     }
    19.     else if (distance <= 20)
    20.     {
    21.         transform.Translate(Vector3.forward);
    22.         anim.SetFloat ("Speed", 3f);
    23.         anim.SetBool("Running", true);
    24.         anim.SetBool("Walking", false);
    25.     }
    26.     else if (distance <= 999)
    27.     {
    28.         transform.Translate(Vector3.forward);
    29.         anim.SetFloat ("Speed", 1.5f);
    30.         anim.SetBool("Running", false);
    31.         anim.SetBool("Walking", true);
    32.     }
    33. }
    I'm attempting to use this to make an enemy walk/run towards a player. I know that anim.SetFloat and such works in c#, but what is the JS/US equivalent? I'm doing it wrong here, it says SetFloat isn't a member of Object. Can anyone tell me how to set floats and bools with js via code? Thanks!
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I wasn't able to reproduce this in my JavaScript code... It's odd that it's referring to anim as an Object, isn't it?

    Sorry for the dumb question, but is this the whole script? Does any other script access this GameObject's Animator component?
     
  3. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    is it getting the Animator component? double check that.
     
  4. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    No nothing else accesses it, and yes, it is the full current script. It isn't complete, but I want the basic functions first.
     
  5. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    The main problem is the fact that it is telling me that SetFloat is not a member of Object, so all I need to know is how to properly reference the Animator on the Enemy GameObject so it doesn't read animator as gameobject.
     
  6. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    yeah that is the c# version for GetComponent.

    try

    var anim : Animator;

    function Start (){

    anim = GetComponent("Animator");

    }
     
  7. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Yes, he now animates properly and moves towards me, thanks!
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. GetComponent.<type>()
    3.  
    is perfectly valid UnityScript syntax, the problem being the lack of typing on the declaration which means the returned component is converted into a generic "object" reference instead of being of type Animator in the OP...

    Not too sure why he's translating c# into unityscript though o_O
     
    Jaysta likes this.
  9. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    cool I thought it may of just been the declaration type
     
  10. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Long story xD, basically I'm taking a Java course through high school, and they wanted the full project JS, not c# at all, and my partner knows c# more fluid than JS, so I'm basically a translator. I should really switch soon, as c# is a lot more versatile from what I've seen
     
  11. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    In my attempt at reproducing the issue, I made it a point to not type the declaration (I also thought that was the problem), but my GameObject still animated properly. Does the object not become an Animator on assignment?
     
  12. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I'm pretty sure the reason it didn't happen for me is because I didn't have #pragma strict in my script, by the way.