Hello. I must inform you from the start that I am an amateur when comes to scripting.
I am trying to create an Arcade game like the one from -->> http://unity3d.com/learn/tutorials/projects/space-shooter
Right now I am trying to create the Enemy logic, so, I managed to put together a few lines that I found on the internet and the result is the script bellow.
"
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public Transform target;
public float moveSpeed;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void FixedUpdate ()
{
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Move on target's X position
Vector3 playerPosition = new Vector3 (target.position.x, 0.0f, 0.0f);
GetComponent<Rigidbody> ().velocity = playerPosition * moveSpeed; // PROBLEM
myTransform.position = playerPosition * moveSpeed; // PROBLEM
}
}
"
I have managed to get the enemy to seek for the Player’s position on X axis. Now, my question is how do I get him moving in a slow motion maner so the Player should have the time to avoid intersecting him on the X axis.
I have tried two different things which I commented them with //PROBLEM in the script:
- GetComponent ().velocity = playerPosition * moveSpeed;
This line lets me to create some sort of a Slow Motion movement for the Enemy object. The thing is, using this code, the Enemy doesn’t inherit the Players position. It doesn’t stop to Player’s X position. I have increased the Angular Drag value from the RigidBody component just to see if I can achieve a similar result to what I want but it doesn’t. It moves the Enemy object with a certain speed until the Player stops from moving around.
- myTransform.position = playerPosition * moveSpeed;
I tried this as well. Using this line, the Enemy object inherited the Player’s X position. The thing is, it follows the EXACT position which I don’t want to. I want to give the Player the chance to avoid this object. That is why I tried to include the “moveSpeed” value which it did not work. Instead of decreasing the speed of the Enemy object, it multiplied the playerPosition with the value that I gave to “moveSpeed”.
For example:
if moveSpeed = 1 => Enemy X position = Player X Position;
if moveSpeed = 0.5 => Enemy X position = Player X Positon / 2;
if moveSpeed = 2 => Enemy X position = Player X Position * 2;
This is not the result that I want.
I want the Enemy to inherit the Player’s X Position but at the same time to slow down the movement of the Enemy object so the Player can avoid it.
Can someone help me with this?
PS: I will include a Y movement too but this movement will be constant so I don’t belive it will be too troublesome. I already created that for other objects in the game using the tutorial that I mentioned from the start.