Can you set pitch on audioclips?
I'm using
[code]AudioSource.PlayClipAtPoint(openSound, gameObject.transform.position);[/code]
to play a sound at the moment, but it would be nice if I didn't have to make 10 different pitched 'openSound's and randomly play them, but rather just choose a random pitch. I know that SFML was capable of doing this easily.
[QUOTE=war_man333;47067316]I'm getting 'load can only be loaded from the main thread'
am I doing it right?
[img]http://i.imgur.com/A8aWQpi.png[/img]
not entirely sure. I got help from some guy on GDSE. Can't remember the name of this concept.[/QUOTE]
You can't do this:
[code]
public Object whatever = Resources.Load( whatever );
[/code]
^ You're declaring the field, and initializing it to Resources.Load. Yeah, you can't do that. Instead, assign them in Awake or Start:
[code]
public Object whatever;
void Awake()
{
whatever = Resources.Load( whatever );
}
[/code]
[QUOTE=war_man333;47067727]Can you set pitch on audioclips?
I'm using
[code]AudioSource.PlayClipAtPoint(openSound, gameObject.transform.position);[/code]
to play a sound at the moment, but it would be nice if I didn't have to make 10 different pitched 'openSound's and randomly play them, but rather just choose a random pitch. I know that SFML was capable of doing this easily.[/QUOTE]
Yes, you can. Look it up in documentation.
[QUOTE=Fourier;47067403](dunno just a guess, but if you are on main thread, don't do it on coroutine or something like that, just on Awake() or Start())[/QUOTE]
From what i heard, it's best not to use multithreading in unity anyway. Read most/all of the unity API is not threadsafe.
[QUOTE=Arxae;47067928]From what i heard, it's best not to use multithreading in unity anyway. Read most/all of the unity API is not threadsafe.[/QUOTE]
It's ok, you just need to commit all your changes/computation in main thread, which means you can do all heavy computations multithreaded with no problem then every Update/FixedUpdate you commit changes. Which means lots of work and knowing, what the heck is going on.
There even exist some Multi-threaded coroutines in Unity asset store and you have special functions to commit changes (probably some special messenger with lambdas-anon functions)
Hey, is the the correct way to do a player controller?
[code]
using UnityEngine;
using System;
using System.Collections;
[AddComponentMenu("Player/PlayerMovement")]
public class PlayerMovement : MonoBehaviour
{
[HideInInspector]
public Player LocalPlayer;
[HideInInspector]
public States playerState;
private Movement forward;
private Movement backward;
private Movement left;
private Movement right;
private Movement jump;
private Movement sprint;
private Movement duck;
public float playerSpeed = 10f;
public float playerMaxspeed = 10f;
public float playerJumpheight = 200f;
public float playerGravity = 5f;
public float playerTerminalVelocity = 200f;
public float playerMomentumDivider = 2f;
public float playerJumpGravity = 10f;
private void Awake()
{
this.LocalPlayer = base.GetComponent<Player>();
this.playerState = new States( this.LocalPlayer );
this.left = new Movement("left", this.LocalPlayer);
this.right = new Movement("right", this.LocalPlayer);
this.forward = new Movement("forward", this.LocalPlayer);
this.backward = new Movement("backward", this.LocalPlayer);
this.jump = new Movement("jump", this.LocalPlayer, () =>
{
if (this.LocalPlayer.PlayerMovement.playerState.playerOnGround)
{
this.LocalPlayer.PlayerMovement.playerState.playerOnGround = false;
this.LocalPlayer.rigidbody.velocity += (Vector3)Vector3.up * this.playerJumpheight;
}
});
this.sprint = new Movement("sprint", this.LocalPlayer, () =>
{
this.LocalPlayer.PlayerMovement.playerMaxspeed = 10f;
});
this.duck = new Movement("duck", this.LocalPlayer, () =>
{
this.LocalPlayer.PlayerMovement.playerMaxspeed = 3f;
});
}
private void Update()
{
this.checkMovement();
this.doGravity();
this.momentumCap();
}
private void checkMovement()
{
this.left.doMovement(-base.transform.right);
this.right.doMovement(base.transform.right);
this.forward.doMovement(base.transform.forward);
this.backward.doMovement(-base.transform.forward);
this.jump.doMovement();
}
public void currentAllowedSpeed()
{
if (this.sprint.isDown())
{
this.sprint.onKey();
return;
}
else if (this.duck.isDown())
{
this.duck.onKey();
return;
}
this.playerMaxspeed = 6f;
}
private void doGravity()
{
RaycastHit playerRaycast;
if (!this.playerState.playerOnGround)
{
if (Physics.Raycast(base.rigidbody.transform.position, -Vector3.up, out playerRaycast, 2000.0F))
{
if (Mathf.Round(playerRaycast.distance) > 5f)
{
base.rigidbody.AddForce(Vector3.ClampMagnitude(Vector3.down * this.playerGravity * playerRaycast.distance / 5f, this.playerTerminalVelocity));
}
}
}
}
private void momentumCap()
{
if (this.playerState.playerOnGround)
{
base.rigidbody.velocity = Vector3.Lerp(base.rigidbody.velocity, Vector3.zero, Time.deltaTime * this.playerMomentumDivider);
}
else if(!this.playerState.playerOnGround)
{
base.rigidbody.velocity = Vector3.Lerp(base.rigidbody.velocity, Vector3.zero, Time.deltaTime / this.playerJumpGravity);
}
}
}
public class Movement
{
private Player LocalPlayer;
private Action MovementFunction;
private string MovementIdentifier = "left";
public Movement(string _identifier, Player _localplayer, Action _function = null)
{
if (_identifier != null)
{
this.MovementIdentifier = _identifier;
this.LocalPlayer = _localplayer;
if (_function != null)
{
this.MovementFunction = _function;
}
}
}
public bool isDown()
{
if (Input.GetButton(MovementIdentifier))
{
return true;
}
return false;
}
public bool isValid()
{
if (this.LocalPlayer.PlayerMovement.playerState.playerOnGround)
{
return true;
}
return false;
}
public void doMovement(Vector3 MovementDirection = new Vector3())
{
if (this.isDown())
{
this.LocalPlayer.PlayerMovement.playerState.onGround();
this.LocalPlayer.PlayerMovement.currentAllowedSpeed();
if (isValid())
{
if (this.MovementFunction != null)
{
MovementFunction();
}
else
{
this.LocalPlayer.rigidbody.velocity += MovementDirection * this.LocalPlayer.PlayerMovement.playerSpeed;
}
this.LocalPlayer.rigidbody.velocity = Vector3.ClampMagnitude(this.LocalPlayer.rigidbody.velocity, this.LocalPlayer.PlayerMovement.playerMaxspeed);
}
}
}
public void onKey()
{
if (this.isDown())
{
if (this.MovementFunction != null)
{
MovementFunction();
}
}
}
}
public class States
{
private IComparer rayComparer;
public Player LocalPlayer;
public bool playerOnGround = false;
public States(Player _localplayer)
{
this.LocalPlayer = _localplayer;
this.rayComparer = new RayHitComparer();
}
public void onGround()
{
if ((this.LocalPlayer.rigidbody.velocity.y <= 2.5) && this.LocalPlayer.rigidbody.useGravity)
{
RaycastHit[] _raycast = Physics.RaycastAll(new Ray(this.LocalPlayer.rigidbody.transform.position + this.LocalPlayer.PlayerCollider.center, -this.LocalPlayer.transform.up), this.LocalPlayer.PlayerCollider.height * 0.7f);
Array.Sort(_raycast, this.rayComparer);
for (int i = 0; i < _raycast.Length; i++)
{
if (_raycast[i].collider != null)
{
playerOnGround = true;
return;
}
}
playerOnGround = false;
return;
}
}
private class RayHitComparer : IComparer
{
public int Compare(object x, object y)
{
RaycastHit hit = (RaycastHit)x; RaycastHit _hit = (RaycastHit)y;
return hit.distance.CompareTo(_hit.distance);
}
}
}
[/code]
I don't know about you guys and saving inventories ect... But I've taking advantage of reflection with its ability to grab a container such as a 'InventoryList' and then getting each 'Item' inside that list and being able to scrub through every variable in that item.
Now to make my life easier, I created a custom attribute called 'Savable' that I just have to tag each variable I want to be saved in the 'Item' class, so this method allows me to save and load variables inside each 'Item' so I can repopulate every 'Item' on load with its variable.
On top of this, the system is scalable, as all I have to do if I wish to save new variables is tag them.
tldr;
I use reflection to get defined variable names so I can save them as strings.
I made a game manager object that just does that. Manages the game :p It makes a separate object that handles the UI so i can just do GameManager.Get().UiManager.... to set ui stuff.
The question i have with this is the following.
I have setup my ui parts (the new default ui that comes with unity). And i have to hook up the UiManager to the canvas object.
Gamemanager just adds the uimanager as a component at the start.
But what would be the best way to make sure the UiManager can find the ui elements? At the moment i just have a popupbox (background + text) for when you can use interact with something.
The GUI is tagged with a tag (GUI) and i get that using GameObject.FindGameObjectWithTag. Then i loop over the children of the gui until i find the popup. Then loop over the children of the popup until i have the text. Then assign those 2 to a variable. Something like this: [url]https://gist.github.com/arxae/bea79f4f422b0982ece7[/url]
Is this a good enough way to do it? Since it only does this once per scene. Or is there a better way? I just get the feeling this can be done in a MUCH more optimized fashion
is there not an equivalent for GUI.DrawTexture which draws sprites? I see there is a GUI.DrawTextureWithTexCoords but then I have to figure out where in the spritesheet that specific sprite is, and that doesn't work.
Run away from GUI.DrawTexture, it's old and it's crappy.
[QUOTE=Fourier;47075171]Run away from GUI.DrawTexture, it's old and it's crappy.[/QUOTE]
what do you recommend?
i found this horrible code which works
[code]
Sprite s = slots[i].Sprite;
Texture t = s.texture;
Rect tr = s.textureRect;
Rect r = new Rect(tr.x / t.width, tr.y / t.height, tr.width / t.width, tr.height / t.height);
GUI.DrawTextureWithTexCoords(slotRect, t, r);[/code]
[editline]4th February 2015[/editline]
I don't understand this:
[code]Sprite sprite = Resources.Load<Sprite>("Items/Weapons/BasicWeapons");[/code]
this works and sets the sprite to the first sprite in the BasicWeapons texture.
[code]Sprite sprite = Resources.Load<Sprite>("Items/Weapons/BasicWeapons_0");[/code]
this just doesn't work. what if I wanted the second sprite in the texture?
[QUOTE=war_man333;47075175]what do you recommend?
i found this horrible code which works
[code]
Sprite s = slots[i].Sprite;
Texture t = s.texture;
Rect tr = s.textureRect;
Rect r = new Rect(tr.x / t.width, tr.y / t.height, tr.width / t.width, tr.height / t.height);
GUI.DrawTextureWithTexCoords(slotRect, t, r);[/code]
[editline]4th February 2015[/editline]
I don't understand this:
[code]Sprite sprite = Resources.Load<Sprite>("Items/Weapons/BasicWeapons");[/code]
this works and sets the sprite to the first sprite in the BasicWeapons texture.
[code]Sprite sprite = Resources.Load<Sprite>("Items/Weapons/BasicWeapons_0");[/code]
this just doesn't work. what if I wanted the second sprite in the texture?[/QUOTE]
Sorry if I missed this, but what's wrong with just using a SpriteRenderer?
[QUOTE=BackwardSpy;47075277]Sorry if I missed this, but what's wrong with just using a SpriteRenderer?[/QUOTE]
Is it relevant? I'm using the sprites as part of an inventory.
[QUOTE=war_man333;47075762]Is it relevant? I'm using the sprites as part of an inventory.[/QUOTE]
You could probably do something with adding each one as a child object of the inventory object, each one would have a SpriteRenderer component on it and be positioned properly.
[QUOTE=war_man333;47075175]what do you recommend?
i found this horrible code which works
[code]
Sprite s = slots[i].Sprite;
Texture t = s.texture;
Rect tr = s.textureRect;
Rect r = new Rect(tr.x / t.width, tr.y / t.height, tr.width / t.width, tr.height / t.height);
GUI.DrawTextureWithTexCoords(slotRect, t, r);[/code]
[/quote]
If you use this approach you should cache the information. I've done something similar and I use a struct to hold the data required for drawing, but the data is only calculated once.
[QUOTE=war_man333;47075175]
[editline]4th February 2015[/editline]
I don't understand this:
[code]Sprite sprite = Resources.Load<Sprite>("Items/Weapons/BasicWeapons");[/code]
this works and sets the sprite to the first sprite in the BasicWeapons texture.
[code]Sprite sprite = Resources.Load<Sprite>("Items/Weapons/BasicWeapons_0");[/code]
this just doesn't work. what if I wanted the second sprite in the texture?[/QUOTE]
Maybe [URL="http://forum.unity3d.com/threads/mini-tutorial-on-changing-sprite-on-runtime.212619/#post-1429976"]this[/URL] could help you.
I re-modeled the ugly placeholder switches to much nicer looking placeholder switches for my cockpit panels. I also completely re-factored and re-wrote the way that the switches work entirely. I was planning on doing it animation based, but that would have created significantly more overheard than it was worth so instead I'm just writing generic switch classes that can be set to do generic things.
Thanks Rizzler for the suggestion, this is much better. All the special things I was planning to do with animations I realized would be super easy to emulate with simple translations and rotations.
Right now I've got an NPositionSwitch and MomentaryPressSwitch. You tell them what axis to rotate/translate on, by how much, and in the case of NPosition what the rotation at each position will be.
Webm related has 2 NPositions (a two position switch and a 4 position knob) and 1 momentary press button.
[vid]http://webmup.com/B87jA/vid.webm[/vid]
So I thought since my game is going to be using a lot of items, to save time I decided to try out adding a dynamic derived base class for my item system.
This is the ItemBase class, its highly customizable and can be heavily well rewritten.
[code]
public class ItemBase
{
public Action onLoad;
public Action onUse;
public Action onDelete;
public Action onDrop;
public ItemBase()
{
onLoad = ()=>
{
Debug.Log("Testing dynamic bases");
};
}
public Action loadFunction
{
get
{
return this.onLoad;
}
set
{
this.onLoad = value;
}
}
public Action setOnUse
{
get
{
return this.onUse;
}
set
{
this.onUse = value;
}
}
public Action setOnDelete
{
get
{
return this.onDelete;
}
set
{
this.onDelete = value;
}
}
public Action setOnDrop
{
get
{
return this.onDrop;
}
set
{
this.onDrop = value;
}
}
public Action onEquip
{
}
public Action onUnequip
{
}
public Action onCraft
{
}
public Action onClick
{
}
public Action onRemove
{
}
}
[/code]
You first create your new ItemBase class with your own custom needs, so for instance:-
[code]
ItemBase my_base = new ItemBase();
my_base.onUse = () =>
{
Debug.Log("Test");
};
[/code]
And the to simply pass it in the item you want:-
[code]
Item my_item_with_advanced_base = new Item(my_base);
[/code]
It means now that for instance if I was coding weapons, I could use the same required functions and replicated functions and save a ton of time.
The items designed that as soon as its constructed, it'll look for the onLoad hook on its item base, if its not null.
[code]
public Item(ItemBase DynamicBase = null)
{
if (DynamicBase != null)
{
DynamicBase.onLoad();
}
}
[/code]
You can still also set your own 'custom' onUse and onDelete functions via the item too.
Any thoughts on this?
[QUOTE=Fourier;47075171]Run away from GUI.DrawTexture, it's old and it's crappy.[/QUOTE]
Doesn't Unity has GUI tools? As in WYSIWYG tools?
[QUOTE=Fourier;47079207]Doesn't Unity has GUI tools? As in WYSIWYG tools?[/QUOTE]
do you mean like the drag-and-drop WPF in Visual Studio? I second this!
[QUOTE=war_man333;47079208]do you mean like the drag-and-drop WPF in Visual Studio? I second this![/QUOTE]
[url]http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui[/url]
I want to dynamically create a prefab through scripting:
[code]
weaponGameObject = new GameObject();
weaponGameObject.AddComponent<SpriteRenderer>();
weaponGameObject.AddComponent<Rigidbody2D>();
weaponGameObject.AddComponent<BoxCollider2D>();
Weapon_Swing wpn = Instantiate(weaponGameObject, pos, rotation) as Weapon_Swing;
[/code]
this instantiates it twice, because it creates a new game object at 0,0 and the Weapon_Swing instance.
How can I do this correctly?
[code]
weaponGameObject = new GameObject();
[/code]
This creates a new game object in the scene.
[code]
Weapon_Swing wpn = Instantiate(weaponGameObject, pos, rotation) as Weapon_Swing;
[/code]
And then you call Instantiate, which creates a clone of the game object. Therefore, now you've got two of them.
I only want the bottom one ofcourse.
But I have to initialize the GameObject as a new one.
so, how would I go about this?
Well, think about it for a moment. You [B]already have a game object in the scene[/B], initialized with all of the components you need. There's no point in calling Instantiate at that point, is there?
[QUOTE=KillaMaaki;47088872]Well, think about it for a moment. You [B]already have a game object in the scene[/B], initialized with all of the components you need. There's no point in calling Instantiate at that point, is there?[/QUOTE]
oh right.
what exactly is the difference between making a new GameObject and using instantiate then?
you get to choose its position and rotation through instantiate?
[QUOTE=war_man333;47088885]oh right.
what exactly is the difference between making a new GameObject and using instantiate then?
you get to choose its position and rotation through instantiate?[/QUOTE]
Instantiate, actually, is a lot more versatile than just cloning game objects. Actually, it clones [I]any asset[/I] you pass it and returns the clone. That is, you can clone materials, audio clips, textures, etc. It's most useful, however, when you have a [I]prefab[/I] - a game object stored in your project rather than in your scene. In this case, you might have something like:
[code]
public GameObject SomePrefab;
void DoSomething()
{
Instantiate( SomePrefab );
}
[/code]
In the inspector, you can then assign a game object prefab from your project to the "Some Prefab" slot, and that will be spawned in the scene when you call the DoSomething method.
I don't work on this everyday so I'm making like no progress.
[img]http://puu.sh/fzxqV/9d54fb276d.gif[/img]
I spent a while writing a shield shader today. It's not done yet, but I'm reasonably happy with what I have so far, especially since I've never written a shader in Unity before.
[url=http://backwardspy.github.io/flite.html]Click to fire a shot of randomly chosen power against the shield.[/url]
[editline]mmhmm[/editline]
Just updated it so backface culling no longer applies to the shield. This helps to give it more of a sense of volume, in my opinion.
I'm getting a heavy Nexus vibe from that everything. Looks pretty cool. I would probably make the idle shield much more transparent if that's something that's on all the time.
[QUOTE=Why485;47091520]I'm getting a heavy Nexus vibe from that everything. Looks pretty cool. I would probably make the idle shield much more transparent if that's something that's on all the time.[/QUOTE]
I agree, I'll probably end up making it a lot less visible when it has fully dissipated excess energy.
[editline]7th February 2015[/editline]
Whoops, I just realised I totally ruined the rim shading when I turned off the culling. Fixed version is now uploaded, though you might have to Ctrl-F5 to see it.
Sorry, you need to Log In to post a reply to this thread.