Spin The Wheel With Touch Inputs in Unity - unity3d

I am a beginner in unity So, I wanted to Make A Fortune Wheel But Instead of Rotating it with a button I want Like, the speed of its spinning will depend on how fast I touch n' dragged the wheel itself.
If I flick/rotate it very fast, the wheel would go spinning faster.
If I only moved it slowly, then it would barely spin at all. Just like what would happen if you spin a wheel like in Wheel of Fortune in real life.
Thanks

Touch control object rotation and zoom in Unity.
using UnityEngine;
using System.Collections;
using System.IO;
public class ScaleAndRotate : MonoBehaviour
{
private Touch oldTouch1; //Last touch point 1 (finger 1)
private Touch oldTouch2; //Last touch point 2 (finger 2)
void Start()
{
}
void Update() {
// no touch
if ( Input. touchCount <= 0 ){
return;
}
//Single touch, rotate horizontally up and down
if( 1 == Input.touchCount ){
Touch touch = Input.GetTouch(0);
Vector2 deltaPos = touch.deltaPosition;
transform.Rotate(Vector3.down * deltaPos.x , Space.World);
transform.Rotate(Vector3.right * deltaPos.y , Space.World);
}
//Multi-touch, zoom in and out
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//The second point just started touching the screen, only recording, not processing
if( newTouch2.phase == TouchPhase.Began ){
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//Calculate the distance between the old two points and the distance between the new two points. When it becomes larger, you need to enlarge the model, and when it becomes smaller, you need to zoom the model.
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//The difference between the two distances, positive means zoom in gesture, negative means zoom out gesture
float offset = newDistance - oldDistance;
//Amplification factor, one pixel is calculated by 0.01 times (100 can be adjusted)
float scaleFactor = offset / 100f;
Vector3 localScale = transform.localScale;
Vector3 scale = new Vector3(localScale.x + scaleFactor,
localScale.y + scaleFactor,
localScale.z + scaleFactor);
//Minimum zoom to 0.3 times
if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f) {
transform.localScale = scale;
}
//Remember the latest touch point and use it next time
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
Hope it helps you.

Related

Unity, script for rotating the camera by swiping (3d)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
[SerializeField] private float sensitivityHor = 9.0f;
[SerializeField] private float sensitivityVert = 9.0f;
[SerializeField] private float minimumVert = -45.0f;
[SerializeField] private float maximumVert = 45.0f;
private float _rotationX = 0;
private Rigidbody PlayerRigidbody;
void Start()
{
PlayerRigidbody = GetComponent<Rigidbody>();
if (PlayerRigidbody != null)
{
PlayerRigidbody.freezeRotation = true;
}
}
void Update()
{
_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
float delta = Input.GetAxis("Mouse X") * sensitivityHor;
float rotationY = transform.localEulerAngles.y + delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
Good evening. I have written a script to rotate the camera by swiping a finger across the screen
(it is on my camera), everything works correctly with one finger, but if you touch with two fingers at the same time, the application will react incorrectly (suddenly change the camera rotation). How can I fix it using Input.GetAxis or what can I use to write a script for multiple touches?
You may use only the first touch for your movement, so that if there is a second nothing happens and change Input.GetAxis("Mouse Y") by Input.GetTouch(0).deltaPosition.y with x and y respectively. Like this:
if (Input.touchCount > 0) {
_rotationX -= Input.GetTouch(0).deltaPosition.y * sensitivityVert;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
float delta = Input.GetTouch(0).deltaPosition.x * sensitivityHor;
float rotationY = transform.localEulerAngles.y + delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
Code not debugged, as its just your code with the substitution.
If handling the first touch acts weird, you can handle the rotation with the middle point maybe. Like this:
//get the touch middle when the second finger touches
if (Input.GetTouch(1).phase == TouchPhase.Began) {
touchMiddle = (Input.GetTouch(0).position +
Input.GetTouch(1).position) / 2;
touchDistSqr = (Input.GetTouch(0).position -
Input.GetTouch(1).position).sqrMagnitude;
return;
}
if (Input.touchCount == 2) {
var deltaMidde = touchMiddle - (Input.GetTouch(0).position + Input.GetTouch(1).position) / 2;
transform.localEulerAngles = new Vector3(deltaMidde.x, deltaMidde.y, 0);
}
All the code in the LateUpdate().
Preferably camera movements needs to be done in the LateUpdate() for the objects that might have moved inside Update to be at their final state for each frame. As per adviced in the documentation.
Not really relevant but note that you can use Vector2 instead of Vector3for the screen operations most of the time.
Also it is very interesting in the Update or LateUpdate operations to have them multiplied by Time.deltaTime so that the movement is frame rate independent.

the GameObject rotates incorrectly on the z-axis

there is a circle, it needs to be rotated by pressing the button, with a certain speed along the z axis, and when it turns 90, the rotation will stop, but for some reason the rotation is not working properly
public GameObject Circle;
private bool RotationActive;
//rotation value at which the circle stops rotation
private float RotatePost;
private float RotationSpeed;
private float CircleRotateZ;
void Start()
{
RotationActive = false;
RotationSpeed = 0.5f;
RotatePost = 90;
}
//function is bound to a button
public void RotateActive(){
RotationActive = true;
}
void FixedUpdate()
{
if (RotationActive == true)
{
CircleRotateZ = Circle.transform.rotation.z;
//if the circle along the z axis is rotated more than Rotation Post...
if (CircleRotateZ >= RotatePost )
{
RotatePost = CircleRotateZ + 90;
RotationActive = false;
}
//assignment of a new coordinate
Circle.transform.Rotate(new Vector3(0,0,RotationSpeed + CircleRotateZ));
}
}
You are trying to grab the rotation from the Quaternion which is not the angle of the object. Instead you should use:
CircleRotateZ = Circle.transform.eulerAngles.z;
Also the way you are currently rotating will make it speed up over time. The Rotate functions rotates the object by the amount, not to the amount given, so if you only want to rotate by the rotation speed you should use:
Circle.transform.Rotate(new Vector3(0, 0, RotationSpeed));
What do you mean by not working properly. You mean it doesn't move at all? If it doesn't move at all, check if you dragged the circle game object to the game object field in the Inspector. One more thing, why didn't you call the 'RotateActive' function?

Unity Rotation Clamping Issue

I'm trying to implement a system where my camera will zoom in on the front face of a gameobject and I can then rotate around it with clamping.
I've managed to get it work, though I'm guessing I've done it totally wrong - Basically I'm expecting actualAngle to be NEGATIVE when I pan to the left and POSITIVE when I pan to the right.
It does work for targets that have no rotation but not for targets that do have a rotation as the centred angle is not zero.
Here's my function:
float _prevMousePosX;
void RotateCamera()
{
//Allow min -30 degrees when panning to the left
minXLimit = -50;
//And max 30 degrees when panning to the right
maxXLimit = 50;
//Work out how much the mouse has moved
var mouseX = Input.GetAxis("Mouse X");
Vector3 angle = target.position + target.rotation * transform.position * -1;
var actualAngle = angle.x;
//The angle is very small so times it by 100
actualAngle = actualAngle * 100;
//This angle is not defaulting to zero before any rotation has occurred when the game object has a rotation other than zero - I think I should be taking into account the existing rotation somehow
Debug.Log(actualAngle);
//The rest of this code is working correctly
if ((actualAngle > minXLimit && actualAngle < maxXLimit && isMoving))
{
//No - Allow rotating in either direction horizontally
if(isMoving) transform.RotateAround(target.transform.position, Vector3.up, mouseX * 1);
} else
{
//Yes, it's more than 30 degrees either left or right
//Work out which way wer're trying to drag the mouse and whether we should allow rotation further in that direction
if (mouseX>0 && actualAngle < minXLimit)
{
//Don't allow?
}
else if (mouseX<0 && actualAngle < minXLimit)
{
//Allow rotating back
if (isMoving) transform.RotateAround(target.transform.position, Vector3.up, mouseX * 1);
}
else
{
//Mouse isn't moving
}
if (mouseX>0 && actualAngle > maxXLimit)
{
//Allow rotating back
if (isMoving) transform.RotateAround(target.transform.position, Vector3.up, mouseX * 1);
}
else if(mouseX<0 && actualAngle > maxXLimit)
{
//Don't allow rotating in this direction any further
} else
{
//Mouse isn't moving
}
}
_prevMousePosX = mouseX;
}
Issue resolved.
//Work out how close we are to facing each other
//We can minus 180 because we'll always be facing each other, but just not always directly
var newAngle = Quaternion.Angle(target.rotation, transform.rotation) -180;
//Then we need to work out whether we are to it's left or it's right
float rotateDirection = (((transform.eulerAngles.y - target.eulerAngles.y) + 360f) % 360f) > 180.0f ? 1 : -1;
var actualAngle = newAngle;
//And finally we can negate our degrees if we're to it's left
actualAngle = actualAngle * rotateDirection;
You can then use actualAngle to clamp your rotation as I do above.

Car rotation help for a mini game project

I have a mini project where i have a car that drives on a cylinder and is rotating around the cylinder. When i press left mouse click the car rotates to the left and goes to the left and when i press right mouse click it rotates right and it goes right. What i want is when i release the mouse click buttons i want the car to rotate back to face the forward direction of the cylinder and continue going forward along the cylinder i have a script that is working but it is not rotating the car back to forward direction.
//THIS SCRIPT IS ATTACHED TO THE CAR
void Update(){
//CHECKS IF MOUSE BUTTON ARE CLICKED
if (Input.GetMouseButton (0)) {
rotation = -1;
} else if (Input.GetMouseButton (1)) {
rotation = 1;
} else {
rotation = 0;
}
}
void FixedUpdate(){
//moves the car forward
//rgb is the rigidbody of the car
rgb.MovePosition(rgb.position - transform.right * moveSpeed * Time.fixedDeltaTime);
// THE LINES BELOW ROTATE THE CAR TO THE LEFT OR RIGHT
Vector3 yRot = Vector3.up * rotation * rotation_speed * Time.deltaTime;
Quaternion deltaRot = Quaternion.Euler(yRot);
Quaternion targetRot = rgb.rotation * deltaRot;
rgb.MoveRotation(Quaternion.Slerp(rgb.rotation, targetRot, 50f * Time.fixedDeltaTime));
}
This is because you are setting the variable "rotation" to 0.
Vector3 yRot = Vector3.up * rotation * rotation_speed * Time.deltaTime;
Which results to 0 and your object doesn't rotate.
I would recommend you to do something like this in your update method:
if (Input.GetMouseButton (0))
{
rotation = -1;
}
else if (Input.GetMouseButton (1))
{
rotation = 1;
}
else
{
//Now when the rotation is positive,
//the object starts to rotate towards negative, but stops at 0
//Same is true when the rotation is negative
if (rgb.rotation.y > 0)
{
rotation = -1;
}
else if (rgb.rotation.y < 0)
{
rotation = 1;
}
else
{
//This needs to be added here to stop the object from rotating
//when the y rotation is already 0
rotation = 0;
}
}
Now it rotates towards 0 when you don't press anything. Also don't use Time.deltaTime in FixedUpdate.
Edit:
So I noticed that with this method it doesn't quite never return to 0. It gets very close but not close enough. By adding this to FixedUpdate the y rotation gets rounded to 0.
if (Mathf.Abs(targetRot.y) < 0.00001f)
{
targetRot = Quaternion.Euler(new Vector3(targetRot.x, 0, targetRot.z));
}

Unity: How to rotate first person camera by dragging touch across?

Maybe its worth noting that I am using the Fingers asset for touch/mouse.
Okay so I have a turret that has a first person camera, on PC it uses the axis Mouse X/Y as I move the mouse around the camera follows. But I need for Mobile to work with the axis Mouse X/Y to drag the camera using touch.
What I need help with is:
As my fingers drags across the screen to move the camera's rotation not position. The position is modified by the vehicle.
This is the code I use for PC first person, moving the mouse:
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0f);
turretHeadToMove.rotation = rotation;
How would I use touch dragging to move the rotation? instead of mouse?
Any help with this? Thank you
Here is the current code for touch
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Touch touch = Input.GetTouch(0);
x += touch.deltaPosition.x * xSpeed * 0.02f;
y -= touch.deltaPosition.y * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0f);
turretHeadToMove.rotation = rotation;
}
}
For touch you could use
Input.GetTouch(0).deltaPosition
Try fiddle with something using this:
float strength = 2;
if (Input.touchCount == 1) {
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDirection = Input.GetTouch(0).deltaPosition;
// Update ur transform.Rotate the way u desire
// Different depending on using 1st person, 3rd person etc.
}
}