Input.GetAxis Key release not working on WebGL - c#

I am doing camera navigation (Movement/Rotation) on Update event in this way:
void UpdateMovement()
{
bool accelerate = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
moveDirection =
new
Vector3(
Input.GetAxisRaw("Horizontal") * moveSpeed,
0,
Input.GetAxisRaw("Vertical") * moveSpeed);
//moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0, Input.GetAxis("Vertical") * moveSpeed);
moveDirection = transform.TransformDirection(moveDirection);
if (Input.GetButton("Up"))
{
moveDirection.y += moveSpeed;
}
else if (Input.GetButton("Down"))
{
moveDirection.y -= moveSpeed;
}
if (Input.GetAxisRaw("Mouse ScrollWheel") > 0)
{
moveDirection.y = moveDirection.y + scrollSpeed;
}
else if (Input.GetAxisRaw("Mouse ScrollWheel") < 0)
{
moveDirection.y = moveDirection.y - scrollSpeed;
}
moveDirection *= (accelerate ? speed : moveSpeed);
controller.Move(moveDirection * Time.deltaTime);
}
void UpdateRotation()
{
if (!Input.GetMouseButton(1))
return;
rotationX += Input.GetAxis("Mouse X") * lookSpeed;
rotationY += Input.GetAxis("Mouse Y") * lookSpeed;
rotationY = Mathf.Clamp(rotationY, -90, 90);
rotationZ = Input.GetAxis("Mouse ScrollWheel");
transform.localRotation = Quaternion.AngleAxis(rotationZ, Vector3.forward);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
}
All working fine but the problem on WebGL canvas when I rotate the camera using mouse and comes out of the bound of WebGl canvas meanwhile, I also continuously press horizontal or vertical key, then release input key doesn't work. Remember I logged the key[Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical")] input and found that it is not reset to zero on release.
Debug.Log("Hr GetAxisRaw : " + Input.GetAxisRaw("Horizontal"));
Debug.Log("Vertical : " + Input.GetAxisRaw("Vertical"));
Normally when I don't use camera rotation using mouse and during this time, when i release the horizontal/vertical key, it works fine. I was previously using Input.GetAxis now i am using Input.GetAxisRaw but the problem is same.

Related

How do I make my third person shooter player look around?

I've genuinely gone through a million tutorials, but it doesn't work out. With this code, I either have to activate the last line or the second last line to get x rotation or y rotation respectively, but I obviously want them to work together.
void MouseLook()
{
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -60, 10);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
The one always seems to cancel the other out. This is from a Brackeys tutorial. I had to tweak some stuff so it's applicable, but I obviously am breaking the code that way. Please help!
Try this Stuff
public float mouseSensitivity = 10.0f;
public Transform target;
public float dstFromTarget = 2.0f;
public float yaw;
public float pitch;
public Vector2 pitchMinMax = new Vector2(-50, 85);
public float rotationSmoothTime = 0.02f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
void LateUpdate()
{
MouseLook();
}
void MouseLook()
{
//Mouse Movement
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
// Claemp
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
// Positioning
transform.eulerAngles = currentRotation;
// Smoothening
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.localRotation = Quaternion.Euler(currentRotation.x,currentRotation.y,currentRotation.z);
}

Camera resets keyboard rotation before applying mouse rotation

I'm having an issue with the free roam camera I'm trying to implement. The camera can be rotated via keyboard (only on the Y axis) and via mouse (on the X and Y axis) while holding the middle mouse button. With my current implementation, if I rotate the camera using the keyboard and the rotate it using the mouse, it remove any rotation done by the keyboard as soon as I hit the middle mouse button. Of course I would like to not do that... Code is below. Can someone give tell me what I'm doing wrong?
private void RotateCameraKeyboard()
{
if (Input.GetKey(KeyCode.Q))
{
transform.RotateAround(transform.position, Vector3.up, -rotationSpeed * Time.deltaTime * 30);
}
if (Input.GetKey(KeyCode.E))
{
transform.RotateAround(transform.position, Vector3.up, rotationSpeed * Time.deltaTime * 30);
}
}
private void RotateCameraMouse()
{
if (Input.GetMouseButton(2))
{
pitch -= rotationSpeed * Input.GetAxis("Mouse Y");
yaw += rotationSpeed * Input.GetAxis("Mouse X");
pitch = Mathf.Clamp(pitch, -90f, 90f);
while (yaw < 0f)
{
yaw += 360f;
}
while (yaw >= 360f)
{
yaw -= 360f;
}
transform.eulerAngles = new Vector3(pitch, yaw, 0f);
}
}
Instead of using RotateAround (which in your case of using the transform.position as pivot is redundant anyway .. you could as well just use Rotate) additionally also add the according amount to your pitch and yawn.
I would simply generalize and use the same method for both inputs like e.g.
private void RotateCameraKeyboard()
{
if (Input.GetKey(KeyCode.Q))
{
Rotate(-rotationSpeed * Time.deltaTime * 30, 0);
}
if (Input.GetKey(KeyCode.E))
{
Rotate(rotationSpeed * Time.deltaTime * 30, 0);
}
}
private void RotateCameraMouse()
{
if (Input.GetMouseButton(2))
{
var pitchChange = rotationSpeed * Input.GetAxis("Mouse Y");
var yawChange = rotationSpeed * Input.GetAxis("Mouse X");
Rotate(yawChange, pitchChange);
}
}
private void Rotate(float yawChange, float pitchChange)
{
pitch = Mathf.Clamp(pitch + pitchChange, -90f, 90f);
yaw += yawChange;
while (yaw < 0f)
{
yaw += 360f;
}
while (yaw >= 360f)
{
yaw -= 360f;
}
transform.eulerAngles = new Vector3(pitch, yaw, 0f);
}

Shaking camera when looking at certain angle in unity 3d

I have a strange problem with my camera controller and I can't understand why. When I look in certain directions, the camera starts to shake. The rotation of the camera seems to change very quickly at this moment, but I don't understand why.
Here is the code I use (this is called in update).
void PhotoMode()
{
Vector3 newPos = transform.position;
//déplacement avant
if(Input.GetKey(InputManager.IM.forward))
{
newPos += cam.transform.forward * moveSpeedPhoto * Time.unscaledDeltaTime;
}
//déplacement arrière
if(Input.GetKey(InputManager.IM.backward))
{
newPos -= cam.transform.forward * moveSpeedPhoto * Time.unscaledDeltaTime;
}
//déplacement gauche
if(Input.GetKey(InputManager.IM.left))
{
newPos += -transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
}
//déplacement droite
if(Input.GetKey(InputManager.IM.right))
{
newPos += transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
}
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
x *= photoRotSpeed * Time.unscaledDeltaTime;
y *= photoRotSpeed * Time.unscaledDeltaTime;
currentX = Mathf.Lerp(currentX, x, smoothTime * Time.unscaledDeltaTime);
currentY = Mathf.Lerp(currentY, y, smoothTime * Time.unscaledDeltaTime);
float rotationX = transform.localEulerAngles.y + currentX * photoRotSpeed;
rotationY += currentY * photoRotSpeed;
rotationY = Mathf.Clamp(rotationY, -90+65, 90+65);
transform.localRotation = Quaternion.Euler(new Vector3(-rotationY, rotationX, 0));
transform.position = newPos;
}
I managed to make it work with this : (the cam in inside a game object, which has the script)
void PhotoMode()
{
if(Input.GetKeyDown(KeyCode.Space))
{
dontMove = !dontMove;
screenshotManager.photoModeCanvas.SetActive(dontMove);
if(!dontMove && screenshotManager.focusDistanceIndicatorToggle.isOn && screenshotManager.distanceIndicator.activeSelf)
{
screenshotManager.FocusDistanceIndicator(false);
}
if(dontMove && screenshotManager.focusDistanceIndicatorToggle.isOn && !screenshotManager.distanceIndicator.activeSelf)
{
screenshotManager.FocusDistanceIndicator(true);
}
}
if(dontMove)
{
Cursor.lockState = CursorLockMode.None;
return;
}
Cursor.lockState = CursorLockMode.Locked;
Vector3 newPos = transform.position;
float scroll = Input.GetAxis("Mouse ScrollWheel");
moveSpeedPhoto += scroll*10;
moveSpeedPhoto = Mathf.Clamp(moveSpeedPhoto, 0, 100);
//déplacement avant
if(Input.GetKey(InputManager.IM.forward))
{
newPos += cam.transform.forward * moveSpeedPhoto * Time.unscaledDeltaTime;
}
//déplacement arrière
if(Input.GetKey(InputManager.IM.backward))
{
newPos -= cam.transform.forward * moveSpeedPhoto * Time.unscaledDeltaTime;
}
//déplacement gauche
if(Input.GetKey(InputManager.IM.left))
{
newPos += -transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
}
//déplacement droite
if(Input.GetKey(InputManager.IM.right))
{
newPos += transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
}
float x = Input.GetAxis("Mouse X") * photoRotSpeed * Time.unscaledDeltaTime;
float y = Input.GetAxis("Mouse Y") * photoRotSpeed * Time.unscaledDeltaTime;
rotationX -= y;
rotationX = Mathf.Clamp(rotationX, -90, 90);
cam.transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
this.transform.Rotate(Vector3.up * x);
transform.position = newPos;
}

unity rotate object on x-axis when moving mouse up / down

I want to rotate a object on the x-axis when I move the mouse up or down (increase the x-rotation when moving mouse up, decrease when moving mouse down).
But I don't know how to do this.
I tried this script:
public float mouseSensitivity = 100.0F;
public float clampAngle = 80.0F;
float rotX = 0.0F, rotY = 0.0F;
void Update()
{
// Mouse Look
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, 0, 0.0F);
transform.rotation = localRotation;
}
But this controls also rotation on the y axis. I also have rotation on the y-axis, but they are controlled via keyboard input:
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * Time.deltaTime * -moveSpeed);
if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed, Space.World);
else if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed, Space.World);
and I want the mouse up/down only control the rotation on the x-axis.
If someone can help me, that would be great!
void Update()
{
// Mouse Look
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
rotX = mouseY * mouseSensitivity;
rotY = mouseX * mouseSensitivity;
transform.rotation *= Quaternion.Euler(rotX, 0, 0.0f);
}

Unity 5.4 - Zooming RTS camera without FoV manipulation

I'm trying to create a RTS-esque camera control system for my game and I've hit a wall and am unsure how to proceed.
What I want to happen is the following:
Mouse Click + Drag = Move the camera around the world (works)
Mouse Scroll Wheel Click + Drag = Rotate camera around the world (works)
Mouse Scroll Wheel = Zoom in/out using MoveToward/Transform.Forward..etc (sorta works)
In another script I have FoV zoom working, but it is not ideal for the type of experience I want to create. Instead I am trying to physically move the camera in the direction it is pointed.
This current script is 90% functional in that it can do all the things listed above, but when I click + drag to move to a new location after scrolling to zoom, the camera resets to the original position.
Here is what happens in Update():
void Update ()
{
currentPosition = transform.position;
scrollAxis = Input.GetAxis ("Mouse ScrollWheel");
if (Input.GetMouseButtonDown (0))
{
mouseOrigin = Input.mousePosition;
isPanning = true;
}
// cancel on button release
if (!Input.GetMouseButton (0))
{
isPanning = false;
}
//move camera on X & Y
else if (isPanning)
{
Vector3 pos = Camera.main.ScreenToViewportPoint (Input.mousePosition - mouseOrigin);
Vector3 move = new Vector3 (pos.x * -panSpeed, pos.y * -panSpeed, 0);
Camera.main.transform.Translate (move, Space.Self);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0 );
transform.localPosition = new Vector3( transform.position.x, transform.position.y - (transform.position.y - (initialPosition.y)), transform.position.z );
}
//rotate camera with mousewheel click
if (Input.GetMouseButton(2)) {
yRotation -= Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
yRotation = Mathf.Clamp(yRotation, -80, 80);
xRotation += Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
xRotation = xRotation % 360;
transform.localEulerAngles = new Vector3(yRotation + initialRotation.x, xRotation + initialRotation.y, 0);
}
//zoom with scroll
if ( Input.GetAxis("Mouse ScrollWheel") > 0 && !isPanning) {
transform.Translate(Vector3.forward * Time.deltaTime * 10000f * scrollAxis, Space.Self );
transform.localPosition = new Vector3( transform.position.x, transform.position.y, transform.position.z );
}
}
Any help is much appreciated, thanks!
Solved it with the help of a friend.
Needed to reset the initialPosition Vector3 each time I zoomed, to prevent from snapping back to the original location.
if ( Input.GetAxis("Mouse ScrollWheel") > 0) {
transform.Translate(Vector3.forward * Time.deltaTime * 10000f * scrollAxis, Space.Self );
initialPosition = transform.position;
}

Categories

Resources