I am having problems with making a texture select screen. What I want to do is when you press start on the main menu you go to this screen. This screen will allow the player to select a texture for the model of choice. Allow for rotation of the model to see the newly applied texture. Then the player can press save texture and start game or go back to the main menu. This rough sketch might explain better of what I am wanting to do.
My problem is, I really don’t know where to start. I do know how to go from one scene to another, but not change texture or even the model and save it and allow the player to use it throughout the game. I also want to make the some textures unlockable.
I would ask for this much help, but I cannot seem to find a good tutorial on this. The ones I did find are usually unfinished. I want to post this to provide this for those in the future having the same problems, so if some of you wouldn’t mind helping out.
All very doable. What I’d recommend (as usual) is to take it one step at a time. What you’ve described has about 5-10 different elements involved (e.g. assigning a new texture or material to a model, picking a material from a selection grid, rotating the model in response to a slider control, etc., saving the assigned material, etc.).
So, pick one of those things, and then post back and let us know what you’ve decided to work on first. Then, we’ll help you with that.
(Note that there are some important software development themes at play here, such as modularity, the ‘single responsibility principle’, and unit testing.)
function OnGUI ()
{
// Make a background box
GUI.BeginGroup (Rect(Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));
// Make the first button. If it is pressed, Application.Loadlevel (2) will be executed
if (GUI.Button (Rect (20,40,80,20), "Start Game")) {
Application.LoadLevel (2);
}
// Make the second button.
if (GUI.Button (Rect (20,70,80,20), "Main Menu")) {
Application.LoadLevel (1);
}
GUI.EndGroup();
}
So i would say start with moving these two to the bottom left and then start on rotating the model.
You can use ‘code’ tags (rather than ‘quote’ tags) to post your code with correct formatting. (Usually, at least. Sometimes it still doesn’t quite work, but replacing tabs with spaces usually fixes any problems, IMX.)
So do you want to rotate the model using a slider (as your sketch seems to indicate)?
If so, this is what you’ll want to do (or something like this, at least):
Create a script that implements an OnGUI() function.
In that script, add a public Transform reference variable, and then drag the game object with the model attached onto that variable.
Add a ‘slider’ variable of type float.
In OnGUI(), use a HorizontalSlider() control to update the value of this variable, specifying the range as [0, 360] (or whatever you want the range of rotation to be).
Update the object’s rotation using this variable, e.g.:
/ Draws a horizontal slider control that goes from 0 to 10.
var hSliderValue : float = 0.0;
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (25, 25, 100, 30), hSliderValue, 0.0, 10.0);
targetTransform.eulerAngles.y = sliderAngle;
}
This is already attached to the ship. But I am having problems moving and scaling the gui.
// Draws a horizontal slider control that goes from 0 to 10.
var hSliderValue : float = 0.0;
var target : GameObject;
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (800, 500, 200, 20), hSliderValue, 0.0, 10.0);
targetTransform.eulerAngles.y = sliderAngle;
}
I am not great at coding. I know I need to set a varible for the the object to be transformed, but when I give it a target it give me this.
Generally it’s best to get comfortable with the basics of scripting and working with Unity before tackling something like this, but let’s forge ahead anyway - it’ll just be a ‘learn as you go’ experience
You’ve got two errors there, the first of which is an easy fix. In this line of code:
targetTransform.eulerAngles.y = sliderAngle;
You’ve simply used the wrong variable name. You’ve correctly declared and initialized the corresponding variable, and you’re using it with your call to HorizontalSlider(). That’s the variable you want to be using in the above line. (The variable you’re using currently doesn’t exist.)
For the other variable (‘targetTransform’), you need to create that one. You already know how to add a public variable, because you did it with ‘hSliderValue’. You just need to do the same thing again, except the name should be ‘targetTransform’ and the type should be ‘Transform’. Once you’ve added this variable it should show up in the inspector, at which point you can drag the game object with the spaceship model onto it.
(Also, note that your slider range is currently [0, 10], which will make for a pretty small rotation when you move the slider. If you want the rotation to be more pronounced, you’ll probably want to increase that range.)
Ok I have done the first one before, but I am alittle dense with the last one. It still doesn’t work for me. Here is what I have done.
// Draws a horizontal slider control that goes from 0 to 10.
var hSliderValue : float = 0.0;
var target: GameObject;
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (800, 500, 200, 20), hSliderValue, 0.0, 10.0);
targetTransform = GameObject;
targetTransform.eulerAngles.y = hSliderValue;
}
Keep in mind that programming requires considerable attention to detail. In most cases, ‘close’ won’t cut it; you have to get it exactly right.
Here’s a quote from my earlier post:
Note that I told you what the name and type of the variable needed to be. However, you used a different name, and a different type. That’s why you’re getting errors (these are the kinds of details that have to be gotten right).
So, try making that fix, and see what happens after that.
Sure thing give me a few mins to get all setup and I will try to make the variable. Quick question, last night when I was going through the scripting reference I found this now I am a bit confused.
// Print the rotation around the global X Axis
print (transform.eulerAngles.x);
// Print the rotation around the global Y Axis
print (transform.eulerAngles.y);
// Print the rotation around the global Z Axis
print (transform.eulerAngles.z);
// Assign an absolute rotation using eulerAngles
var yRotation = 5.0;
function Update ()
{
yRotation += Input.GetAxis("Horizontal");
transform.eulerAngles = Vector3(10, yRotation, 0);
}
Do not set one of the eulerAngles axis seperately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. When setting them to a new value set them all at once as shown above. Unity will convert the angles to and from the rotation stored in Transform.rotation
If you’re just rotating the model on one axis, so there is no need to worry.
It’s a warning for the undesired effect of using Euler angles. If you rotate the axis for x, y, z in different orders, you end up with a different final rotations (even if they’re the same values) Which is why Euler is only for us Humans to understand, and Quaternions are used internally.
Ok thanks. That clears that up. I feel like a complete idiot here. This is like algebra for me. Everytime I think I have it, I screw up the script. I found an example for what I am doing, but I cannot implement it.
// Draws a horizontal slider control that goes from 0 to 10.
var hSliderValue : float = 0.0;
var target: GameObject;
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (800, 500, 200, 20), hSliderValue, 0.0, 10.0);
targetTransform = transform;
targetTransform.eulerAngles.y = hSliderValue;
}
I told you what the name and the type of the public variable needed to be. But you’re still using both a different name, and a different type. That (and the extra code you’ve added to work around it) is why it isn’t working.
So, try making that change, and see if it works then. If it doesn’t, post your code (note that there’s an extra line of code that you added that you’ll have to remove in order for it to work).
If there’s anything you don’t understand about what I’m suggesting, post back and explain what part is confusing you.
Ok, I guess what is confusing is what you are trying to tell me. I am guessing this line is the one you are saying to remove.
I have some of the basics to c# and java, but it seems I still have more to go. I am really bad at coding, but I have no problem doing models, texture and animations.
Do you maybe have a reference that I might look at? I realize you get people that just want a free script, but that is not what I am going for. I don’t have much time right now(getting ready to go plow snow), but I will try to keep the code running through my mind. My problem right now is I have to see a pattern before I get it, once I see it I can go until I see another pattern that I don’t get.
Sure, I understand that you’re not just asking for a script, which is good. Let me try one more time and see if I can lead you to the answer:
This is the line of code that needs to be changed:
var target: GameObject;
What I said earlier was:
When you have a variable declaration of the form:
var target: GameObject;
Here’s what the different parts are:
var <the name goes here>: <the type goes here>;
Currently, you have ‘target’ as the name and ‘GameObject’ as the type. I’m telling you to use ‘targetTransform’ for the name and ‘Transform’ for the type. From that, you should be able to figure out how to fix the line of code in question, but if not, post back.