About autoPlay "enable checking" but cannot disable

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 ??????
		}
	}
}

Call Cheatcode() before the if statement in Update

Currently, once autoPlay is true, Cheatcode will never get called again.

oh thankyou sir !!!
i will go on the scource Learnt to Code … for knowledge about that

but … i think the " void Update only call one only"
i can’t void Update two
ican’t …

Start runs once, Update runs continuously

Try this

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

    // check button and set autoplay flag
    Cheatcode(); // my new voi update

    if (!autoPlay) {
		MoveWithMouse();		
	} else {
		AutoPlay();
	}
}

I would also only check the user is pressing “autoplay off” button in Cheatcode if autoplay is true, and vice versa… because you don’t actually need to check if user is pressing off, if autoplay is false etc

void Cheatcode (){ // my new voi update
	if (!autoplay && Input.GetMouseButtonDown (2)) {
		autoPlay = true;
	} else if (autoplay && Input.GetMouseButtonDown (0)) {
		autoPlay = false; 
	}
}
1 Like

cool !!!
i was succesful then you teach me syntax " && "
thankyou teacher very much !!!

1 Like

Privacy & Terms