Question

A simple AI code for air hockey using unity3D? No need for boundaries and strikers

A simple AI code for air hockey using unity3D? No need for boundaries and strikers

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Player.cs

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
BoxCollider coll;
Rigidbody striker;
GameObject st;
public float playerSpeed; //set value from inspector
public float strikerSpeed; //speed at which striker moves
AI ai;

void Awake() {
strikerSpeed = PlayerPrefs.GetFloat ("Striker_Speed");
}

// Use this for initialization
void Start () {
ai = GameObject.Find ("AI").GetComponent<AI> ();
coll = GameObject.Find ("Table").GetComponent<BoxCollider> ();
striker = GameObject.FindGameObjectWithTag ("Striker").GetComponent<Rigidbody> ();
st = GameObject.FindGameObjectWithTag ("Striker");
}

// Update is called once per frame
void Update () {
//Input to move the player
if (Input.GetKey ("left"))
transform.Translate (-playerSpeed * Time.deltaTime, 0f, 0f);
if (Input.GetKey ("right"))
transform.Translate (playerSpeed * Time.deltaTime, 0f, 0f);
if (Input.GetKey ("up"))
transform.Translate (0f, 0f, playerSpeed * Time.deltaTime);
if (Input.GetKey ("down"))
transform.Translate (0f, 0f, -playerSpeed * Time.deltaTime);

//Collision detection with edges, basically we are restricting player movement
if (transform.position.x <= -4.74f)
transform.position = new Vector3 (-4.74f, transform.position.y, transform.position.z);
if (transform.position.x >= 4.74f)
transform.position = new Vector3 (4.74f, transform.position.y, transform.position.z);
if (transform.position.z >= -1f)
transform.position = new Vector3 ( transform.position.x, transform.position.y, -1f);
if (transform.position.z <= -8.4f)
transform.position = new Vector3 ( transform.position.x, transform.position.y, -8.4f);



}

void OnCollisionEnter(Collision c) {
if (c.gameObject.tag == "Striker") {
ai.counter = 0f; //see AI.cs for explanation

//Controls to hit the striker
if (Input.GetKey ("space")) { //if you keep space pressed and up arrow key and then touch, stiker is smashed
  //---Control Part---
if (Input.GetKey ("up")) {
if (Input.GetKey ("right")) {
striker.velocity = new Vector3 (strikerSpeed, striker.velocity.y, strikerSpeed);
} else {
striker.velocity = new Vector3 (-strikerSpeed, striker.velocity.y, strikerSpeed);
}
}



}

else { //no space pressed and then touch then a gentle push is given

if (Input.GetKey ("right")) {
striker.velocity = new Vector3 (strikerSpeed * 0.5f, striker.velocity.y, strikerSpeed * 0.60f);
} else {
striker.velocity = new Vector3 (strikerSpeed * -0.5f, striker.velocity.y, strikerSpeed * 0.60f);
}
}
}

}

}

-------END OF Player.cs---------------

AI.cs

using UnityEngine;

using System.Collections;

public class AI : MonoBehaviour {

public float speed;

GameObject striker;

Rigidbody strikerRB;

float forceDir;

public float counter;

public float strikerSpeed;

Vector3 basePoint;

public float difficulty;

float smashCance;

void Awake() {

difficulty = PlayerPrefs.GetFloat ("Difficulty");

}

// Use this for initialization

void Start () {

striker = GameObject.FindGameObjectWithTag ("Striker");

strikerRB = GameObject.FindGameObjectWithTag ("Striker").GetComponent<Rigidbody> ();

//set difficulty

//based on the value the more the difficulty the more behind or close to the goal the ai is

//it plays defensive and gets extra time to think and play in high difficulty

//ideally difficuly = 1 must be invincible, if there are no game glitches

if (difficulty < 0.45f) { //easy

basePoint = new Vector3 (0.88f, transform.position.y, 0.85f);

difficulty = 0.2f;

} else if (difficulty >= 0.45f && difficulty < 1f) {

basePoint = new Vector3 (0.88f, transform.position.y, 3.3f);

difficulty = 0.7f;

}

else if(difficulty == 1f){

basePoint = new Vector3 (0.88f, transform.position.y, 6.65f);

}

}

// Update is called once per frame

void Update () {

counter += 1f * Time.deltaTime; //acts like a timer

Debug.Log (counter);

if(striker.transform.position.z >= -0.2f) { //if striker is in its half

if (counter >= 1f) { //wait for one second to see if the stiker comes to you or stops, if it does not come then :

Vector3 newPos = new Vector3 (striker.transform.position.x -0.5f, striker.transform.position.y, striker.transform.position.z + 0.5f);

//move towards the striker to its position

transform.position = Vector3.MoveTowards (transform.position, newPos, 4f * Time.deltaTime);

//4f * Time.deltaTime states time to transit the two positions

} else //if less than 1 second change your x-position based on difficulty, i.e. try to move closer to striker

transform.position = new Vector3 (striker.transform.position.x * difficulty, transform.position.y, transform.position.z);

}

else //if in other half then move towards base position

transform.position = Vector3.MoveTowards (transform.position, basePoint, 2f * Time.deltaTime);

}

void OnCollisionEnter(Collision c) {

if (c.gameObject.tag == "Striker") {

counter = 0f; //on hitting reset the wait time...

forceDir = (int)Random.Range (0, 10);//for hitting to the left or right

smashCance = (int)Random.Range (0, 10); //chance that it will smash the striker

//hit the striker with force

if (forceDir <= 5)

strikerRB.velocity = new Vector3 (-strikerSpeed, strikerRB.velocity.y, -strikerSpeed);

else

strikerRB.velocity = new Vector3 (strikerSpeed, strikerRB.velocity.y, -strikerSpeed);

}

}

}

--------END OF AI.cs---------

Striker.cs

using UnityEngine;
using System.Collections;

public class Striker : MonoBehaviour {
GameManager gm;

void Awake() {

}

// Use this for initialization
void Start () {
gm = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<GameManager> ();
}

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

}

void OnTriggerEnter(Collider c) {
if (c.tag == "AI_GOAL") {
gm.pScore += 1f;
gm.Reset (1);
}
if (c.tag == "PLAYER_GOAL") {
gm.eScore += 1f;
gm.Reset (0);
}
}


}



-----------END OF Striker.cs----------------




GameManager.cs

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
GameObject striker,player,ai,pS,eS; //pS - playerScore, eS - enemyScore

//for score
public float pScore,eScore; //p-player,e-enemy


// Use this for initialization
void Start () {
pScore = 0f;
eScore = 0f;
striker = GameObject.FindGameObjectWithTag ("Striker");
player = GameObject.FindGameObjectWithTag ("Player");
ai = GameObject.Find ("AI");
pS = GameObject.Find ("P_Score");
eS = GameObject.Find ("E_Score");
}

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

pS.gameObject.GetComponent<TextMesh> ().text = "YOU:" + pScore;
eS.gameObject.GetComponent<TextMesh> ().text = "OPP:" + eScore;
}

public void Reset(int status) {
//Reset all the gameObjects to original position to start a new game session
if (status == 1) //recieved when striker hits any goal, check Striker.cs
striker.transform.position = new Vector3 (0.06f, striker.transform.position.y, 2.82f);//if enemy wins,
//place on player side
else
striker.transform.position = new Vector3 (0.06f, striker.transform.position.y, -4.59f);

striker.gameObject.GetComponent<Rigidbody>().velocity = new Vector3 (0f,0f,0f);//stop the striker
player.transform.position = new Vector3 (0.88f, player.transform.position.y, -8.17f);
ai.transform.position = new Vector3 (0.88f, ai.transform.position.y, 6.65f);
}


}

-----------<END OF GameManager.cs>-------------

Add a comment
Know the answer?
Add Answer to:
A simple AI code for air hockey using unity3D? No need for boundaries and strikers
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT