Need to make Joystick With Four Directions - iphone

Hi. How can I make the joystick which is movable only in four directions? Can anyone give me some suggestions?
Write now I am using this code for joystick. How to make this one to allow only in four directions?
-(void) trackVelocity:(CGPoint) nodeTouchPoint {
CGPoint ankPt = self.anchorPointInPoints;
// Get the touch point relative to the joystick home (anchor point)
CGPoint relPoint = ccpSub(nodeTouchPoint, ankPt);
// Determine the raw unconstrained velocity vector
CGPoint rawVelocity = CGPointMake(relPoint.x / travelLimit.x,relPoint.y / travelLimit.y);
// If necessary, normalize the velocity vector relative to the travel limits
CGFloat rawVelLen = ccpLength(rawVelocity);
velocity = (rawVelLen <= 1.0) ? rawVelocity : ccpMult(rawVelocity, 1.0f/rawVelLen);
// Calculate the vector in angular coordinates
// ccpToAngle returns counterclockwise positive relative to X-axis.
// We want clockwise positive relative to the Y-axis.
CGFloat angle = 90.0- CC_RADIANS_TO_DEGREES(ccpToAngle(velocity));
if(angle > 180.0) {
angle -= 360.0;
}
// angularVelocity.radius = ccpLength(velocity);
// angularVelocity.heading = angle;
// Update the thumb's position, clamping it within the contentSize of the Joystick
[thumbNode setPosition: ccpAdd(ccpCompMult(velocity, travelLimit), ankPt)];
}

Refer the following sample,
https://github.com/jasarien/JSController
It consists of four direction button, free drag view and two buttons, it will be helpful for your question.

Related

Spin The Wheel With Touch Inputs in Unity

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.

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.

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.
}
}

Swift SpriteNode Initial Angle

I'm trying to set the initial angle of my sprite to be 90 degrees. I don't want to call an action, is there a way of setting the angle parameter of a sprite as you do with the position?
zRotation
You can use the zRotation property of SKNode which accept radiants
Degrees to Radiants
There's a useful extension for that
extension Int {
var degreesToRadians: Double { return Double(self) * M_PI / 180 }
var radiansToDegrees: Double { return Double(self) * 180 / M_PI }
}
sprite.zRotation = CGFloat(90.degreesToRadians)
From the Api Doc
The Euler rotation about the z axis (in radians).
The default value is 0.0, which indicates no rotation. A positive value indicates a counterclockwise rotation. When the coordinate system is rotated, it affects the node and its descendants. The rotation affects the node’s frame property, hit testing, rendering, and other similar characteristics.
The Yoshi's Island image comes form here.

Unity 2D Android Limit GameObject transform horizontal

Is there a way to limit the transform of a game object on the x axis? I am using GUI Textures as buttons to slide a game object right and left, but I don't want to it to slide outside of the screen view. Any advice is appreciated.
public float moveSpeed = 0.5f;
void Update () {
float horiz = Input.GetAxis ("Horizontal");
transform.Translate(new Vector3(horiz * moveSpeed,0,0));
}
You can use the Mathf.clamp() method.
Example:
float xPos = Mathf.clamp(xPos, minX, maxX);
This makes sure that if xPos goes below minX it will be set to minX and if it goes above maxX it will be set to maxX meaning that xPos can never be higher than maxX or lower than minX.
A simple way is to put game objects (ex. cube) as boundary of the screen. That way, you cant move your game object outside the screen, because it will collide with the boundary and stop going outside the screen.