Swift SpriteNode Initial Angle - swift

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.

Related

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?

Camera rotation with interpolation

I have an object that rotates on the y axis with the "Mouse X" input and a video camera that through a slerp quaternion should follow the rotation of the object.
Unfortunately, when I rotate the object with fast mouse movements, the rotation of the slerp camera stops as if it hits a wall and does not follow that of the object.
Without quaternion slerp it works following the object but I need to do it with the interpolation.
rotation object (in LateUpdate:
float rot = Input.GetAxisRaw("Mouse X") * speedrotation;
transform.Rotate(0, rot, 0);
rotation camera (in LateUpdate):
var rot = Quaternion.Slerp(camera.transform.rotation, object.rotation, speed_rot * Time.deltaTime);
camera.transform.position = object.transform.position + rot;
camera.transform.LookAt(object.transform.position);
Try EulerAngles instead! That is better option for make camera controller!
Simple Code:
Vector3 currentCameraEulers;
float rotationDPI = 7f; // I didn't remeber how to call better this thing
void Update(){
float cameraX = Input.GetAxisRaw("Mouse X") * rotationDPI;
if(cameraX != 0f){
currentCameraEulers = new Vector3(0, cameraX, 0);
transform.eulerAngles += currentCameraEulers;
}
}
That should work completely fine.

Orbiting target with camera and changing height

I want to be able to use joystick (left-right) to make the camera orbit around target. I have this handled with the code below:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class OrbitCamera : MonoBehaviour {
public Transform target;
public float turnSpeed;
public float height;
public float distance;
private Vector3 offsetX;
void Start()
{
offsetX = new Vector3 (0, height, distance);
}
void LateUpdate()
{
offsetX = Quaternion.AngleAxis (CrossPlatformInputManager.GetAxis ("hOrbit") * turnSpeed, Vector3.down) * offsetX;
transform.position = target.position + offsetX;
transform.LookAt (target.position);
}
}
I need to extend this script so that the player can move camera also up and down using the same joystick. So what I want is the camera to be able to move around player in a shape of sphere, always looking at the player. Thanks in advance.
Use a boom camera
A boom camera is wonderfully easy to setup and has a range of convenient cinematic controls:
To set it up, you simply create a new game object which we'll call the dolly. You then simply parent the camera to the dolly and tidy up the positions and rotations:
Camera
Transform: 0,0,-distance
Rotation: 0,0,0
Dolly
Transform: 0,height,0
Rotation: 0,0,0
Why a boom camera is great
Rotate the dolly on x and y and you'll get the camera moving in a sphere. (Note that dragging the rotation gizmo in scene view doesn't show the effect properly because of axis alignment; edit the x/y rotation values in the inspector only).
That -distance z value is the zoom. Change the local position of the camera to zoom in/out.
Local rotating the camera gives interesting tilt/pan effects.
Making it work in your case
You'd attach the script to the dolly gameobject, then use the joystick input to rotate it on x/y:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class OrbitCamera : MonoBehaviour {
// Height and distance are now set in the scene (by positioning the dolly and changing that camera z value).
public float turnSpeed;
private float horizontal;
private float vertical;
void LateUpdate()
{
// Update horizontal/ vertical angles:
horizontal += CrossPlatformInputManager.GetAxis ("hOrbit") * turnSpeed;
vertical += CrossPlatformInputManager.GetAxis ("vOrbit") * turnSpeed;
// Update the rotation:
transform.rotation = Quaternion.Euler(horizontal, vertical, 0f);
}
}

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.

Need to make Joystick With Four Directions

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.