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?
-
\$\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– zoran4042015-01-28 01:11:57 +00:00Commented Jan 28, 2015 at 1:11
-
\$\begingroup\$ What are you talking about? \$\endgroup\$kprovost7314– kprovost73142015-01-28 22:58:42 +00:00Commented Jan 28, 2015 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– Dimitri mx2015-01-29 15:09:21 +00:00Commented Jan 29, 2015 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– jzx2015-01-31 00:04:48 +00:00Commented Jan 31, 2015 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– return true2015-02-05 01:38:35 +00:00Commented Feb 5, 2015 at 1:38
2 Answers
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
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);
}
}
-
\$\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– kprovost73142015-02-01 20:16:46 +00:00Commented Feb 1, 2015 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– jzx2015-02-02 03:21:11 +00:00Commented Feb 2, 2015 at 3:21