Hi Teacher and every body !
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public float minX, maxX;
//gioi han 2 ben man hinh cua paddle = Inspector
private Ball ball;
void Start () {
ball = GameObject.FindObjectOfType<Ball>();
print ("ball");
}
void Update () {
MoveWithMouse ();
Cheatcode ();
}
void Cheatcode (){
if (Input.GetMouseButtonDown (2)) {
Vector3 PaddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
Vector3 ballPos = ball.transform.position;
PaddlePos.x = Mathf.Clamp (ballPos.x, minX, maxX);
// dong code 15 la su gioi han di chuyen ben trai va ben phai paddle
// ko cho vuot ra khoi truc x background image
this.transform.position = PaddlePos;
}
}
void MoveWithMouse(){
// Update is called once per frame
if (Input.GetMouseButtonDown (0)) {
Vector3 PaddlePos = new Vector3 (0.5f,this.transform.position.y,0f);
float mousePosInBlocks =Input.mousePosition.x/Screen.width * 16;
PaddlePos.x = Mathf.Clamp (mousePosInBlocks,minX,maxX);
// dong code 15 la su gioi han di chuyen ben trai va ben phai paddle
// ko cho vuot ra khoi truc x background image
this.transform.position = PaddlePos;
//print (mousePosInBlocks) ; < == dong cod nay de coi vi tri paddle dichuyen
//luu y cac code phai theo thu tu,sai thu tu se ko co tac dung xay ra}
}
}
}
this code build still succesful but it not work like my expect my wish expect
if i click mouse middle so paddle move with ball (" autoPlay = true ")
else if i click mouse left and every thing revert : paddle move with mouse
… but it not work my expect about it
sorry i’m not good English
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public bool autoPlay = false;
public float minX, maxX;
private Ball ball;
void Start () {
ball = GameObject.FindObjectOfType<Ball>();
}
// Update is called once per frame
void Update () {
if (!autoPlay) {
MoveWithMouse();
Cheatcode(); // my new voi update
} else {
AutoPlay();
}
}
void AutoPlay() {
Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
Vector3 ballPos = ball.transform.position;
paddlePos.x = Mathf.Clamp(ballPos.x, minX, maxX);
this.transform.position = paddlePos;
}
void MoveWithMouse () {
Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp(mousePosInBlocks, minX, maxX);
this.transform.position = paddlePos;
}
void Cheatcode (){ // my new voi update
if (Input.GetMouseButtonDown (2)) {
autoPlay = true; // it worked !!
} else if (Input.GetMouseButtonDown (0)) {
autoPlay = false; // but not working ?? why ??????
}
}
}