Moving GameObject with Mouse problem
my camera is at 0,0,0 as you can see in the script below, my mouseInput.x / ScreenWidth * 16 statement, i used the print method to see my mouse and see how big my screen play space is and it is 16 across with my mouse , my problem is when i hit play , my paddle starts of at the end of the right side(position 8) and only goes to the middle of the screen , when i move my mouse to the middle of the screen its at position 0 when i move the camera over half way to the right , it works perfectly (so it starts at 0), is there a reason why my paddle starts at the end of the right side instead of the middle. am i doing this wrong ? When i move the paddle with the arrow keys i dont have this problem. i already have a way to find out the left and right side of the camera and i have them in a variable called xmin and xmax and using it to restrict the paddle in between those points, i do not know what am i doing wrong. i am completely lost been at it for days. Also when i remove the Screen.Width / 16 from float MousePos = Input.mousePosition.x/ Screen.width * 16;
my paddle is placed out of screen to 480 on x posistion , so that did not work either. thank you i think i need to have it so it starts at what ever position its at right?
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour {
public float Speed = 1f;
float xmin;
float xmax;
float padding = 1f;
// Update is called once per frame
void Start(){
// get Right and left side of the camera
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3 (0, 0, distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance));
xmin = leftmost.x + padding;
xmax = rightmost.x - padding;
}
void Update () {
print(Input.mousePosition.x/ Screen.width * 16);
Movement ();
}
void Movement(){
//get mouse inout
float MousePos = Input.mousePosition.x/ Screen.width * 16;
// //constrain paddle to the right and left edge of camera.
float newX = Mathf.Clamp (MousePos, xmin, xmax);
this.transform.position = new Vector3 (newX
, transform.position.y, transform.position.z);
}
}
Answer by AngryTilde · Nov 11, 2015 at 02:34 AM
Since your camera is at 0,0,0 the left side of the view port is negative (in worldSpace), and as Screen size and Mouse Position start at 0 on the left hand side rather then the center you'll never see your paddle moving before the center point of your screen.
You can either A) ensure your cameras left most view x point is 0 or b) modify your script to account for the screen starting at 0 with something like this:
float offsetMousePos = (Input.mousePosition.x - (Screen.width * 0.5));
which would return the Mouse X relative to the mid point of the screen.
Your answer
![](https://koobas.hobune.stream/wayback/20220612044603im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation only plays in one of the objects 1 Answer
Load scene after battle 0 Answers
Action (by collider) 0 Answers