Last active
February 8, 2017 13:34
-
-
Save onionmk2/40316d201b5d9b35053e82ccc4050a19 to your computer and use it in GitHub Desktop.
this is inelegant but it works
This file contains 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
using UnityEngine; | |
public class Paddle : MonoBehaviour | |
{ | |
void Update() | |
{ | |
var cameraObj = GameObject.FindWithTag("MainCamera"); | |
var camera = cameraObj.GetComponent(typeof(Camera)) as Camera; | |
if (camera == null) return; | |
var backgroundImageObj = GameObject.Find("BackgroundImage"); | |
var backgroundImage = backgroundImageObj.GetComponent(typeof(SpriteRenderer)) as SpriteRenderer; | |
if (backgroundImage == null) return; | |
var paddleObj = GameObject.Find("Paddle"); | |
var paddle = paddleObj.GetComponent(typeof(SpriteRenderer)) as SpriteRenderer; | |
if (paddle == null) return; | |
var mouseX = camera.ScreenToWorldPoint(Input.mousePosition).x; | |
var leftTopOfSprite = backgroundImage.sprite.rect.min; | |
var rightBottomOfSprite = backgroundImage.sprite.rect.max; | |
var paddleWidth = paddle.sprite.rect.width / paddle.sprite.pixelsPerUnit; | |
var xMin = camera.ScreenToWorldPoint(leftTopOfSprite).x + paddleWidth; | |
var xMax = camera.ScreenToWorldPoint(rightBottomOfSprite).x - paddleWidth; | |
var x = Mathf.Clamp(mouseX, xMin, xMax); | |
transform.position = new Vector3(x, transform.position.y, transform.position.z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment