标签:
一、屏幕坐标转换为世界坐标。
1.游戏逻辑,根据界面布局,将墙体控制到对应的位置;
vector3 position=Camer.main.ScreenToWorldPoint(new vetor2(Screen.));//得到最右上的点。
位置有了,解决大小。通过自身的BoxCollider中的size来设置墙体的大小。
设置墙体就很容易解决。
void ResetWall(){
rightWall = transform.Find ("rightWall").GetComponent<BoxCollider2D> ();
leftWall = transform.Find ("leftWall").GetComponent<BoxCollider2D> ();
upWall = transform.Find ("upWall").GetComponent<BoxCollider2D> ();
downWall = transform.Find ("downWall").GetComponent<BoxCollider2D> ();
//屏幕坐标转换世界坐标,自适应于不同屏幕的变化
Vector3 upWallPosition = Camera.main.ScreenToWorldPoint (new Vector2(Screen.width/2f,Screen.height));
upWall.transform.position = upWallPosition+new Vector3(0,0.5f,0);
float width= Camera.main.ScreenToWorldPoint(new Vector2(Screen.width,Screen.height)).x*2f;
upWall.size = new Vector2(width,1f);
downWall.transform.position = -upWall.transform.position;
downWall.size = upWall.size;
Vector3 position = Camera.main.ScreenToWorldPoint (new Vector2(Screen.width,Screen.height));
rightWall.transform.position = new Vector3 (position.x,0,0)+new Vector3(0.5f,0,0);
rightWall.size = new Vector2 (1, position.y * 2f);
leftWall.transform.position = -rightWall.transform.position;
leftWall.size = rightWall.size;
}
二、主角的控制
1.主要的亮点在于设置keyCode可以设置多个角色用于对战的游戏。2.通过刚体组件rigidbody.velocity(new vector2())来控制移动;
public class PlayerContrl : MonoBehaviour {
public KeyCode keyUp;
public KeyCode keyDown;
public float speed=10;
private Rigidbody2D rd;
private AudioSource audio;
// Use this for initialization
void Start () {
rd = GetComponent<Rigidbody2D> ();
audio = GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKey (keyUp)) {
rd.velocity = new Vector2 (0, speed);
}
else if(Input.GetKey(keyDown)){
rd.velocity = new Vector2 (0, -speed);
}
else {
rd.velocity = new Vector2 (0,0);
}
}
void OnCollisionEnter2D(){
audio.pitch = Random.Range (0.8f,1.2f);
audio.Play ();
}
}
三、球自身的运动
1.球的运动用的是:rigidbody.AddForce(new Vcetor());方向是取随机数来可控制Random.Ranage();
2.在Update()控制球的速度变化不大。//(velocity.x<9&&velocity.x>-9&&velocity.x!=0)
//代码
public class Boll : MonoBehaviour {
private Rigidbody2D rd;
// Use this for initialization
void Start () {
rd = GetComponent<Rigidbody2D> ();
GoBoll ();
}
public void ResetBoll(){
transform.position = Vector3.zero;
GoBoll ();
}
void GoBoll(){
int number = Random.Range (0,2);//随机方向
if (number == 1) {
rd.AddForce (new Vector2 (100, 0));
} else {
rd.AddForce (new Vector2 (-100,0));
}
}
void OnCollisionEnter2D(Collision2D col){
if(col.collider.tag=="Player"){
//Debug.Log ("hit");
Vector2 velocity = rd.velocity;
velocity.y =( velocity.y + col.rigidbody.velocity.y)/2f;
rd.velocity = velocity;
}
if(col.gameObject.name=="rightWall"||col.gameObject.name=="leftWall"){
Gamemanage.instance.ChanageScore (col.gameObject.name);
}
}
// Update is called once per frame
void Update () {
Vector2 velocity = rd.velocity;
if(velocity.x<9&&velocity.x>-9&&velocity.x!=0){
if (velocity.x > 0) {
velocity.x = 10;
} else {
velocity.x = -10;
}
rd.velocity = velocity;
}
}
}
四、游戏的GameManage
1.学到是单例模式的运用;声明私有字段,设置可写属性。在Awake(){instance=this;}初始化。//就可在其他类中调用。
2.GameObject.Find().sendMessage("meoth");给游戏体发消息,调用它身上的方法。
3.碰撞检测的语法 void OnCollisionEnter2D(Collision2D col){
if(col.collider.tag=="tag"){
}
}
4.音频播放:
private AudioSource audio;
在Start()里audio=GetCompent<>(AudioSource);
在需要的地方调用audio.Play()等控制音频的方法。
代码:
//private static Gamemanage _instance;
public static Gamemanage instance{
get
{
return _instance;
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Gamemanage : MonoBehaviour {
//单例模式
private static Gamemanage _instance;
public static Gamemanage instance{
get
{
return _instance;
}
}
private BoxCollider2D rightWall;
private BoxCollider2D leftWall;
private BoxCollider2D upWall;
private BoxCollider2D downWall;
public Transform player1;
public Transform player2;
private int score1;
private int score2;
public Text score1Text;
public Text score2Text;
void Awake(){
_instance = this;
}
// Use this for initialization
void Start () {
ResetWall ();
ResetPlayer ();
}
// Update is called once per frame
void Update () {
}
public void ChanageScore(string name){
if (name == "leftWall") {
score2++;
} else if (name == "rightWall") {
score1++;
}
score1Text.text = score1.ToString ();
score2Text.text = score2.ToString ();
}
public void ResetBtOnClick(){
score1 = 0;
score2 = 0;
score1Text.text = score1.ToString ();
score2Text.text = score2.ToString ();
GameObject.Find ("Ball").SendMessage ("ResetBoll");
}
void ResetWall(){
rightWall = transform.Find ("rightWall").GetComponent<BoxCollider2D> ();
leftWall = transform.Find ("leftWall").GetComponent<BoxCollider2D> ();
upWall = transform.Find ("upWall").GetComponent<BoxCollider2D> ();
downWall = transform.Find ("downWall").GetComponent<BoxCollider2D> ();
//屏幕坐标转换世界坐标,自适应于不同屏幕的变化
Vector3 upWallPosition = Camera.main.ScreenToWorldPoint (new Vector2(Screen.width/2f,Screen.height));
upWall.transform.position = upWallPosition+new Vector3(0,0.5f,0);
float width= Camera.main.ScreenToWorldPoint(new Vector2(Screen.width,Screen.height)).x*2f;
upWall.size = new Vector2(width,1f);
downWall.transform.position = -upWall.transform.position;
downWall.size = upWall.size;
Vector3 position = Camera.main.ScreenToWorldPoint (new Vector2(Screen.width,Screen.height));
rightWall.transform.position = new Vector3 (position.x,0,0)+new Vector3(0.5f,0,0);
rightWall.size = new Vector2 (1, position.y * 2f);
leftWall.transform.position = -rightWall.transform.position;
leftWall.size = rightWall.size;
}
void ResetPlayer(){
Vector3 player1Position = Camera.main.ScreenToWorldPoint (new Vector3 (100,Screen.height/2,0));
player1Position.z = 0;
player1.transform.position = player1Position;
Vector3 player2Position = Camera.main.ScreenToWorldPoint (new Vector3(Screen.width-100,Screen.height/2,0));
player2Position.z = 0;
player2.transform.position = player2Position;
}
}
标签:
原文地址:http://www.cnblogs.com/ouyangJJ/p/5742802.html