0
\$\begingroup\$

I am trying to make an AI that tries to move towards the player but I don't know how. I tried using Vector2.MoveTowards() but it just mimics my movements instead of moving towards the player. I tried everything i could think of (which was not a lot :D) but i couldn't make it move towards and touch the player! Help please :D.

Also my original code:

#pragma strict
var speed : float = 100;
var targetX : Transform;
var targetY : Transform;
var mex : Transform;
var meY : Transdorm;
function Update ()
{
var targetX = transform.position.x;
var targetY = transform.position.y;
var meX = transform.position.x;
var meY = transform.position.y;
Vector2.MoveTowards(Vector2(meX, meY), Vector2(targetX, targetY), speed);
}
\$\endgroup\$
2
\$\begingroup\$

Hey there are couple of things which you are missing out.

1) Even thought the motion is in 2d plane but the gameobject dimension is 3D, so use Vector3 instead of Vector2

2) When you calculate Vector3 Movetowards it returns a value which is calculated upon current position , target position and speed.

This needs to be assigned to the transform of the AI. Link:- http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

Here is the code: This script needs to be put on AI object , and assign the player variable through inspector.

 var player : GameObject;
 var speed : float;

function Start () 
{
    speed = 10;
}

function Update () 
{
    gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, player.transform.position  , Time.deltaTime*speed);
}
\$\endgroup\$
0
\$\begingroup\$

Your targetX and targetY are being set as your current script's transform.position, rather than your target's position.

Also, targetX and targetY are Transforms, when really it sounds like they should be floats, or perhaps even just simplified as a single var target : Transform;. You could also just have var me : Transform; as well.

Your MoveTowards should also multiply speed by Time.deltaTime to make the movement be framerate-independent.

#pragma strict
var speed : float = 100;
var target : Transform;
var me : Transform;

function Update ()
{
    Vector2.MoveTowards(me.position, target.position, speed * Time.deltaTime);
}

Note that you will need to define both target and me either in the Inspector or within a script.

\$\endgroup\$
1
  • \$\begingroup\$ Nope.. it didn't solve it... It basically did the same thing. Is there a better way to make something try to touch you? Becouse I tried everything with this function and nothing worked. \$\endgroup\$ – ElPolloLoco999 Aug 20 '15 at 16:47
-2
\$\begingroup\$
public GameObject player;
public float speed;

void Start()
{
    player = GameObject.Find ("player"); 
}


void Update()
{
    float z = Mathf.Atan2((player.transform.position.y - transform.position.y), (player.transform.position.x - 
                                                                                 transform.position.x))*
        Mathf.Rad2Deg - 90;

    transform.eulerAngles = new Vector3( 0, 0, z);

    GetComponent<Rigidbody2D>().AddForce (gameObject.transform.up * speed);


}

}

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Its probably worth trying to address the question with more than just an unexplained code sample.. \$\endgroup\$ – Gnemlock Dec 14 '16 at 7:27
-2
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveEnemy : MonoBehaviour{
 //link the player//
 public GameObject player;
//fix the speed//
public float speed;



void Update () 
{
    gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, player.transform.position  , Time.deltaTime*speed);
}
}
\$\endgroup\$
1
  • \$\begingroup\$ Donwvoting because it has no explanation and it's repeating existing answers. Also the question asks for unityscript, not c#. \$\endgroup\$ – Vaillancourt Nov 10 '20 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.