I want to scroll my screen to LHS and RHS. So, iam using the following statement to scroll the screen to RHS and its working fine
solo.scrollToSide(Solo.RIGHT);
but when iam using the following statement to scroll the screen to LHS, the screen is not scrolling to left
solo.scrollToSide(Solo.LEFT);
even i tried solo.scrollToSide(21);
but no use....?
Why don't you use this method:
void drag(float fromX, float toX, float fromY, float toY, int stepCount)
Simulate touching the specified location and dragging it to a new location.
This is the official documentation about method:
drag
public void drag(float fromX,
float toX,
float fromY,
float toY,
int stepCount)
Simulate touching the specified location and dragging it to a new location.
Parameters:fromX - X coordinate of the initial touch, in screen coordinatestoX - X coordinate of the drag destination, in screen coordinatesfromY - Y coordinate of the initial touch, in screen coordinatestoY - Y coordinate of the drag destination, in screen coordinatesstepCount - How many move steps to include in the drag
Related
I have a gameobject with an animator controller. The gameobject moves from the bottom of the screen to the top, this movement is being controlled by the animation of th Y axis.
What i am trying to do is to also move it randomly in the X axis While the this animation is moving it in the Y axis
What i did was to set a few animation events in this main animation:
In this events i am calling a script that moves the object between 2 x positions:
public void RandomX ()
{
var pos = transform.position;
pos.x = Mathf.Clamp(transform.position.x, -1.75f, 2.85f);
print(pos.x);
}
But this is not working. Tha animation plays with no modifications in the X axis.
Thanks in advance for any help provided
The animation overwrites the transform position. For your object to be controlled by code, you would have to change the hierarchy.
-object A with script controlling x position
- object B with animation
Now you can move the parent object A by code. The animation controls only the object B.
I am trying to draw at one place by dragging a mouse (for example, a circle in left upper quadrant) and have the figure appear at 4 places around center of canvas (including one right under the mouse cursor). I have tried following code:
int x=0;
int y=0;
void setup(){
size(400, 400);
background(255);
translate(width/2, height/2);
}
void draw() {
}
void mouseDragged(){
translate(width/2, height/2);
for(int i=0; i<4; i++){
rotate(PI/2);
x= mouseX ; y=mouseY;
line(x, y, mouseX, mouseY);
}
}
However, the figure does not get drawn under the mouse cursor. If I am very close to upper left corner, the lines get drawn at center of canvas.
Secondly, a total of 5 figures get drawn instead of 4 as I am trying. If I draw near left upper corner, the unwanted figure appears at right lower corner of canvas. This unwanted figure is fainter than other figures.
Where are the errors and how can both these problems be corrected? I want to achieve this with rotate commands (rotating around center of canvas), though I saw that I can also use translate command to draw at 4 places on canvas.
Thanks for your help.
None of your points are under the mouse because you're translating the canvas (to width/2, height/2) before drawing your points, but you're using mouseX, mouseY to draw them. Think of mouseX, mouseY as the X and Y distance from the upper-left corner. If you want a point to be under the mouse, you have to use the distance from the point you translated to!
In other words, instead of this:
x = mouseX;
y = mouseY;
You want this:
x = width/2 - mouseX;
y = height/2 - mouseY;
As for the extra point being drawn, that seems to be a side-effect of using the mouseDragged() function. Something strange is going on there, but you can get around this problem by just using the draw() function and the mousePressed variable instead of the mouseDragged() function.
Edit: I switched the order they should be in. Check out my answer to your next question for a better explanation.
I'm trying to make my UI elements work and remain the same for every different resolution. I added a Canvas Scaler to my Canvas and played around with the settings until it looked finished.
I then tried building the game and running it at few different resolutions to confirm that it was working. However, the Canvas Scaler doesn't seems to work.
http://prntscr.com/d1afz6
Above is some random resolution but that's how big my editor screen is and that's what I'm using as my reference resolution. That's also the hierarchy for this specific Canvas http://prntscr.com/d1aggx. It takes almost the whole screen when ran at 640x480. I have no clue why this is not working. I've read most of the unity guides on that but none of them seem to have that problem.
Ok, to fit something no matter the size of the screen, you have to use a separate coordinate system than Unity's absolute system. One of Unity's models is the View. The View is coordinates 0,0 at the top left, and 1,1 at the bottom right. Creating a basic Rect that handles that, is the following.
using UnityEngine;
namespace SeaRisen.nGUI
{
public class RectAnchored
{
public float x, y, width, height;
public RectAnchored(float x, float y, float width, float height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public static implicit operator Rect(RectAnchored r)
{
return new Rect
{
x = r.x * Screen.width,
y = r.y * Screen.height,
width = r.width * Screen.width,
height = r.height * Screen.height
};
}
}
}
Here, we take the normal Rect floats, the x,y coordinates along with a width and height. But these are in the values [0..1]. I don't clamp it, so it can be tweened on and off the screen with animation, if desired.
The following is a simple script that create's a button in the lower right corner of the screen, and resizes as the screen grows or shrinks.
void MoveMe()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit, float.MaxValue)
|| Physics.Raycast(transform.position, Vector3.up, out hit, float.MaxValue))
transform.position = hit.point + Vector3.up * 2;
}
void OnGUI()
{
if (GUI.Button(new RectAnchored(.9f, .9f, .1f, .1f), "Fix me"))
{
MoveMe();
}
}
The X is .9 to the right and Y .9 from the top, and width and height of .1, so the button is 1/10th of the screen in height and width, positioned in the bottom 1/10th of the screen.
Since OnGUI is rendered every frame (or so), the button rect updates with the screen resize automatically. The same would work in a typical UI, if you are using Update() to render your windows.
I hope this explains the difference between what I meant with absolute coordinates. Setting say the previous example (using absolutes) in 640x480, it'd be something like new Rect(576, 432, 64, 48) and it wouldn't scale. By using new RectAnchored(.9f, .9f, .1f, .1f) and have it rendered into UI space based on Screen size, then it scales automatically.
I'm using the following script to control a ball but it doesn't do exactly what I want.
Our 3D game will be played in landscape mode with the home button (or bottom) of the device in the right hand. Tilting (not turning) the device to the left should make the ball roll left, tilting to the right should make it roll right. Tilting the device down (top of device going down) should make the ball roll faster and tilting the device upward should slow it down.
I don't want the ball to indefinitely accelerate either.
The code below wants the device held straight as opposed to laying flat and it moves the ball by turning the device not by tiling it.
void FixedUpdate()
{
// Player movement in mobile devices
// Building of force vector
Vector3 movement = new Vector3(-Input.acceleration.x, 0.0f, -Input.acceleration.z);
// Adding force to rigidbody
var move = movement * speed * Time.deltaTime;
rigidbdy.AddForce(move);
}
For your tilting problem you likely just need to choose something other than (-Input.acceleration.x, 0.0f, -Input.acceleration.z);, in the example in the documentation they do (-Input.acceleration.y, 0.0f, Input.acceleration.x); to do tilt controls.
For the max speed issue, just add a check for rigidbdy.velocity.magnitude > maxSpeed in your code and cap the value if it is maxed.
public float maxSpeed;
void FixedUpdate()
{
// Player movement in mobile devices
// Building of force vector
Vector3 movement = new Vector3(-Input.acceleration.y, 0.0f, Input.acceleration.x);
// Adding force to rigidbody
var move = movement * speed * Time.deltaTime;
rigidbdy.AddForce(move);
//Limits the max speed
if(rigidbdy.velocity.magnitude > maxSpeed)
{
rigidbdy.velocity = rigidbdy.velocity.normalized * maxSpeed;
}
}
That will cause the velocity to be capped to whatever value you have set for maxSpeed in the inspector.
I'm trying to flip some animations in LibGDX, but because they are of different width, the animation plays weird. Here's the problem:
(the red dot marks the X/Y coordinate {0,0})
As you can see, when the animation plays "left" when you punch, the feet starts way behind than were it was, but when you punch right, the animations plays fine because the origin of both animations is the left corner, so the transition is smooth.
The only way I think of achieving what I want is to see what animation is playing and adjust the coordinates accordingly.
This is the code:
public static float draw(Batch batch, Animation animation, float animationState,
float delta,
int posX, int posY, boolean flip) {
animationState += delta;
TextureRegion r = animation.getKeyFrame(animationState, true);
float width = r.getRegionWidth() * SCALE;
float height = r.getRegionHeight() * SCALE;
if (flip) {
batch.draw(r, posX + width, posY, -width, height);
} else {
batch.draw(r, posX, posY, width, height);
}
return animationState;
}
Any suggestion is welcome as how to approach this.
Use some other batch.draw option (with other parameters). You can set "origin" parameters. It's like a hot spot...center of the image... So if you i.e. rotate, rotation will be done around that hot spot.
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Batch.html
I didn't use it for flipping, but it should work the same way. But if it doesn't then you have to adjust coordinates on your own, make some list with X offset for every frame and add it for flipped images.
Other solution would be to have wider frame images and keep center of the character always match the center of the image. That way your images will be wider then they have to - you'll have some empty space, but for sane number of frame it's acceptable.