Ok… a bit more work is needed then!
Any chance you can put the whole project up somewhere for me to download?
The game object being passed in / referenced should be a “Paddle” - e.g. the actual paddle game object itself…
I did miss defining the reversePaddlePosInBlocks as a float though… so yes that’s wrong… damn, and there’s an extra bracket in there too!.. (correct in the above now - sorry)
Can you package up the whole thing for me to grab and open in Unity? I can look at it right now
Updated Wed Dec 28 2016 00:13
Ok - I downloaded the complete course version from the end of the section in the end…
Opened the project and converted to Unity 5 but didn’t make any code changes with regards to the deprecated methods.
I’ve realised that it not so much the theory but the maths that is at fault with my previous solution.
It looks like a little offset is being generated and I’ve not quite worked out why yet. If you watch the video below you will see that one paddle is able to reach the edge before the other and then vice versa.
I’m thinking about the mouse position at the start of the game at the moment, e.g. both paddles are not perfectly aligned because the mouse isn’t in the middle of the screen when the game is run, it’s slightly off depending where you click on the start button.
Updated Wed Dec 28 2016 00:53
Ok - fixed…
On the line where I have;
float reversedMousePosInBlocks = (maxX + (mousePosInBlocks * -1));
…in this specific example, it’s a bit odd because min and max have been set to 1.18 and 14.8 respectively, I believe this is to do with the angled parts of the paddle, so your own game may vary slightly, but that said, as long as you factor in these values which total the width of the playable space + the mouse position in block multiplied by -1 (to reverse the value)
Anyway… the fix for this is;
float reversedMousePosInBlocks = ((maxX+minX) + (mousePosInBlocks * -1));
It now works, I believe, as you want - the code is a bit of a mess and can certainly be tidied up, and there are a few things that I personally would want to change in the rather hack and slash way I’ve cobbled this together… but - it works…
Paddle.cs
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public bool autoPlay = false;
public float minX, maxX;
private Ball ball;
public ReversedPaddle reversedPaddle;
void Start () {
ball = GameObject.FindObjectOfType<Ball>();
}
// Update is called once per frame
void Update () {
if (!autoPlay) {
MoveWithMouse();
} 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 (0f, this.transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp(mousePosInBlocks, minX, maxX);
this.transform.position = paddlePos;
Vector3 reversedPaddlePos = new Vector3(0f, reversedPaddle.transform.position.y, 0f);
float reversedMousePosInBlocks = ((maxX+minX) + (mousePosInBlocks * -1));
reversedPaddlePos.x = Mathf.Clamp(reversedMousePosInBlocks, minX, maxX);
reversedPaddle.transform.position = reversedPaddlePos;
}
}
ReversedPaddle.cs
using UnityEngine;
using System.Collections;
public class ReversedPaddle : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}