M6 2016 OccTech & Computer
Thursday, June 9, 2016
COMPUTER-FUTBALL PROJECT
Here is the link to the project so far LINK
- Download it and unpack it (unzip it) to a folder in your D drive
- We will add goals to the game and a game manager to keep up with the score.
- Download it and unpack it (unzip it) to a folder in your D drive
- We will add goals to the game and a game manager to keep up with the score.
Thursday, June 2, 2016
GOAL CODE
using UnityEngine;
using System.Collections;
public class GoalOne : MonoBehaviour {
public GameObject ball;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.layer == LayerMask.NameToLayer("Water"))
{
Debug.Log (other.gameObject.layer);
if(ball != null){
ball.GetComponent<Transform>().position =new Vector3(0,2f,0);
}
}
}
using System.Collections;
public class GoalOne : MonoBehaviour {
public GameObject ball;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.layer == LayerMask.NameToLayer("Water"))
{
Debug.Log (other.gameObject.layer);
if(ball != null){
ball.GetComponent<Transform>().position =new Vector3(0,2f,0);
}
}
}
KickBall Code
public class PlayerOne : MonoBehaviour {
public GameObject ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 pos = GetComponent<Transform>().position;
if(Input.GetKey (KeyCode.D))
{
pos.z += 0.1f;
}
if(Input.GetKey (KeyCode.A))
{
pos.z -= 0.1f;
}
if(Input.GetKey (KeyCode.W))
{
pos.x -= 0.1f;
}if(Input.GetKey (KeyCode.S))
{
pos.x += 0.1f;
}
GetComponent<Transform>().position = pos;
if(Input.GetKeyDown(KeyCode.G) && ball != null)
{
Vector3 ballPos = ball.GetComponent<Transform>().position;
float distance = Vector3.Distance (ballPos, pos);
if(distance <1.5f)
{
Vector3 direction = ballPos - pos;
direction.Normalize();
ball.GetComponent<Rigidbody>().AddForce(direction * 100);
}
}
}
}
public GameObject ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 pos = GetComponent<Transform>().position;
if(Input.GetKey (KeyCode.D))
{
pos.z += 0.1f;
}
if(Input.GetKey (KeyCode.A))
{
pos.z -= 0.1f;
}
if(Input.GetKey (KeyCode.W))
{
pos.x -= 0.1f;
}if(Input.GetKey (KeyCode.S))
{
pos.x += 0.1f;
}
GetComponent<Transform>().position = pos;
if(Input.GetKeyDown(KeyCode.G) && ball != null)
{
Vector3 ballPos = ball.GetComponent<Transform>().position;
float distance = Vector3.Distance (ballPos, pos);
if(distance <1.5f)
{
Vector3 direction = ballPos - pos;
direction.Normalize();
ball.GetComponent<Rigidbody>().AddForce(direction * 100);
}
}
}
}
Subscribe to:
Posts (Atom)
