Unity3D Touch Rotate Object - android

I would like to rotate my gameobject without using the z-axis, so that it only rotates horizontal and vertical.
Right now I am using this code
void Update () {
if (Input.touchCount == 1) {
var touch = Input.GetTouch(0);
switch(Input.GetTouch(0).phase){
case TouchPhase.Moved:
float swipeDistVertical = (new Vector3(0, touch.deltaPosition.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > 0)
{
float swipeValue = Mathf.Sign(touch.deltaPosition.y - startPos.y);
if (swipeValue > 0 || swipeValue < 0)//up swipe
{
vertical = true;
horizontal = false;
}
}
float swipeDistHorizontal = (new Vector3(touch.deltaPosition.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > 0)
{
float swipeValue = Mathf.Sign(touch.deltaPosition.x - startPos.x);
if (swipeValue > 0 || swipeValue < 0)//right swipe
{
horizontal = true;
vertical = false;
}
}
if(vertical)
{
transform.Rotate(touch.deltaPosition.y * 0.3f, 0,0,Space.World);
}
if(horizontal)
{
transform.Rotate(0,touch.deltaPosition.x * 0.3f,0,Space.World);
}
break;
}
}
}
I got this code from this link
Right now I can rotate, but it rotates on the z-axis aswell which I don't want. And it doesn't handle the vertical swipe right, it switches between both instead of recognizing that it is vertical right now.
I use Unity 4.6.2 and this should work on iOS and Android.

Add following code before where you've put "break".
float z=transform.rotation.z;
transform.Rotate(0,0,-z);

Related

Unity how can i swerve box with use joint?

I want to get an image like in the picture with code. I stacked boxes because i must add joint with code. I want to make the boxes look like this as the character slides left and right smoothly. And how can i smooth swerving control in unity for touch or mouse button?
I tried this codes for movement:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
firstPos = touch.position;
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
Swerving(touch.position);
}
private void Swerving(Vector2 touch)
{
endPos = touch;
float diff = endPos.x - firstPos.x;
transform.Translate(diff * Time.deltaTime * swerveSpeed, 0, 0);
}
But not smooth swerving.
I tried hinge joint for image. I tried random values to motor,spring etc. But it didnt work i have never used joints.
In general when dealing with physics you shouldn't use transform this break collision detection and other physics related things like your joints etc.
Try and rather go through the Rigidbody using e.g. Rigidbody.MovePosition (or accordingly Rigidbody2D.MovePosition if your are in 2D) within FixedUpdate like e.g.
// or Rigidbody2D depending on your needs
[SerializeField] private Rigidbody _rigidbody;
private Vector2 startPos;
private float movement;
private void Awake()
{
if(!_rigidbody) rigidbody = GetComponent<Rigidbody>();
}
// get user input in Update in general
private void Update()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
switch(touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
movement = 0;
break;
case TouchPhase.Stationary:
case TouchPhase.Moved:
movement = touch.position.x - startPos.x;
break;
default:
movement = 0;
}
}
}
// Apply Physics in FixedUpdate!
private void FixedUpdate()
{
_rigidbody.MovePosition(_rigidbody.position + Vector3.right * movement * Time.deltaTime * swerveSpeed);
}
However, in general the touch is a 2D pixel space position which might be quite different on different devices.
I would rather translate it into a 3D world space position and use something like e.g.
[SerializeField] private Rigidbody _rigidbody;
[SerializeField] private Camera _mainCamera;
private float _offset;
private Plane? _hitPlane;
private Vector3 _targetPosition;
private void Awake()
{
if (!_rigidbody) _rigidbody = GetComponent<Rigidbody>();
if (!_mainCamera) _mainCamera = Camera.main;
}
// get user input in Update in general
private void Update()
{
_targetPosition = _rigidbody.position;
if (Input.touchCount <= 0)
{
return;
}
var touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
{
// Check if you are touching the character
var ray = _mainCamera.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out var hit) && hit.rigidbody == _rigidbody)
{
// create a virtual plane parallel to the camera view
// passing the hit point
_hitPlane = new Plane(-ray.direction, hit.point);
// also store the offset from pivot to the hit point
// we only care about the X axis
_offset = (_rigidbody.position - hit.point).x;
}
break;
}
case TouchPhase.Stationary:
case TouchPhase.Moved:
{
if (_hitPlane.HasValue)
{
// now instead keep ray casting against that virtual plane
var ray = _mainCamera.ScreenPointToRay(touch.position);
if (_hitPlane.Value.Raycast(ray, out var distance))
{
var hitPoint = ray.GetPoint(distance);
_targetPosition.x = hitPoint.x + _offset;
}
}
break;
}
default:
_hitPlane = null;
break;
}
}
// Apply Physics in FixedUpdate!
private void FixedUpdate()
{
_rigidbody.MovePosition(_targetPosition);
}
this makes sure your character is definitely placed on the X axis where you are dragging it without any delays but still complying with physics

Detect swipe without losing immediate response of tap

I'm creating an Android game with Unity. There are only three ways to control the movement of the character:
Tap in the right half of the screen: jump to the right
Tap in the left half of the screen: jump to the left
Swipe upwards: character dashes forward
In theory, I know that I can differentiate the touches with the TouchPhases (began, moved, stationary and ended). When only detecting the taps without caring for swipes, I just checked if the phase of the touch began and made the player jump. That felt fast on my device.
However, because I have to consider that a swipe may follow, I can not initiate the jump action until I detected ThouchPhase.Ended. This leads to a very slow responding character, which doesnt jump until the user rises his finger of the screen.
I tried to use ThouchPhase.Moved and ThouchPhase.Stationary instead to simulate a immediate response but my solution is pretty bad in terms of detecting the difference between a tap and a swipe:
Vector2 startTouchPosition;
Vector2 endTouchPosition;
Vector2 currentSwipe;
void Update()
{
if (Input.touches.Length > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began)
{
//save began touch 2d point
startTouchPosition = new Vector2(touch.position.x, touch.position.y);
}
if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
{
//save ended touch 2d point
endTouchPosition = new Vector2(touch.position.x, touch.position.y);
if (endTouchPosition.y - startTouchPosition.y < 5)
{
if (touch.position.x > (Screen.width / 2))
{
JumpToRight();
}
else if (touch.position.x < (Screen.width / 2))
{
JumpToLeft();
}
}
else
{
//create vector from the two points
currentSwipe = new Vector2(endTouchPosition.x - startTouchPosition.x, endTouchPosition.y - startTouchPosition.y);
//normalize the 2d vector
currentSwipe.Normalize();
//swipe upwards
if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
DashForward();
}
}
}
}
}
}
Here is the code I used. I tested and it works but sometimes, I noted a delay. Let me know if it is good enough for you. Basically you dont need to go through all touches if you dont need multi-touch. And you just need Begin and End touch phases.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class touch : MonoBehaviour {
private Vector2 startTouchPosition;
private Vector2 endTouchPosition;
private Vector2 currentSwipe;
public Text textbox;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touches.Length > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
//save began touch 2d point
startTouchPosition = new Vector2(touch.position.x, touch.position.y);
}
if (touch.phase == TouchPhase.Ended)
{
//save ended touch 2d point
endTouchPosition = new Vector2(touch.position.x, touch.position.y);
//create vector from the two points
currentSwipe = new Vector2(endTouchPosition.x - startTouchPosition.x, endTouchPosition.y - startTouchPosition.y);
//normalize the 2d vector
currentSwipe.Normalize();
if(Mathf.Abs(currentSwipe.y) < 0.1f && Mathf.Abs(currentSwipe.x) < 0.1f)
{
if (touch.position.x > (Screen.width / 2))
{
textbox.text= "jump right";
}
else if (touch.position.x < (Screen.width / 2))
{
textbox.text= "jump left";
}
}
if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
textbox.text= "Dash forward";
}
}
}
}
}

Multi-touch bug on my first Android game

Right now I've been developing a game which will make the character move up or down if either swipe up or down on the left side of the screen. Then, he can also, shoot projectiles if I tap the right side of the screen. I've succeeded on making it work but the problem is that the swipe function do not works while I tap the the right screen. Both only works if I do one of them. I cannot move the character if I am firing projectiles. Any suggestions? Appreciate it in advanced.
Here's the code:
For the swipe movement function:
void Swipe() {
if (Input.touchCount > 0) {
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
lp = t.position;
fp = t.position;
text.text = fp.x.ToString();
}
else if (t.phase == TouchPhase.Ended)
{
lp = t.position;
//Swipe Up
if (fp.y < lp.y && fp.x < 400)
{
currentLane += 1;
}
//Swipe Down
else if (fp.y > lp.y && fp.x < 400)
{
currentLane -= 1;
}
}
}
}
And here's the code for the tap or firing projectiles function:
void FireBullets() {
interval += 2 * Time.deltaTime;
anim.SetBool("Attacking", firing);
if (Input.touchCount > 0 && interval > .75f) {
Vector3 bulletTouchPos;
Touch bulletT = Input.GetTouch(0);
bulletTouchPos = bulletT.position;
if (bulletT.phase == TouchPhase.Began) {
if (bulletTouchPos.x > 400)
{
interval = 0;
firing = true;
//Fire Bullets
Instantiate(bullets, new Vector3(transform.position.x + 1.2f, transform.position.y + .3f, transform.position.z), Quaternion.identity);
}
}
}else
{
firing = false;
}
}
Your lp and fp values don't care which finger is being checked.
If you want to detect multiple touch gestures at once, you need to discriminate your detection based on which finger that is.
You can do this by looking at the Touch.fingerId value, which is unique for each finger. Fingers are just an ID assigned to a given touching point as it moves across the screen in order to identify it from other touching points currently also on the screen.
You'll need to adjust your code to handle how to store the information you need in order to do what you need, but this should get you started.

swipe gestures on android in unity

I'm trying to get unity to recognize that I am swiping left to right, I have solved that but my issue is that it doesn't understand this till I lift my finger off the screen.
My question is how would i make it so that it knows i went right and then left and then right again all without ever taking my finger of the screen
Here is the code I have so far
using UnityEngine;
using System.Collections;
public class Gestures : MonoBehaviour {
private Vector2 fingerStart;
private Vector2 fingerEnd;
public int leftRight = 0;
public int upDown = 0;
void Update () {
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerStart = touch.position;
fingerEnd = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
fingerEnd = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
if((fingerStart.x - fingerEnd.x) > 80 || (fingerStart.x - fingerEnd.x) < -80) // Side to side Swipe
{
leftRight ++;
}
else if((fingerStart.y - fingerEnd.y) < -80 || (fingerStart.y - fingerEnd.y) > 80) // top to bottom swipe
{
upDown ++;
}
if(leftRight >= 3){
leftRight = 0;
}
if(upDown >= 4){
upDown = 0;
}
}
}
}
}
The issue you're facing is because you've done your checks in the TouchPhase.Ended. What you want to do is perform your checks in TouchPhase.Moved, with a smaller change in value (you're using 80 in Ended, try something like 10 if you the code doesn't work)
Unity's documentation on TouchPhase http://docs.unity3d.com/ScriptReference/TouchPhase.html
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerStart = touch.position;
fingerEnd = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
fingerEnd = touch.position;
if((fingerStart.x - fingerEnd.x) > 80 ||
(fingerStart.x - fingerEnd.x) < -80) // Side to side Swipe
{
leftRight ++;
}
else if((fingerStart.y - fingerEnd.y) < -80 ||
(fingerStart.y - fingerEnd.y) > 80) // top to bottom swipe
{
upDown ++;
}
if(leftRight >= 3){
leftRight = 0;
}
if(upDown >= 4){
upDown = 0;
}
//After the checks are performed, set the fingerStart & fingerEnd to be the same
fingerStart = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
leftRight = 0;
upDown = 0;
fingerStart = Vector2.zero;
fingerEnd = Vector2.zero;
}
If you want to explicitly check for a pattern (i.e. left -> right -> left), rather than just checking if it's some lateral / vertical movement as the code you have will do, try the below code. Just remember to include System.Collentions.Generic & System.Linq namespaces
private Vector2 fingerStart;
private Vector2 fingerEnd;
public enum Movement
{
Left,
Right,
Up,
Down
};
public List<Movement> movements = new List<Movement>();
void Update () {
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began) {
fingerStart = touch.position;
fingerEnd = touch.position;
}
if(touch.phase == TouchPhase.Moved) {
fingerEnd = touch.position;
//There is more movement on the X axis than the Y axis
if(Mathf.Abs(fingerStart.x - fingerEnd.x) > Mathf.Abs(fingerStart.y - fingerEnd.y)) {
//Right Swipe
if((fingerEnd.x - fingerStart.x) > 0)
movements.Add(Movement.Right);
//Left Swipe
else
movements.Add(Movement.Left);
}
//More movement along the Y axis than the X axis
else {
//Upward Swipe
if((fingerEnd.y - fingerStart.y) > 0)
movements.Add(Movement.Up);
//Downward Swipe
else
movements.Add(Movement.Down);
}
//After the checks are performed, set the fingerStart & fingerEnd to be the same
fingerStart = touch.position;
//Now let's check if the Movement pattern is what we want
//In this example, I'm checking whether the pattern is Left, then Right, then Left again
Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
}
if(touch.phase == TouchPhase.Ended)
{
fingerStart = Vector2.zero;
fingerEnd = Vector2.zero;
movements.Clear();
}
}
}
private bool CheckForPatternMove (int startIndex, int lengthOfPattern, List<Movement> movementToCheck) {
//If the currently stored movements are fewer than the length of the pattern to be detected
//it can never match the pattern. So, let's get out
if(lengthOfPattern > movements.Count)
return false;
//In case the start index for the check plus the length of the pattern
//exceeds the movement list's count, it'll throw an exception, so lets get out
if(startIndex + lengthOfPattern > movements.Count)
return false;
//Populate a temporary list with the respective elements
//from the movement list
List<Movement> tMovements = new List<Movement>();
for(int i = startIndex; i < startIndex + lengthOfPattern; i++)
tMovements.Add(movements[i]);
//Now check whether the sequence of movements is the same as the pattern you want to check for
//The SequenceEqual method is in the System.Linq namespace
return tMovements.SequenceEqual(movementToCheck);
}
EDIT Added some more code as a sample
//The idea of a pattern match is to check for the exact same set of swipe gesture.
//This requires the following conditions to be met
// (a) The List of movements that need to be checked must be at least as long as the List of movements to check against.
// (b) The correct indices should be used for the startIndex. In this case I'm just using 0 as the startIndex.
// (c) Remember to clear the List right after you get a true return from the method, otherwise the next return will most likely be a false.
//Example - Training set is Left -> Right -> Left (This is what we want to check)
// Step 1 - User swipes LEFT, method returns false because there are too few Movements to check
// Step 2 - User swipes RIGHT, method returns false (same reason as above)
// Step 3a - User swipes RIGHT (L, R, R now) - false, incorrect pattern (L, R, R instead of L, R, L)
// Step 3b - User swipes LEFT (L, R, L now) - TRUE, Correct pattern!
//Immediately clear if Step 3b happens otherwise Step 4 will occur
// Step 4 - User swipes L or R (direction is immaterial right now), and method will return FALSE
// if you use the last three indexes!
//Pre-populating the movements List with L, R, L
movements = new List<Movement>()
{
Movement.Left,
Movement.Right,
Movement.Left
};
//Checking a match against an L, R, L training set
//This prints true to the console
Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
Here's how my Update function looks like. Note the usage of GetMouseButton over Input.touch
void Update () {
//Example usage in Update. Note how I use Input.GetMouseButton instead of Input.touch
//GetMouseButtonDown(0) instead of TouchPhase.Began
if (Input.GetMouseButtonDown(0)) {
fingerStart = Input.mousePosition;
fingerEnd = Input.mousePosition;
}
//GetMouseButton instead of TouchPhase.Moved
//This returns true if the LMB is held down in standalone OR
//there is a single finger touch on a mobile device
if(Input.GetMouseButton(0)) {
fingerEnd = Input.mousePosition;
//There was some movement! The tolerance variable is to detect some useful movement
//i.e. an actual swipe rather than some jitter. This is the same as the value of 80
//you used in your original code.
if(Mathf.Abs(fingerEnd.x - fingerStart.x) > tolerance ||
Mathf.Abs(fingerEnd.y - fingerStart.y) > tolerance) {
//There is more movement on the X axis than the Y axis
if(Mathf.Abs(fingerStart.x - fingerEnd.x) > Mathf.Abs(fingerStart.y - fingerEnd.y)) {
//Right Swipe
if((fingerEnd.x - fingerStart.x) > 0)
movements.Add(Movement.Right);
//Left Swipe
else
movements.Add(Movement.Left);
}
//More movement along the Y axis than the X axis
else {
//Upward Swipe
if((fingerEnd.y - fingerStart.y) > 0)
movements.Add(Movement.Up);
//Downward Swipe
else
movements.Add(Movement.Down);
}
//After the checks are performed, set the fingerStart & fingerEnd to be the same
fingerStart = fingerEnd;
//Now let's check if the Movement pattern is what we want
//In this example, I'm checking whether the pattern is Left, then Right, then Left again
Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
}
}
//GetMouseButtonUp(0) instead of TouchPhase.Ended
if(Input.GetMouseButtonUp(0)) {
fingerStart = Vector2.zero;
fingerEnd = Vector2.zero;
movements.Clear();
}
}

Move animated movieClips using buttons instead of arrow keys

I'm trying to develop a game where I want my character to run when I click a button, and continue running if I hold the button. I'm new to ActionScript 3, so I'm a bit lost here.
I've found code that satisfies my requirements; but it uses the arrow keys, as below:
function moveRunKei() {
if (Key.isDown(Key.RIGHT)) {
dx = 15; //speed
runKei._xscale = 50;
} else if (Key.isDown(Key.LEFT)) {
dx = -15;
runKei._xscale = -50;
} else {
dx = 0;
}
runKei._x += dx;
if (runKei._x < 100) runKei._x = 100; //30
if (runKei._x > 550) runKei._x = 550;
if (dx != 0 && runKei._currentframe == 1) {
runKei.gotoAndPlay("run");
} else if (dx == 0 && runKei._currentframe != 1) {
runKei.gotoAndStop("stand");
}
}
this.onEnterFrame = function(){
moveRunKei();
}
I need to be able to do this using buttons.
////////////////////////////////////////////////////////////////////////////////////
import flash.events.Event;
var mouseDown:Boolean;
var speed:Number=4;
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onMouseDown(event:MouseEvent):void
{
mouseDown = true;
}
function onMouseUp(event:MouseEvent):void
{
mouseDown = false;
}
function onEnterFrame(event:Event):void
{
if (mouseDown)
{
runKei.gotoAndPlay("Run");
runKei.x += speed;
}
}
This code able to make my character move continuously when I hold the button but it didn't animate while it move(the character freeze until I release the button) - I'm not sure how to explain it.
You'll need to add event listeners for mouse down and mouse up on each of the buttons for movement. Then have booleans that keep track of whether or not the button is down.
It's worth mentioning the code you've linked seems to be actionscript2 so I've changed it to work with as3
var leftDown:Boolean = false;
var rightDown:Boolean = true;
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
rightButton.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
leftButton.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
rightButton.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
leftButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseUp)
rightButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseUp)
function onMouseDown(e:MouseEvent):void
{
//since you can't click on two things at once, this is fine.
rightDown = (e.target == rightButton);
leftDown = (e.target == rightButton);
}
function onMouseDown(e:MouseEvent):void
{
//since you can't click on two things at once, this is fine.
rightDown = (e.target == rightButton);
leftDown = (e.target == leftButton);
}
function moveRunKei()
{
if (rightDown) {
dx = 15; //speed
runKei.scaleX = -0.5;
} else if (leftDown) {
dx = -15;
runKei.scaleX = -0.5;
} else {
dx = 0;
}
runKei.x += dx;
if (runKei.x < 100) runKei.x = 100; //30
if (runKei.x > 550) runKei.x = 550;
if (dx != 0 && runKei.currentFrame == 1)
{
runKei.gotoAndPlay("run");
}
else if (dx == 0 && runKei.currentFrame != 1)
{
runKei.gotoAndStop("stand");
}
}
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void
{
moveRunKei();
}

Categories

Resources