martes, 18 de octubre de 2016

player who shoots

using UnityEngine;
using System.Collections;

public class PlayerControler : MonoBehaviour {

public float speed;

//to jump:
private bool jump;
private Rigidbody rb;

//to shoot:
public Rigidbody projectile;
public Transform shotpos;
public float shotForce;

// Use this for initialization
void Start () {
jump = true;
rb = GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {
float x = Input.GetAxis ("Horizontal");
transform.Translate (x * Time.deltaTime * speed, 0, 0);

if (Input.GetKeyDown (KeyCode.LeftControl)) {
//fire projectile
Rigidbody shot = Instantiate(projectile, shotpos.position, Quaternion.identity) as Rigidbody;
shot.AddForce (shotpos.right * shotForce);

}

if (Input.GetKeyDown (KeyCode.Space)) {
//jump
//jump=true;
if(jump){
rb.AddForce (transform.up * 700);
jump = false;
}

}
}

void OnTriggerEnter(){
transform.position = new Vector3 (-9,0,0);
}

void OnCollisionEnter(Collision col){
if (col.gameObject.CompareTag ("floor")) {
jump = true;
}
}
}

Bullet control

using UnityEngine;
using System.Collections;

public class BulletControl : MonoBehaviour {



// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnCollisionEnter(Collision col){
if (col.gameObject.CompareTag ("enemy")) {
Destroy (col.gameObject);
Destroy (gameObject);
}
}

void OnTriggerEnter(){
Destroy (gameObject);
}
}

Motion

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(NavMeshAgent))]
public class Motion : MonoBehaviour {

CharacterController cc;
NavMeshAgent agent;

public Transform target;

public Vector3 objective;

// Use this for initialization
void Start () {

cc = GetComponent<CharacterController> ();
agent = GetComponent<NavMeshAgent> ();
objective = Vector3.zero;
}

// Update is called once per frame
void Update () {

float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
//cc.Move (new Vector3 (h, 0, v) * Time.deltaTime);
//cc.SimpleMove(new Vector3(h, 0, v));

if (!cc.isGrounded) {

Debug.Log ("FREE FALLIN");
}

if (Input.GetMouseButtonUp (0)) {

Ray mouseRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(mouseRay, out hit)){

objective = hit.point;
}
}

agent.destination = objective;
}

void OnControllerColliderHit(ControllerColliderHit c) {

//Debug.Log (c.transform.name);
}
}

domingo, 16 de octubre de 2016

using the pathfinding algorithm (ver.2HW)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class enemymovement : MonoBehaviour {

public float threshold;
int pathstep=0;
int newCurrent;

Node nearest;
Node inicio;

bool recorre;
bool cercano;
bool oneTime;

public Node[] allNodes;
private int currntNode;

private List<Node> myPath;


// Use this for initialization
void Start () {

recorre = true;
cercano = false;
nearest = allNodes [0];
oneTime = false;

}

// Update is called once per frame
void Update () {

if (recorre==true) {

transform.LookAt (allNodes[currntNode].transform);
transform.Translate (transform.forward * 5 * Time.deltaTime, Space.World);

float distance = Vector3.Distance (allNodes[currntNode].transform.position, transform.position);
if (distance < threshold) {
currntNode++;
currntNode %= allNodes.Length;
}
}

if (Input.GetMouseButtonUp(0)) {
recorre = false;
inicio = allNodes [currntNode];

float distancen;
float distancej;

for (int j = 0; j < allNodes.Length; j++) {
distancen=(Input.mousePosition - Camera.main.WorldToScreenPoint(nearest.transform.position)).magnitude;
distancej=(Input.mousePosition - Camera.main.WorldToScreenPoint(allNodes[j].transform.position)).magnitude;
if (distancej<distancen) {
nearest = allNodes [j];
newCurrent = j;
}
}
cercano = true;
}

if (cercano) {
if (!oneTime) {
findNearest ();
oneTime = true;
}
transform.LookAt (myPath [pathstep].transform);
transform.Translate (transform.forward * 5 * Time.deltaTime, Space.World);
float distance= Vector3.Distance(myPath[pathstep].transform.position,transform.position);
if (distance < threshold) {
pathstep++;
}
if (pathstep >= myPath.Count) {
cercano = false;
oneTime = false;
pathstep = 0;
recorre = true;
currntNode = newCurrent;
}
}


}

void findNearest(){
List<Node> path= Pathfinding.BreadthwiseSearch(inicio,nearest);
for (int i = 0; i < path.Count; i++) {

Debug.Log (path [i].transform.name);
}
myPath = new List<Node> (path);
}

}

Path test (using the algorithms)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PathTest : MonoBehaviour {

public Node start;
public Node end;
public float threshold;

private List<Node> myPath;
private int currntNode;

// Use this for initialization
void Start () {

Debug.Log ("Breathwise search:");
List<Node> path = Pathfinding.BreadthwiseSearch (start, end);
for (int i = 0; i < path.Count; i++) {

Debug.Log (path [i].transform.name);
}

myPath = new List<Node> (path);

Debug.Log ("Depthwise search:");
path = Pathfinding.DepthwiseSearch (start, end);
for (int i = 0; i < path.Count; i++) {

Debug.Log (path [i].transform.name);
}
Debug.Log ("A* search:");
path = Pathfinding.AStar(start, end);
for (int i = 0; i < path.Count; i++) {

Debug.Log (path [i].transform.name);
}
}

// Update is called once per frame
void Update () {
//2 things
//-motion (as smooth as posible)
//-input(as responsive as posible)
transform.LookAt (myPath [currntNode].transform);
transform.Translate (transform.forward * 5 * Time.deltaTime, Space.World);

//this is not OK
float distance= Vector3.Distance(myPath[currntNode].transform.position,transform.position);
if (distance < threshold) {
currntNode++;
currntNode %= myPath.Count;
}

}
}

node.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Node : MonoBehaviour {

public Node[] neighbors;
public List<Node> history;

public float g;
public float f;

void OnDrawGizmos(){

Gizmos.color = Color.green;
for (int i = 0; i < neighbors.Length; i++) {
Gizmos.DrawLine (transform.position, neighbors [i].transform.position);
}
}
}

To use pathfinding algorithms:


  • complete graph of nodes

  • each node has an array of neighbors

  • start and end 

  • thas it