- Home /
Question by
awplays49 · Dec 18, 2014 at 02:09 AM ·
not working
Rotation of player occurs continuously when on the left side of the screen
This code:
using UnityEngine;
using System.Collections;
public class PlayerRotation : MonoBehaviour {
public float Sensitivity;
private Vector3 MousePos;
private Vector3 WorldPos;
private float MouseXSet;
void Start () {
MousePos = Input.mousePosition;
MouseXSet = 0;
}
void Update () {
MousePos = Input.mousePosition;
MousePos = Camera.main.WorldToScreenPoint (MousePos);
if (MousePos.x > MouseXSet)
{
rigidbody.transform.Rotate (new Vector3 (0, 1, 0) * Sensitivity * Time.deltaTime);
MouseXSet = Input.mousePosition.x;
}
if (MousePos.x < MouseXSet)
{
rigidbody.transform.Rotate (new Vector3 (0, -1, 0) * Sensitivity * Time.deltaTime);
MouseXSet = Input.mousePosition.x;
}
}
}
Comment
Best Answer
Answer by HYPERSAVV · Dec 18, 2014 at 02:49 AM
MousePos = Input.mousePosition;
MousePos = Camera.main.WorldToScreenPoint (MousePos);
Here you are converting a screen point (treated as a point in world-space) to a screen point, which is probably causing some issues. Screen-space has an origin on the bottom-left, World-space is not guaranteed to line-up (and is most likely the center of your screen). You should be fine just grabbing your mouse position and comparing that to MouseXSet.
Your answer
Follow this Question
Related Questions
Script not working on clones 1 Answer
animation transitioning but not playing?? (still need help) 1 Answer
New project is taking a very long time to load 1 Answer
Unity Will not run 0 Answers
OnMouseDown not working when deployed 2 Answers