I implemented a GUI Skin to my script. Although in button options, I implement a texture to onActive background texture, it did not work. Except this, all of the changes are working inside GUI Skin. The texture which I used in onActive texture type is Sprite(2D and UI). You may see how I implement in the onGUI() method in the script to the following code.
//...
public GUISkin mySkin;
//...
private void OnGUI()
{
//...
GUI.skin = mySkin;
turkishButton.onClick.AddListener(() => ButtonClickLanguageEvent.Add(1));
englishButton.onClick.AddListener(() => ButtonClickLanguageEvent.Add(2));
latinButton.onClick.AddListener(() => ButtonClickLanguageEvent.Add(3));
Vector3 scale = new Vector3(Screen.width / nativeSize.x, Screen.height / nativeSize.y, 1.0f);
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, scale);
float spacing = 30;
float x = 7 + spacing;
float y = 63;
//...
}
No matter how much I researched the cause of the problem, I could not find it. If it helps, I can throw out any part of the code.
As seen from the pictures, although I added a blue texture to the background of "onActive", nothing changed when I clicked.
Related
This is my first from scratch game project. I'm trying to make a pinball game but I don't want to just "watch a video on how to make a pinball game". I want to run into the problems and learn how to tackle them as they come.
So far, attaching script to a sprite was issue #1 but I've kinda worked that out. Issue #2 was creating variables and having them translate to real object values. After multiple hours of trial and error I eventually just copied someone elses script that had the most basic setup possible, then broke it and rebuilt it to what I have below with the addition of void Update.
My question is mostly to gather a better understanding but also about a new problem of mine.
Issue #3 is currently when I click play, it moves the object only once. I thought void update is supposed to call every frame?
I would also like to know why when I do transform.position, why can't I do transform.position += (value 1, value 2)? From what I've come up with from experimenting, the only way to alter transform.position is to do = new Vector everytime which I don't fully understand... Another way of wording this part of the question would be: Is there a shorter way of writing a vector transformation or is this the only way the change can be written?
Below is the code. I appreciate any answers even if it's simply directing on the right path to find the information I want.
public float width, height, xSpeed, ySpeed, xPosition, yPosition;
public Vector2 position, scale;
void Start() {
// Initialise the variables
width = 0.5f;
height = 0.5f;
xSpeed = 0;
ySpeed = -1f;
xPosition = 0;
yPosition = 3.5f;
// set the scaling
Vector2 scale = new Vector2(width, height);
transform.localScale = scale;
// set the position
transform.position = new Vector2(xPosition, yPosition);
}
void Update() {
transform.position = new Vector2(xPosition + xSpeed,
yPosition + ySpeed);
}
First I would recommend you changing the title of the question to something that is a bit more on point, so that people have an idea what programming concept your question is about! :) I would recommend something like "How to do vector addition in Unity".
I will go through your questions one-by-one:
Yes, the Update-Function is called every frame! With each call of Update() you set your position to exactly the same value again and again. That is why it is not moving. Neither the xPosition/yPosition nor the xSpeed/ySpeed variables are changing after they have been defined in Start(), so your Update Function will always set your position to (0, 2.5, 0).
You can do Vector addition! But in order to do that, you need to properly write it in your code, by which I mean you need to make a vector out of the values you want to add and only then you can add them to the position vector! So if you wanted to add the xSpeed/ySpeed values ontop of your position, it would look like that:
transform.position += new Vector2(xSpeed, ySpeed);
I hope that helps!
With the help of Jayne, this is what the code ended up turning into, hope this helps anyone else who just wants a straightforward way to alter a vector:
public float width, height, xSpeed, ySpeed, xPosition, yPosition;
public Vector2 position, scale;
void Start() {
// Initialise the variables
width = 0.5f;
height = 0.5f;
xSpeed = 0;
ySpeed = -0.01f;
xPosition = 0;
yPosition = 3.5f;
// set the scaling
Vector2 scale = new Vector2(width, height);
transform.localScale = scale;
// set the position
transform.position = new Vector2(xPosition, yPosition);
}
void Update() {
// Move the pinball
xPosition += xSpeed;
yPosition += ySpeed;
transform.position = new Vector2(xPosition, yPosition);
}
Why is this code doesn't work? I fixed the quotes, but I get an error message in the console:
Te script from here: https://medium.com/#verochan/how-to-make-a-360%C2%BA-image-viewer-with-unity3d-b1aa9f99cabb
How can I fix this?
Thank you very much in advance!
float horizontal;
float vertical;
Transform container;
void LateUpdate ()
{
//Using mouse
horizontal = Input.GetAxis(“Mouse X”);
vertical= Input.GetAxis(“Mouse Y”);
//This is made in order to avoid rotation on Z, just by typing 0 on Zcoord isn’t enough
//so the container is rotated around Y and the camera around X separately
container.Rotate(new Vector3(0, horizontal*(-1), 0f)*Time.deltaTime*turnSpeedMouse);
transform.Rotate(new Vector3(vertical, 0, 0)*Time.deltaTime*turnSpeedMouse);
}
The tutorial forgot to declare the turnSpeedMouse variable and this is very likely a float.
public float turnSpeedMouse = 50f;
The whole code should look like this:
public float turnSpeedMouse = 50f;
float horizontal;
float vertical;
Transform container;
void LateUpdate ()
{
//Using mouse
horizontal = Input.GetAxis(“Mouse X”);
vertical= Input.GetAxis(“Mouse Y”);
//This is made in order to avoid rotation on Z, just by typing 0 on Zcoord isn’t enough
//so the container is rotated around Y and the camera around X separately
container.Rotate(new Vector3(0, horizontal*(-1), 0f)*Time.deltaTime*turnSpeedMouse);
transform.Rotate(new Vector3(vertical, 0, 0)*Time.deltaTime*turnSpeedMouse);
}
I looking for the programming logic behind the following scenario: I'm trying to change the color of sprites from Blue -> Red as they are get more and more far away from a particular point on space.
So, as the sprite as at a further distance from a particular point in screen, the color of their SprriteRenderer should change accordingly.
This is what I've done right now:
if (distanceBetweemCenterAndSprites > 10.0F)
{
sprites[pos]
.GetComponentInChildren<SpriteRenderer>()
.color = new Color(1.0F, 0.0F, 0.0F);
}
The code is simply calculates the distance between the centre (the point) and the sprites. If Distance > 10.0F, the color of all the sprites become red. What I want is a progressive change in color (from Blue -> Red) but I can't seem to find a logic to do so.
public class ColorShifter : MonoBehaviour
{
public float MinDistance = 1f;
public float MaxDistance = 10f;
public Transform Target;
protected SpriteRenderer SpriteRenderer;
protected void Awake()
{
SpriteRenderer = GetComponent<SpriteRenderer>();
}
protected void Update()
{
var distance = Vector3.Distance(transform.position, Target.transform.position);
var ratio = Mathf.Clamp01((distance - MinDistance) / (MaxDistance - MinDistance));
var inverseRatio = 1f - ratio;
SpriteRenderer.color = new Color(ratio * ratio, 0f, inverseRatio * inverseRatio);
}
}
Assign this script to a sprite and don't forget to set Target.
This is a basic color interpolation. This topic can get really tough depending on how deep you want to dive. Search for color interpolation techniques if you think it won't be enough. But I hope this code gives some idea.
My app has a custom panel used to display the XNA screen within a WinForm. I've currently displayed a test model with no problem and now working on camera movement. My camera is a Free Camera (not bound to look at any specific target), but I've been having trouble getting the mouse to update the yaw and pitch of the camera on its own axis. I thought maybe something was wrong with my update method, but that wasn't the case, because the camera updates moving forward and backwards using KeyboardState. But I have no idea as to why the MouseState isn't working.
FreeCamera.cs
using XNAButtonState = Microsoft.Xna.Framework.Input.ButtonState;
....
MouseState pastMouseState;
private float rotationSpeed_ = 1f / 60f;
private float yaw_, pitch_;
...
private void updateMatrix()
{
Matrix rotationMatrix = Matrix.CreateRotationX(pitch_) *
Matrix.CreateRotationY(yaw_);
Vector3 forward = new Vector3(0, 0, 1);
forward = Vector3.Transform(forward, rotationMatrix);
viewMatrix_ = Matrix.CreateLookAt(Position, Position + forward, Up);
projectionMatrix_ = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4, 16.0f / 9.0f, 0.1f, 100000.0f);
}
private void cameraInput()
{
KeyboardState keyboardState = Keyboard.GetState(); <-- updates
currentMouseState = Mouse.GetState(); <-- not updating
if (currentMouse.LeftButton == XNAButtonState.Pressed)
pitch_ -= rotationSpeed_;
if (keyboardState.IsKeyDown(Keys.W))
move(1);
if (keyboardState.IsKeyDown(Keys.S))
move(-1);
pastMouseState = currentMouseState;
}
public void update()
{
cameraInput();
updateMatrix();
In order to use XNA's mouse API (rather than WinForm events) you must set Mouse.WindowHandle appropriately (MSDN).
If you are using the official samples, then putting this in your MainForm's constructor will do the trick:
Mouse.WindowHandle = this.Handle;
(Of course using Microsoft.Xna.Framework.Input;)
I'm attempting my first 2D updown scroller really, and i can't seem to get the background size to fully work. for whatever reason it stays small and it is of no help.
I think in the beginning of the parallax scrolling class i was told to initialize variables and I know which one I have to change.
public Texture2D picture;
public Vector2 position = Vector2.Zero;
public Vector2 offset = Vector2.Zero;
public float depth = 0.0f;
public float moveRate = 0.0f;
public Vector2 pictureSize = Vector2.Zero;
public Color color = Color.White;
I believe i must change pictureSize in order to make it bigger as the current sizing i get this!
The final question i have is, How can I remove the whitespace from the ship image?
Thanks!
If any more code is needed please tell me, the reason I did not put it there, is due to the length.
EDIT*:
Here is the code for the rendering of the actual ship
public void Render(SpriteBatch batch)
{
batch.Begin();
batch.Draw(shipSprite, position, null, Color.White, 0.0f, spriteOrigin, 1.0f,
SpriteEffects.None, 0.0f);
batch.End();
}
The whitespace seems to be provided by XNA as I've cleared it several times using photoshop.
Because i can't directly upload to the site I have put it here: http://www.justbeamit.com/nk95n
And for the background rendering its
public void Draw()
{
layerList.Sort(CompareDepth);
batch.Begin();
for (int i = 0; i < layerList.Count; i++)
{
if (!moveLeftRight)
{
if (layerList[i].position.Y < windowSize.Y)
{
batch.Draw(layerList[i].picture, new Vector2(0.0f,
layerList[i].position.Y), layerList[i].color);
}
if (layerList[i].position.Y > 0.0f)
batch.Draw(layerList[i].picture, new Vector2(0.0f,
layerList[i].position.Y - layerList[i].pictureSize.Y),
layerList[i].color);
else
batch.Draw(layerList[i].picture, new Vector2(0.0f,
layerList[i].position.Y + layerList[i].pictureSize.Y),
layerList[i].color);
}
else
{
if (layerList[i].position.X < windowSize.X)
{
batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X,
0.0f), layerList[i].color);
}
if (layerList[i].position.X > 0.0f)
batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X -
layerList[i].pictureSize.X, 0.0f), layerList[i].color);
else
batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X +
layerList[i].pictureSize.X, 0.0f), layerList[i].color);
}
}
batch.End();
}
^
This is not my code but rather from a book I'm reading on how to do XNA(first attempt at C# to :/)
Without all the relevant code and details it is a bit hard to tell the problem, But I assume your scrolling code has a spriteBatch.Draw() call simlar to this:
spriteBatch.Draw(Texture, Destination, Source, Color.White);
TheDestination is probably equal too new Rectangle(0,0,pictureSize.X,pictureSize.Y), I'm assume with no code shown that the pictureSize is the size of the picture (No really!), with the spritebatch Destination parameter, you can "stretch" your image to your viewport.
With this your PictureSize should be the size of your viewport
pictureSize = new Vector2(graphics.PreferredBackBufferWidth,graphics.PreferredBackBufferHeight);
You can also use the Scale parameter of the SpiteBatch to enlarge it (However it will get blurred) or just create a bigger image for your needs.
Now for the spaceship having a white background, is XNA doing this or is the image white? I assume the latter, you will need a photo editing tool such as GIMP to remove the white background of the image.
How can I remove the whitespace from the ship image?
You could try making white transparent, using the Bitmap.MakeTransparent method. Something like this might work:
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
Bitmap test = new Bitmap("C:\Myimage.jpg");
test.MakeTransparent(Color.White);