How to stop movement, when mouse is off screen - c#

I'm hoping there's someone out there that can help me with a small problem.
Currently I have an Input Manager attached to the main camera to allow the user to pan around the map by moving the mouse to the edges of the window, but I've encountered a slight problem which I've tried to fix myself to no avail.
If the mouse goes outside of the window, the panning still happens, which I find irritating when I'm debugging or using other applications. So I am hoping that someone can help me to stop the movement happening when the mouse is outside the game window.
Here is the code for my Input Manager.
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
public Vector3 position = new Vector3(0,0, -10);
public int boundary = 50;
public int speed = 4;
private int screenBoundsWidth;
private int screenBoundsHeight;
// Use this for initialization
void Start()
{
screenBoundsWidth = Screen.width;
screenBoundsHeight = Screen.height;
}
// Update is called once per frame
void Update()
{
if (Input.mousePosition.x > screenBoundsWidth - boundary) {
position.x += speed * Time.deltaTime;
}
if (Input.mousePosition.x < 0 + boundary) {
position.x -= speed * Time.deltaTime;
}
if (Input.mousePosition.y > screenBoundsHeight - 10) {
position.y += speed * Time.deltaTime;
}
if (Input.mousePosition.y < 0 + boundary) {
position.y -= speed * Time.deltaTime;
}
Camera.mainCamera.transform.position = position;
}
}
Thank you for your time.
EDIT
I have come up with a hacky work around, but it still causes the movement to happen in certain locations around the outside of the window. I am hoping someone can come up with a better solution.
if (Input.mousePosition.x < screenBoundsWidth && Input.mousePosition.y < screenBoundsHeight) {
if (Input.mousePosition.x > screenBoundsWidth - boundary) {
position.x += speed * Time.deltaTime;
}
}
if (Input.mousePosition.x > 0 && Input.mousePosition.y > 0) {
if (Input.mousePosition.x < 0 + boundary) {
position.x -= speed * Time.deltaTime;
}
}
if (Input.mousePosition.y < screenBoundsHeight && Input.mousePosition.x < screenBoundsWidth) {
if (Input.mousePosition.y > screenBoundsHeight - 22) {
position.y += speed * Time.deltaTime;
}
}
if (Input.mousePosition.y > 0 && Input.mousePosition.x > 0) {
if (Input.mousePosition.y < 0 + boundary) {
position.y -= speed * Time.deltaTime;
}
}

3 Ideas:
Rect screenRect = new Rect(0,0, Screen.width, Screen.height);
if (!screenRect.Contains(Input.mousePosition))
return;
The same can be written more verbously as:
float mouseX = Input.MousePosition.x;
float mouseY = Input.MousePosition.y;
float screenX = Screen.width;
float screenY = Screen.height;
if (mouseX < 0 || mouseX > screenX || mouseY < 0 || mouseY > screenY)
return;
// your Update body
...which is pretty much the same as your "hacky" solution (which is completely valid imho).
Another option is to create 4 Rect objects for each screen border, then check if mouse is inside those rects. Example:
public float boundary = 50;
public float speed = 4;
private Rect bottomBorder;
private Rect topBorder;
private Transform cameraTransform;
private void Start()
{
cameraTransform = Camera.mainCamera.transform
bottomBorder = new Rect(0, 0, Screen.width, boundary);
topBorder = new Rect(0, Screen.height - boundary, Screen.width, boundary);
}
private void Update()
{
if (topBorder.Contains(Input.mousePosition))
{
position.y += speed * Time.deltaTime;
}
if (bottomBorder.Contains(Input.mousePosition))
{
position.y -= speed * Time.deltaTime;
}
cameraTransform.position = position;
}
The tricky part here is that Rect coordinates have Y axis pointing down and Input.mousePosition has Y pointing up... so bottomBorder Rect has to be on the top, and topBorder has to be at the bottom. Left and right borders are not affected.

Due to the way Unity and the various host operating systems interact, you have limited control of the mouse cursor. (in short the OS controls the mouse Unity just reads it) That being said you do have some options. Screen.lockCursor jumps to mind.
http://docs.unity3d.com/Documentation/ScriptReference/Screen-lockCursor.html
It won't do exactly what you are looking for but it might be a good starting point

Time.speed = 0;
is this what you want?

Related

How to drag unity object just up-down and left-right .?

I have a graph in unity, I want to drag it just up-down and left-right to see upcoming coordinates.
I wrote this code to drag the graph object but it drags it all around without a limit. I just want it to go just up-down and left-right.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragGraph : MonoBehaviour {
float OffsetX;
float OffsetY;
public void BeginDrag(){
OffsetX = transform.position.x - Input.mousePosition.x;
OffsetY = transform.position.y - Input.mousePosition.y;
}
public void OnDrag(){
transform.position = new Vector3 (OffsetX + Input.mousePosition.x, OffsetY + Input.mousePosition.y);
}
}
if OffsetX > OffsetY, don't use Input.mousePosition.y
And vice versa.
Figuring out what to do when OffsetX == OffsetY is left as an exercise to the reader.
If you want it to go up and down then why are you using the x value? You could just leave it 0 it and only the y value will change and make it go up and down.
Edit
Not sure if this is ideal but its similar to a swipe up, down, left and right.I tried it and it goes in one direction at a time.
Vector2 offset;
Vector2 startPos = Vector2.zero;
public void OnBeginDrag(PointerEventData eventData)
{
startPos = eventData.position;
offset.x = transform.position.x - Input.mousePosition.x;
offset.y = transform.position.y - Input.mousePosition.y;
}
public void OnDrag(PointerEventData eventData)
{
Vector2 direction = eventData.position - startPos;
direction.Normalize();
if ((direction.x>0 || direction.x<0) && direction.y>-0.5f && direction.y < 0.5f)
{
transform.position = new Vector3(offset.x+ Input.mousePosition.x, transform.position.y);
}
if ((direction.y > 0 || direction.y<0) && direction.x > -0.5f && direction.x < 0.5f)
{
transform.position = new Vector3( transform.position.x, offset.y + Input.mousePosition.y);
}
}

Unity: Rotating The Camera Using The Law Of Cosine

I am making the basic controls for a game that is mouse-oriented. I want the camera to follow the player and rotate around the player using the law of cosine to find an angle. Here is my code:
//Move Camera
camX = player.transform.position.x;
camY = 15.5f;
camZ = player.transform.position.z;
//Rotate Camera
rotTriA = player.transform.position.x;
rotTriB = player.transform.position.z;
rotTriC = Mathf.Sqrt((rotTriA * rotTriA) + (rotTriB + rotTriB));
if(rotTriA > 0 && rotTriB > 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg);
}
else if(rotTriA > 0 && rotTriB < 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg) + 90;
}
else if(rotTriA < 0 && rotTriB < 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg) + 180;
}
else if(rotTriA < 0 && rotTriB > 0)
{
getAngle = (Mathf.Acos(((rotTriA * rotTriA)
+ (rotTriB * rotTriB)
- (rotTriC * rotTriC))
/ (2 * (rotTriA) * (rotTriB))) * Mathf.Rad2Deg) + 270;
}
else if(rotTriA == 0 && rotTriB > 0)
{
getAngle = 90;
}
else if(rotTriA == 0 && rotTriB < 0)
{
getAngle = 270;
}
else if(rotTriA > 0 && rotTriB == 0)
{
getAngle = 0;
}
else if(rotTriA < 0 && rotTriB == 0)
{
getAngle = 180;
}
else
{
getAngle = 0;
}
Debug.Log(getAngle);
if (camZ < 0)
{
camZ = player.transform.position.z + 10.5f;
transform.eulerAngles = new Vector3(135f, getAngle, 0.0f);
}
else
{
camZ = player.transform.position.z - 10.5f;
transform.eulerAngles = new Vector3(45f, getAngle, 0.0f);
}
camZ = player.transform.position.z - 15.5f;
cam = new Vector3(-camX, camY, camZ);
mainCamera.transform.position = cam;
I know how to make the camera follow the player, but I don't know how to make the camera rotate around the player without it freaking out.
Possible reasons why this is occurring: I am using raycasting to move the player, so when I move my mouse outside of the arena, the movements wont register, I might not be doing the law of cosine right because I get values up to 400 (which shouldn't be possible, given a circle is 360 degrees).
The easiest way to make a camera rotate around the player would be to take advantage of the game object heirarchy.
First make an empty GameObject and then make your camera a child of it:
EmptyObject
> Camera
Now orient your camera to look at the position of the empty object. The location of this empty object is now where your camera is "Looking". Since the camera is a child, if you rotate this object, the camera rotates around it (while maintaining the orientation of facing the empty object).
A simple script to rotate the empty object will, thanks to transform parenting, make the camera "orbit" around it while looking at it:
using UnityEngine;
public class CameraInput : MonoBehaviour
{
[SerializeField] private float rotationAcceleration; // radians/(frame^2)
[SerializeField] private float rotationSpeedMax; // radians/frame
[SerializeField] private float rotationDamping; // 0.99
private float rotationSpeed;
private void Awake()
{
rotationSpeed = rotationSpeedMax / 4;
}
private void Update()
{
if (Input.GetKey(KeyCode.A))
{
rotationSpeed += rotationAcceleration;
}
if (Input.GetKey(KeyCode.D))
{
rotationSpeed -= rotationAcceleration;
}
rotationSpeed = Mathf.Clamp(
rotationSpeed, -rotationSpeedMax, rotationSpeedMax);
transform.Rotate(0f, rotationSpeed, 0f);
rotationSpeed *= rotationDamping;
}
}
Attaching something like this to the EmptyObject object will do the trick. Now you just need this empty object to follow your character. The rotational functions accept radians as arguments so you'll want to use small [0.00 ~ 0.01] numbers. Also, this is a per-frame implementation so it'll only work smoothly while your frame rate is smooth; you'd want to eventually have Time.deltaTime factor into it.

Determine swipe or tap

I'm trying to determine between tapping the screen and swiping the screen.
I want to determine swipes from left, right, up and down.
Right now when I swipe left or right, it also turns my player.
Which isn't suppose to happen. It should be one or the other, either turn or move left or right. My question is how can I determine all five things?
Swipe left, right, up, and down as-well as just tapping the screen.
Here is my code
using UnityEngine;
using System.Collections;
public class PlayerMotor : MonoBehaviour {
private CharacterController controller;
private Vector3 moveVector;
private float speed = 2.0f;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
public Touch touch;
private void Start() {
controller = GetComponent<CharacterController> ();
}
private void Update()
{
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
if (Input.GetMouseButton (0))
{
if (touch.position.x == touch.deltaPosition.x && touch.position.x == touch.deltaPosition.x)
{ //3px accuracy, stationery :P
moveVector.x = transform.forward.x * speed;
transform.Rotate (new Vector3 (0, -90, 0));
}
else if (touch.position.x != touch.deltaPosition.x && touch.position.x != touch.deltaPosition.x)
{
if (Input.mousePosition.x > Screen.width / 2)
moveVector.x = speed;
else
moveVector.x = -speed;
}
}
moveVector.y = verticalVelocity;
moveVector.z = transform.forward.z * speed;
controller.Move (moveVector * Time.deltaTime);
}
}
I found this code with a little bit of searching. (Here)
Adding an else statement after all the if (currentSwipe...) statements should work to detect the click
//inside class
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
public void Swipe()
{
if(Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if(t.phase == TouchPhase.Began)
{
//save began touch 2d point
firstPressPos = new Vector2(t.position.x,t.position.y);
}
if(t.phase == TouchPhase.Ended)
{
//save ended touch 2d point
secondPressPos = new Vector2(t.position.x,t.position.y);
//create vector from the two points
currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
//normalize the 2d vector
currentSwipe.Normalize();
//swipe upwards
if(currentSwipe.y > 0 currentSwipe.x > -0.5f currentSwipe.x < 0.5f)
{
Debug.Log("up swipe");
}
//swipe down
if(currentSwipe.y < 0 currentSwipe.x > -0.5f currentSwipe.x < 0.5f)
{
Debug.Log("down swipe");
}
//swipe left
if(currentSwipe.x < 0 currentSwipe.y > -0.5f currentSwipe.y < 0.5f)
{
Debug.Log("left swipe");
}
//swipe right
if(currentSwipe.x > 0 currentSwipe.y > -0.5f currentSwipe.y < 0.5f)
{
Debug.Log("right swipe");
}
}
}
}

Moving object 2d with viewmatrix

I'm working on a small game based on the movements of the military in ww2. I first started to make a camera to look around and then I wrote a programm to slowly move a platoon towarts where i clicked and then stop if it was there. UNtil there everything works totally fine, but then I tried to get it to work while having moved the camera so the matrix wasn't the standard matrix. I haven't got the mooving of the platoon to the right location to work.
I'd greatly appreciate it if someone could tell me what I have to do to get it working
moving Code:
public void Update(MouseState mouse, GameTime gameTime,Matrix matrix)
{
if (mouse.LeftButton == ButtonState.Pressed)
{
mousePos = new Vector2(mouse.X, mouse.Y);
Vector2.Transform(mousePos, matrix);
oldPos = testObjPos;
Vector2.Transform(oldPos, matrix);
Difference = mousePos - oldPos;
Difference.Normalize();
}
testObjPos += Difference * (float)gameTime.ElapsedGameTime.TotalSeconds * 20;
if (testObjPos.X > mousePos.X - 1 && testObjPos.X < mousePos.X + 1 &&
testObjPos.Y > mousePos.Y - 1 && testObjPos.Y < mousePos.Y + 1)
Difference = new Vector2(0, 0);
}
and maybe my camera class is the problem
Camera COde
float moveSpeed = 300;
Vector2 position;
Matrix viewMatrix;
int screenwidth, screenheight;
public void Update(KeyboardState keyState, GameTime gameTime)
{
if (keyState.IsKeyDown(Keys.W))
position.Y -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (keyState.IsKeyDown(Keys.S))
position.Y += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (keyState.IsKeyDown(Keys.A))
position.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (keyState.IsKeyDown(Keys.D))
position.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (position.X < -1000)
position.X = -1000;
if (position.Y < -1000)
position.Y = -1000;
if (position.X > 1000)
position.X = 1000;
if (position.Y > 1000)
position.Y = 1000;
viewMatrix = Matrix.CreateTranslation(new Vector3(-position, 0));
}
Thanks for taking the time to look at it already.
I assume that matrix is your view matrix. This matrix is used to transform positions from world space to camera space.
Your mouse position is in view space and you want to transform it to world space, hence in the other way. Therefore, you need the inverse of matrix. Furthermore, the Vector2.Transform() method returns the transformed vector. It does not change the original one. So right now, you don't use any transformed positions:
var invMatrix = Matrix.Invert(matrix);
mousePos = Vector2.Transform(mousePos, invMatrix);
You probably do not want to transform oldPos because it already is in world space (probably, I can't tell definitely from the code snippet).
By the way, there is an easier way to check if testObjPos is near mousePos. Just check the length of the difference vector:
if((testObjPos - mousePos).Length() < 1)
//stop

Monogame move in direction

I'm creating a 2D game based on asteroids. And In that game I need to thrust the ship in a direction.
I'm able draw the ship, so that it turns around. But when it comes to moving it forward my problem occours.
I don't seem to be able to get my head around this. (This whole making games thing is new to me ^^)
The player.cs
protected Vector2 sVelocity;
protected Vector2 sPosition = Vector2.Zero;
protected float sRotation;
private int speed;
public Player(Vector2 sPosition)
: base(sPosition)
{
speed = 100;
}
public override void Update(GameTime gameTime)
{
attackCooldown += (float)gameTime.ElapsedGameTime.TotalSeconds;
// Reset the velocity to zero after each update to prevent unwanted behavior
sVelocity = Vector2.Zero;
// Handle user input
HandleInput(Keyboard.GetState(), gameTime);
if (sPosition.X <= 0)
{
sPosition.X = 10;
}
if (sPosition.X >= Screen.Instance.Width)
{
sPosition.X = 10;
}
if(sPosition.Y <= 0)
{
sPosition.Y = 10;
}
if (sPosition.Y >= Screen.Instance.Height)
{
sPosition.Y = 10;
}
// Applies our speed to velocity
sVelocity *= speed;
// Seconds passed since iteration of update
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
// Multiplies our movement framerate independent by multiplying with deltaTime
sPosition += (sVelocity * deltaTime);
base.Update(gameTime);
}
private void HandleInput(KeyboardState KeyState, GameTime gameTime)
{
if (KeyState.IsKeyDown(Keys.W))
{
//Speed up
speed += 10;
sVelocity.X = sRotation; // I know this is wrong
sVelocity.Y = sRotation; // I know this is wrong
}
else
{
//Speed down
speed += speed / 2;
}
if (KeyState.IsKeyDown(Keys.A))
{
//Turn left
sRotation -= 0.2F;
if (sRotation < 0)
{
sRotation = sRotation + 360;
}
}
if (KeyState.IsKeyDown(Keys.D))
{
//Turn right
sRotation += 0.2F;
if (sRotation > 360)
{
sRotation = sRotation - 360;
}
}
}
Am I close, or seriously far from right?
sRotation is an angle, sVelocity is a velocity. You need trigonometry.
for instance, you could use something like that (I didn't test the signs for correctness):
if (KeyState.IsKeyDown(Keys.W))
{
//Speed up
speed += 10;
sVelocity.X = Math.cos(sRotation * 2 * Math.PI / 360);
sVelocity.Y = -Math.sin(sRotation * 2 * Math.PI / 360);
}
Would that solve your problem?
EDIT: your "speed down" formula is wrong. You are currently adding speed/2 with speed, you should have something along:
speed = speed / 2; // note the "=", not "+="
Also, it would probably be preferable to use something like:
if (speed > 0) {
speed -= 5;
} else {
speed = 0;
}

Categories

Resources