A simple AI code for air hockey using unity3D? No need for boundaries and strikers
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>-------------
A simple AI code for air hockey using unity3D? No need for boundaries and strikers
Write the Matlab code to model and generate the surfaces using boundaries. Question given below is to be solved using matlab Determine the point on the bilinear surface defined by P(0,0)=[0 0 1], P(0,1)=[1 1 1],P(1,0)=[1 0 0],P(1,1)=[0 1 0], corresponding to u=v=0.5 in parametric space.
Need a simple C++ code that read a text file of integers, puts the integers in a array and using quick sort, sorts the numbers.
Complete the calm air, dust, m ai dast concept map using the following terms and phrases: sulfiurous smog ar overlain by warm air, smoke, fossil fuels are burned. 31 which produces 32 Sulfur dioxide which form 34 which becomes trapped by 35 36.
Simple code using Java that uses Naive Query Retrieval
Most successful organizations minimize the need for explicit rules, regulations, and other boundaries by _____________. Select one: a. posting written statements of the organizational goals and objectives b. discouraging the formation of subcultures that isolate work groups c. designing effective reward systems d. encouraging employees to see themselves as free agents Individual rationality ______________ organizational rationality. Select one: a. is a good indicator of b. will ensure c. is often the opposite of d. does not always guarantee As firms...
Write a simple C language code using MPLABX IDE that explains an operation then rewrite the same Code but using assembler code language and explain how the codes work.
can someone help write a simple c++ code that inputs a file and decrypts it using ascii code -4.
Using SIMPLE (not conditional) Signal Assignment, design the following circuit by VHDL code
5. Write the Verilog code using the behavioral algorithmic approach based on a simple loop. 6. Write the testbench code to test the design in (5).
Simple code using Java to create a program that uses TF-IDF algorithm. Please add step by step on how to run the code.