How to detect if Mouse stopped moving in Unity - c#

I want to do a Game like slither.io in Unity but it's hard to detect when the mouse stopped moving, I want the ball to not stop, instead it should move on, worked several hours on this problem but can't quite get it to work. I thought if I save where the last known position is I can keep the velocity but I don't know how to implement yet.
Thanks in advance!
This is my Source Code:
private float xMin, xMax, yMin, yMax;
[SerializeField] float constantSpeed = 100f;
[SerializeField] float padding = 5f;
private Rigidbody2D rb2D;
private Vector3 mousePosition;
private Vector2 direction;
private List<Vector3> ListPos = new List<Vector3>();
private Vector3 empty;
// Use this for initialization
void Start () {
empty = Vector3.zero;
SetUpMoveBoundaries();
rb2D = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
Move();
}
private void SetUpMoveBoundaries()
{
Camera gameCamera = Camera.main;
xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
}
private void Move()
{
if (Input.mousePosition != empty)
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = (mousePosition - transform.position).normalized;
ListPos.Add(direction);
rb2D.velocity = new Vector2(direction.x * constantSpeed, direction.y * constantSpeed);
}
else
{
var last = ListPos.LastOrDefault();
rb2D.velocity = new Vector2(last.x * constantSpeed, last.y * constantSpeed);
}
}

Assuming you want it to trigger one of the two functions depending on whether the mouse has moved or not since last frame:
Vector2 lastMousePosition;
void WhenMouseIsMoving()
{
}
void WhenMouseIsntMoving()
{
}
void Update()
{
if (Input.mousePosition!=lastMousePosition)
{
lastMousePosition=Input.MousePosition;
WhenMouseIsMoving();
} else
WhenMouseIsntMoving();
}
You'll need to add one more bool variable to keep track of whether it has just started or stopped moving;

If you want to check for mouse movement without having to keep track of the mouse position in a variable, you can use the GetAxis function from Unity's Input class.
To do this you will have to make sure that mouse movement is hooked up to axes in Unity's input manager. It is normally there by default already, with 'Mouse X' and 'Mouse Y' mapped to the mouse delta for their respective axes.
For example:
// Is true when the mouse has moved
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
// Do something with mouse input
}
In your case, it looks like you could do something like this as your move function:
Vector2 direction = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
if (direction.magnitude != 0)
{
// ListPos.Add(direction);
rb2D.velocity = direction.normalized * constantSpeed;
}
As an unrelated note, your ListPos variable is growing in size potentially every frame without limit. If you need to keep track of previous positions for reasons other than your attempt at detecting mouse position changes, you should consider how much storage you will need and give it a fixed size replacing the oldest entries, or consider whether or not the values need to be independent or can be merged instead.

Related

Swerve Control Unity C#

Hi I am creating a hyper casual game with unity, but I have encountered a problem with the swerve control (I have also seen many git hubs but even these do not work perfectly)
I've put this in my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float lastframeposx;
private float movefactorx;
public float MoveFactorX => movefactorx;
public Camera m_MainCam;
private float speed = 2.0f;
[SerializeField]
GameObject character;
[SerializeField] private float swerveSpeed = 0.5f;
[SerializeField] private float maxSwerveAmount = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position += Vector3.forward * speed * Time.deltaTime;
Cammina();
/*Vector3 destra = Camera.main.ScreenToWorldPointt(Input.touches[i].position);
transform.position += Vector3.zero destra;*/
}
void Cammina()
{
if(Input.GetMouseButtonDown(0))
{
lastframeposx = Input.mousePosition.x;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
else if (Input.GetMouseButton(0))
{
movefactorx = Input.mousePosition.x - lastframeposx;
lastframeposx = Input.mousePosition.x;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
else if(Input.GetMouseButtonUp(0))
{
movefactorx = 0f;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
}
/*void vaidoveschiacciato()
{
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.collider != null)
{
transform.position += hit.collider.GetComponent<Transform.position> * speed * Time.deltaTime;
}
}
}
}*/
}
1 Problem: he don't go when the finger is
2 Problem: How do I eliminate the movement from right to left (Without making it go out of the path)
(Langauge: C#)
The problem: When you swerve, it swerves just in the direction, there is no limits on how far it goes.
How I would fix this: I would put the movement to change it through a function. This could clamp it, so the higher the distance to the center of the track, the less it swerves. Or, you can altogether check if the distance is a maximum and then stop swerving.
Note: you can use other functions to do this (they just have to flatten out the larger the input).
Smooth, good looking bell curve way
For example you could use a bell curve. Look one up if you've never seen one before. It is at it's highest possible output of one, at a zero input. When it gets hiher or lower, it outputs lower to zero.
the simplest equation is y = i-(x2). The lower i is (above 1), the wider the curve, or the larger the output is for a larger input. I made a graph here.
x can be the distance to the center of the track, so you should adjust i, so the maximum distance from the track is flat.
This output is what you should change swerveAmount to.
The flatter parts of the graoh is how much you will swerve when you are that distance from the center (x axis)
Alternatively
You could just check the distance, and if it passes a certain distance don't translate it.
Let me know in the omments if there are any problems! :)

Jitter when moving around a circular path

This is a pretty strange bug. I created the following script intended to calculate positions to shoot projectiles in my game by moving around a circular path and facing the mouse:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileFire : MonoBehaviour
{
[System.Serializable]
public class ButtonDirectionSet {
public string button;
public float direction;
public bool degrees = true;
}
public GameObject projectile;
public Vector2 centerPoint;
public float radius = 1.0f;
public string fireButton = "Fire1";
public bool followMouse = false;
public bool followKeyboard = true;
public List<ButtonDirectionSet> buttonDirectionMap = new List<ButtonDirectionSet>();
public bool degrees = true;
public float direction;
private readonly float maxDirection = 360 * Mathf.Deg2Rad;
// Start is called before the first frame update
void Start()
{
foreach (ButtonDirectionSet set in buttonDirectionMap) {
if (set.degrees) {
set.direction *= Mathf.Deg2Rad;
}
}
if (degrees) {
direction *= Mathf.Deg2Rad;
}
}
void Update()
{
foreach (ButtonDirectionSet set in buttonDirectionMap) {
if (Input.GetButtonDown(set.button)) {
direction = set.direction;
}
}
if ((followKeyboard && followMouse && Input.GetMouseButton(0)) || !followKeyboard) {
Vector3 mouse = Input.mousePosition;
mouse.z = transform.position.z - Camera.main.transform.position.z;
Vector3 mousePosition = transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(mouse));
float xPos = mousePosition.x - centerPoint.x;
if (Mathf.Abs(xPos) < 0.1f)
{
xPos = 0.1f * Mathf.Sign(xPos);
}
float yPos = mousePosition.y - centerPoint.y;
if (Mathf.Abs(yPos) < 0.1f)
{
yPos = 0.1f * Mathf.Sign(yPos);
}
direction = Mathf.Atan2(yPos, xPos);
}
if (direction < 0) direction += maxDirection;
transform.localPosition = centerPoint + (new Vector2(Mathf.Cos(direction), Mathf.Sin(direction)) * radius);
}
}
If you set both "follow keyboard" and "follow mouse" to true, the object should be positioned toward the mouse when you hold down the left click button. For the most part, this works, but for some reason, there's jitter in the sense that the object rapidly changes between two different positions for a period of time, even when I don't move the mouse at all. Additionally, the "direction" value switches itself at the same rate, even if I don't move my mouse at all. This does not happen all the time, but it does happen pretty frequently at times that appear to be random.
Is there anything I can do to mitigate or eliminate this behavior?
Since the direction also jitters, there is something wrong with the way you calculate the direction.
The reason your direction jitters is because of the transform.InverseTransformPoint. Since the position of the object changed, the position of the mouse relative to the object also changed with every movement so the direction also changed.
Here are two ways to do it:
1:
Vector2 direction;
if ((followKeyboard && followMouse && Input.GetMouseButton(0)) || !followKeyboard)
{
Vector3 mouseOnScreen = Camera.main.ScreenToWorldPoint(Input.mousePosition) - new Vector3(centerPoint.x, centerPoint.y, 0);
direction = new Vector2(mouseOnScreen.x, mouseOnScreen.y);
}
transform.localPosition = centerPoint + (direction.normalized * radius);
This is a simple way to do it without using angles. It gets the position of the mouse on screen in world space. Then it makes it into a vector 2 so the z value doesn't affect it. Then, it sets the new position to the center plus the direction to the mouse with a magnitude of the radius
2:
if ((followKeyboard && followMouse && Input.GetMouseButton(0)) || !followKeyboard)
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - new Vector3(centerPoint.x, centerPoint.y, 0);
direction = Mathf.Atan2(mousePosition.y, mousePosition.x);
}
transform.localPosition = centerPoint + (new Vector2(Mathf.Cos(direction), Mathf.Sin(direction)) * radius);
This is the same way you did it but a little cleaned up. Instead of using transform.InverseTransformPoint, this way subtracts the centerPoint. That returns the vector from the centerPoint to the mouse, setting the centerPoint as the origin. This is also in the firsts solution.

When I make an object face a mouse cursor in Unity it eventually offsets

I made a script that makes the player point towards the mouse cursor, but recently I discovered a bug. When I move the mouse cursor too much (An example being when I spin the mouse around the object in circles, causing the object to move around.), the object ends up pointing a bit off of where the mouse should be. As in, the cursor would signal the object to look at it, and the object ends up looking the slightest bit off, making it feel quite odd after maneuvering quickly. How can I make it so the object always faces the cursor, with no offsets, even when I move the cursor as much as possible.
private void LateUpdate()
{
Vector3 lookAtPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 mousePoint = new Vector3(lookAtPoint.x, lookAtPoint.y, 0);
float angle = getAngle(transform.position, mousePoint);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 9f * Time.deltaTime);
float getAngle(Vector3 currentLocation, Vector3 mouseLocation)
{
float x = mouseLocation.x - currentLocation.x;
float y = mouseLocation.y - currentLocation.y;
return angle = Mathf.Rad2Deg * Mathf.Atan2(y, x);
}
}
Looks like it's down to the way that you are using Quaternion.Lerp(). In particular, the last parameter - it is meant to be a value between 0f and 1f which you supply, it does not auto-increment for you.
So to fix this issue, what you want to do is save off a float somewhere. When you move the mouse (mouse position has changed since last frame) then start that value at 0f again. Then increment it at some value every frame until it is equal to or greater than 1f.
Doing this will not only fix your bug. It will, depending on how fast your increment, give you a smooth rotation effect. Below is an example.
internal class SomeClass : MonoBehaviour
{
private float lerpAmount = 0f;
private Vector3 cachedMousePosition = Vector3.zero;
private void LateUpdate()
{
var mousePosition
= Camera.main.ScreenToWorldPoint(Input.mousePosition)
* new Vector3(1, 1, 0);
bool recalculateRotation = false;
if (this.cachedMousePosition != mousePosition)
{
this.lerpAmount = 0f;
recalculateRotation = true;
}
if (this.lerpAmount < 1f)
{
this.lerpAmount = Mathf.Min(this.lerpAmount + Time.deltaTime, 1f);
recalculateRotation = true;
}
if (recalculateRotation)
{
float angle = getAngle(transform.position, mousePoint);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), this.lerpAmount);
}
float getAngle(Vector3 currentLocation, Vector3 mouseLocation)
{
float x = mouseLocation.x - currentLocation.x;
float y = mouseLocation.y - currentLocation.y;
return angle = Mathf.Rad2Deg * Mathf.Atan2(y, x);
}
}

When fliping the character the rotation of character's weapon goes backward

I have two game objects on the Scene, 1.Character(Parent Object) and 2.Weapon(Child Object) . The problem is when the character is moving to the right side, the rotation of the weapon is fine, it is toward where character is facing and rotating as expected, as you can see in the Gif image attach below. But when i Flip to left side everything goes wrong, the weapon goes backward and when i press down arrow the rotation goes up and when press up arrow the rotation goes down, see the Gif image attach below.. Please help How to Fix it.
Here is my Code:
public float weaponRotationSpeed = 13f;
private Animator anim;
private float angle;
void Awake()
{
anim = GetComponent<Animator>();
}
void Update()
{
Vector2 hv = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
Vector3 changeParentScale = transform.localScale;
if (hv != Vector2.zero)
{
if (Input.GetAxis("Horizontal") < 0)
{
changeParentScale.x = -5f;
transform.localScale = changeParentScale;
}
else if (Input.GetAxis("Horizontal") > 0)
{
changeParentScale.x = 5f;
transform.localScale = changeParentScale;
}
angle = Mathf.Atan2(hv.y, hv.x) * Mathf.Rad2Deg;
transform.Find("Weapon").rotation = Quaternion.Lerp(transform.Find("Weapon").rotation,
Quaternion.Euler(0, 0, angle),
weaponRotationSpeed * Time.deltaTime);
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
well you probably would want to flip the rotation then as well? E.g. using Mathf.Sign
Quaternion.Euler(0,0, angle * Mathf.Sign(changeParentScale.x))
There are some other little flaws in your code!
You shouldn't use Find each frame .. rather store it once.
You are using GetAxis repeatedly and should also store the vaues the first time .. don't you already have them in hv?
Lerp is quite cool tool but you are using it wrong ;) It interpolates each frame using the given factor .. usually you want a fixed one like e.g. 0.5f and not using Time.deltaTime
It should probably rather be something like
// You will have to adjust this value again
// This needs to be a constant value between 0 and 1
// - 0: rotation isn't updated at all
// - 1: rotation immediately jumps to target
// - e.g. 0.5f: rotation is every frame set to the middle between current and target
[Range(0f, 1f)]
public float weaponRotationSpeed = 0.5f;
// already reference these via the Inspector
[SerializeField] private Animator anim;
[SerializeField] private Transform weapon;
private float angle;
// As Fallback get them ONCE on runtime
void Awake()
{
if(!anim) anim = GetComponent<Animator>();
if(!weapon) weapon = transform.Find("Weapon");
}
void Update()
{
Vector2 hv = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
Vector3 changeParentScale = transform.localScale;
if (hv != Vector2.zero)
{
// get direction of Horizontal
int sign = Mathf.Sign(hv.x);
if (!Mathf.Approximately(hv.x, 0))
{
changeParentScale.x = 5f * sign;
transform.localScale = changeParentScale;
}
angle = Mathf.Atan2(hv.y, hv.x) * Mathf.Rad2Deg;
weapon.rotation = Quaternion.Lerp(weapon.rotation, Quaternion.Euler(0, 0, angle * sign), weaponRotationSpeed);
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
}
Alternatively you could probably also already solve it by not using rotation but localRotation for rotating the weapon.

Unity 2D - zoom in =ok, zoom out = borked

I hope someone can help. Im trying to create a little script that zooms in to my player and back out - toggling.
The zoom in works fine, but when I try to zoom back out it doesn't work, it gets stuck. I've created a bool to ensure it only runs the code when it needs to and I'm wondering if that is what's causing the error.
using UnityEngine;
using System.Collections;
public class CameraZoom : MonoBehaviour
{
public float zoom = 10f;
public float normal = 3.471398f;
public float smooth = 5f;
private bool isZoomed = false;
public Camera cam;
public GameObject player;
// lock the camera settings
public float LockedX = 0f;
public float LockedY = 0f;
public float LockedZ = 0f;
private bool hasBeenZoomed = false;
Vector3 targetPos;
private Transform playerTransform;
// Use this for initialization
void Start()
{
targetPos = transform.position;
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("z")) { isZoomed = !isZoomed; }
if (isZoomed == true)
{
ZoomInToPlayer();
hasBeenZoomed = true;
}
else
{
if (hasBeenZoomed)
{
ZoomOutFromPlayer();
hasBeenZoomed = false;
}
}
}
void ZoomInToPlayer()
{
// By default the target x and y coordinates of the camera are it's current x and y coordinates.
float targetX = transform.position.x;
float targetY = transform.position.y;
// ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
targetX = Mathf.Lerp(transform.position.x, playerTransform.position.x, smooth * Time.deltaTime);
//Debug.Log("player x is " + playerTransform.position.x + " and TargetX is " + targetX);
// ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
targetY = Mathf.Lerp(transform.position.y, playerTransform.position.y, smooth * Time.deltaTime);
//Debug.Log("player y is " + playerTransform.position.y+ " and TargetY is " + targetY);
// Set the camera's position to the target position with the same z component.
cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
// Change the size of the camera viewport
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, zoom, Time.deltaTime * smooth);
}
void ZoomOutFromPlayer()
{
// By default the target x and y coordinates of the camera are it's current x and y coordinates.
float targetX;
float targetY;
// Change the size of the camera viewport
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, normal, Time.deltaTime * smooth);
// ... the target x coordinate should be a Lerp between the camera's current x position and the original x position.
targetX = Mathf.Lerp(transform.position.x, LockedX, smooth * Time.deltaTime);
// ... the target y coordinate should be a Lerp between the camera's current y position and the original y position.
targetY = Mathf.Lerp(transform.position.y, LockedY, smooth * Time.deltaTime);
// Set the camera's position to the target position with the same z component.
cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
}
}
Your methods ZoomInToPlayer and ZoomOutFromPlayer are written in a way that suggests that they should be called once per frame for the duration of the zoom in/out animation. However, ZoomOutFromPlayer will only be called once, because Update, when ZoomOutFromPlayer is called, the hasBeenZoomed is immediately set to false.
What you're trying to do here, essentially, is a simple Finite State Machine. I suggest researching this design pattern a little more — it will help you noticing the sources of such problems and structuring your code in a better way.
In this particular case, a good way to prevent this problem when designing your code would be to write something akin to "API documentation" for yourself, when writing your methods. For ZoomOutFromPlayer, it would read something like this:
Call every frame when you want to perform zoom-out animation, until the animation is complete.
After you written (and read) such a description, you should immediately notice a red flag — "until the animation is complete"? So, the code that calls this method should somehow take track of whether the animation is complete or not, during a separate mechanism? Wouldn't it make it really easy to use this method incorrectly? Well, that's exactly what happened here.
Instead, what you could've done, is to create two different methods, ZoomInUpdate and ZoomOutUpdate, with descriptions that would read something like this:
Call every frame when the camera should be zoomed out/zoomed in.
This way, using this methods is a lot easier, and you can safely throw out additional logic with hasBeenZoomed out. Just call these methods every frame, and ensure (inside these methods) that they change the camera settings with a certain speed, if these settings need to be changed, or otherwise do nothing.
Try this
using UnityEngine;
using System.Collections;
public class CameraZoom : MonoBehaviour
{
public float zoom = 10f;
public float normal = 3.471398f;
public float smooth = 5f;
private bool isZoomed = false;
private bool isZoomFinished = true; // the animation zoom is over ?
public Camera cam;
public GameObject player;
public float LockedX = 0f;
public float LockedY = 0f;
public float LockedZ = 0f;
private bool hasBeenZoomed = false;
Vector3 targetPos;
private Transform playerTransform;
void Start()
{
targetPos = transform.position;
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
if (Input.GetKeyDown("z") && isZoomFinished) {
isZoomed = !isZoomed;
isZoomFinished = false;
}
if (isZoomed && !isZoomFinished)
{
ZoomInToPlayer();
}
else if (!isZoomed && !isZoomFinished)
{
ZoomOutFromPlayer()
}
}
float delta = 0;
void ZoomInToPlayer()
{
delta += smooth * Time.deltaTime;
//Cam size
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, zoom, delta);
//Cam pos
float targetX = transform.position.x;
float targetY = transform.position.y;
targetX = Mathf.Lerp(transform.position.x, playerTransform.position.x, delta);
targetY = Mathf.Lerp(transform.position.y, playerTransform.position.y, delta);
cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
// is animation over ?
if(delta >= 1) {
isZoomFinished = true;
delta = 0;
}
}
void ZoomOutFromPlayer()
{
delta += smooth * Time.deltaTime;
//Cam size
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, normal, delta);
//Cam pos
float targetX;
float targetY;
targetX = Mathf.Lerp(transform.position.x, LockedX, delta);
targetY = Mathf.Lerp(transform.position.y, LockedY, delta);
cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
// is animation over ?
if(delta >= 1) {
isZoomFinished = true;
delta = 0;
}
}
}

Categories