-2
\$\begingroup\$

I am making a project in Unity 4.6.1 and I don't know how to make the enemy move to certain points. I would want it to move to 2 to 4 points or more. I also don't know how to make the player move (not First Person View). How do I fix these problems?

\$\endgroup\$
  • \$\begingroup\$ Make a graph (represented by connected dots) of your map, and let them travel over the lines (that connect the dots) from one dot to another \$\endgroup\$ – zoran404 Jan 28 '15 at 1:11
  • \$\begingroup\$ What are you talking about? \$\endgroup\$ – kprovost7314 Jan 28 '15 at 22:58
  • \$\begingroup\$ you might just want to check the unity basic tutorials. Also checking gamedev.stackexchange.com/search?q=%5Bunity3d%5D+player+move cant hurt \$\endgroup\$ – Dimitri mx Jan 29 '15 at 15:09
  • 1
    \$\begingroup\$ This is either a duplicate question or needs a lot more detail. What do you mean by "player?" Is it a sprite? A model from third person view? Along a plane, or is there a vertical component? Just move to specific points? Move how? Accelerate and decelerate, or just move at constant velocity? When? \$\endgroup\$ – jzx Jan 31 '15 at 0:04
  • 1
    \$\begingroup\$ Is this in 3D, or 2D? Is there hit tests, or is there no 'ground' in the scene that you must stand on to move the character? Do you need to avoid obstacles, or move directly in a strait line between points? So, no FPV, so what are you using instead, are you just moving a camera around or using some other type of movement system? - If you can answer these questions, then you may be able get a better answer. Depending on exactly what you are looking for, I would most likely offer a specific approach. \$\endgroup\$ – return true Feb 5 '15 at 1:38
0
+50
\$\begingroup\$

You mentioned you want to have the game object move to 2, 4 or more points however you did not clarify on the behavior of this movement. You can follow jzx's solution but that would only allow you one movement point. So you can implement this in several different ways, one way would be to use an array of GameObject to move to the objects in sequence.

Making an enemy move to predefined points

using UnityEngine;
using System.Collections;

public class MovementSequence : MonoBehaviour
{
    public Transform[] targets;
    public float speed;
    private int index = 0;
    void Update() 
    {
        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, targets[index].position, step);

        if( Vector3.Distance( transform.position,targets[index].position) < 0.01f) 
        {
            if(index == targets.Length - 1)
                {
                     index = 0;
                }
            else
                index++;
        }
    }
}

Another more powerful way to implement this is using a graph but you need to do some research on how to use them correctly. A good place to look up graphs is http://www.vcskicks.com/representing-graphs.php

\$\endgroup\$
2
\$\begingroup\$

Attach this behavior to something to cause it to move (taken directly from Transform.Translate):

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        transform.Translate(Vector3.forward * Time.deltaTime);
        transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    }
}

To move it to a specific point, attach this and set the target and speed fields to where you want it to go and how fast, respectively (from Vector3.MoveTowards reference):

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Transform target;
    public float speed;
    void Update() {
        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }
}
\$\endgroup\$
  • \$\begingroup\$ I keep getting 'NullReferenceException: Object reference not set to an instance of an object.' Should I insert the coordinates of it instead of 'transform.position, target.position, step'? \$\endgroup\$ – kprovost7314 Feb 1 '15 at 20:16
  • \$\begingroup\$ Target should be the transform of a GameObject, possibly an empty. Alternatively, you could make it a GameObject, then use target.transform.position in the MoveTowards call. \$\endgroup\$ – jzx Feb 2 '15 at 3:21

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.