Last active
November 22, 2017 23:36
-
-
Save jcordeiro67/cd9838963906c1479c2342a05f97b3ae to your computer and use it in GitHub Desktop.
Automating the Paddle's clamp values relative to the paddle size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by: John Cordeiro | |
// [email protected] | |
using UnityEngine; | |
using System.Collections; | |
public class Paddle : MonoBehaviour { | |
// Set the public variables to private after functioning. | |
public float clampValLeft = .5f; | |
public float clampValRight = 15.5f; | |
private float tempClampVal; | |
private int gameUnits = 16; // Variable for the number of units in the width of the editor screne: Int | |
private float mousePosInUnits; // Variable to hold the mouse position in screne units: Float | |
private Vector3 paddlePos; // Varaible to hold and set the paddles position: Vector3(x,y,z) | |
private Vector3 paddleScale; // Variable to hold the paddles scale, used to set the clamp value of the paddle | |
void Start () { | |
paddleScale = this.transform.localScale; // Get the paddle scale and set to paddleScale to use the value of X | |
tempClampVal = clampValLeft * paddleScale.x; // Multiply the Paddle scale times the default clamp value | |
clampValLeft = tempClampVal; // Add the Value to the clampValueLeft | |
clampValRight = gameUnits - tempClampVal; // Subtract the tempClampVal from the gameUnits | |
} | |
void Update () { | |
paddlePos = new Vector3 (8f, this.transform.position.y, this.transform.position.z); // Default location of paddle in game | |
mousePosInUnits = Input.mousePosition.x / Screen.width * gameUnits; // Mouse position X / Screen width * Number of Game Units | |
paddlePos.x = Mathf.Clamp (mousePosInUnits, clampValLeft, clampValRight); // Clamps the mouse x value to the size of the screen | |
this.transform.position = paddlePos; // Moves the paddle with the mouse | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment