Hi me and my friend have been looking around and decided to use Unity to create a top down shooter so we found Evac City tutorial.
After guiding each other along we began to scrip the AIscript
However we cannot get it to work correctly and were not sure why not
Our current code is
using UnityEngine;
using System.Collections;
public class AIscript : MonoBehaviour {
//game objects (variables which point to game objects)
private GameObject objPlayer;
private GameObject objCamera;
//input variables (variables used to process and handle input)
private Vector3 inputRotation;
private Vector3 inputMovement;
//indentity variables (variables specific to the game object)
public float moveSpeed = 100f;
private bool thisIsPlayer;
//calculation variables (variables used for calculation)
private Vector3 tempVector;
private Vector3 tempVector2;
//Use this for initialization
void Start () {
objPlayer = (GameObject) GameObject.FindWithTag ("Player");
objCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
if (gameObject.tag == "Player") { thisIsPlayer = true; }
}
// update is called once per frame
void Update() {
FindInput();
ProcessMovement();
if (thisIsPlayer == true)
{
HandleCamera();
}
}
void FindInput ()
{
if (thisIsPlayer == true)
{
FindPlayerInput();
} else {
FindAIinput();
}
}
void FindPlayerInput ()
{
//find vector to move
inputMovement = new Vector3( Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical") );
//find vector to mouse
tempVector2 = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f); //the position of the middle of the screen
tempVector = Input.mousePosition; //find the position of the mouse on the screen
tempVector.z = tempVector.y; //input mouse position gives us 2D coordinates, I am moving the Y coordinate to the Z coordinate in temp vector and setting the Y (up and down) axis, and not the X and Y (in and out of screen) axis
tempVector.y = 0;
Debug.Log(tempVector);
inputRotation = tempVector - tempVector2; // the direction we want face/aim/shoot is from the middle of the screen to where the mouse is pointing
}
void FindAIinput()
{
}
void ProcessMovement()
{
rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = new Vector3(0,transform.eulerAngles.y + 180,0);
transform.position = new Vector3(transform.position.x,0,transform.position.z);
}
void HandleCamera()
{
objCamera.transform.position = new Vector3(transform.position.x,15,transform.position.z);
objCamera.transform.eulerAngles = new Vector3(90,0,0);
}
}
The end result is http://i.imgur.com/wPYWPf5.png However in the tutorial it says
“If you now click the play button
at the top middle of your Unity interface you should be able to play the game and move your character
quite awkwardly around.”
Here is the tutorial pdf http://www.rebelplanetcreations.com/downloads/Other/Tutorials/HowToMakeAGameInUnity3D.pdf