Tag Info

Hot answers tagged

4

Your code is very scrambled. You have all your time relative variables inside the shooting loop. and the instantiation code too. Why? Why not simply make it like this : void ShootBullet() { Rigidbody bPrefab = Instantiate (bulletPrefab, transform.Find ("Bullet").position, Quaternion.identity) as Rigidbody; bPrefab.AddForce (transform.up * ...


3

When importing sprites into Unity, you don't work with pixels anymore, you work with units. By default, all sprites have : 100pixel per unit. 100px = 1 unit. You can change this, go to your sprite in the assets, click it, and on the editor you'll see the "Pixels per Unit", which should indeed be 100. If you change it to 1, then every 1 pixel will equal 1 ...


2

Box cast would probably be more performant since it's only checking 4 vertices but it'll definitely be more accurate, at least depending on what you want. Good idea! Example use: RaycastHit2D raycast = Physics2D.BoxCast ( //Starting point of box Vector2d.zero, //Width of box, 2f, //Angle of box (in radians), 0f, //Height of ...


2

In the frame that bulletCounter become 3, Time.time will be less than gunGlobalCooldown because this line of code: gunLocalCooldown = Time.time + 0.12f; gunGlobalCooldown = gunLocalCooldown + 0.8f; so the boolean canShoot will be set to false, and this if if (Input.GetKey (KeyCode.Space) && Time.time >= gunLocalCooldown && ...


2

How exactly this is best handled depends on the exact use case. The two main use cases are: 1. You want to access a shared background object that persists throughout the game. 2. You want to access a specific object within the scene. For situation 1, you could use one of the code patterns like Service Locator or singleton (justin's suggestion is basically a ...


2

That's not using GetComponent() on the class GameObject, it's using GetComponent() on the result of GameObject.Find() GameObject.Find() is a static function, but it returns a specific object. Note that Code 2 is also using GameObject.Find() but then you store the result object in a variable. Code 1 uses the exact same functions but doesn't store the result ...


2

Without a doubt, an executable per map would be the best option because servers run collisions detection. Collision detection increases in complexity by O(N) so a single instance with 500 objects will be much slower than 5 instances with 100 objects each. There is a small overhead for each individual executable instance but compared to physics and other ...


2

If you want to be hardcore about it... public static bool StringComparison (string s1, string s2) { if (s1.Length != s2.Length) return false; for (int i = 0; i < s1.Length; i++) { if (s1[i] != s2[i]) { Debug.Log ("The " + i.ToString() + "th character is different."); return false; { } return ...


2

Now that I see your problem, this has absolutely nothing in relation to Prefabs, your error resides in addressing the object you're looking for: GameObject.Find("Canvas/PrefabA").GetComponent<GameObject>(); Try doing this : GameObject prefab = GameObject.Find("Canvas").transform.Find("PrefabA").gameObject; or this : GameObject prefab = ...


1

Provided all the events have the same signature: delegate return_type Event(parameter_list); Event[] events = { new Event(Event1), new Event(Event2), //..... new Event(EventN) }; //in your caller method events[random_int](parameters); Delegates are the fastest and easiest method to accomplish what ...


1

Trail or line renderer: http://docs.unity3d.com/Manual/class-TrailRenderer.html. Just have a darker texture to overlay the snow and continuously set a new position for the trail renderer.


1

PlayerPrefs are not designed for saving scores. They are easily modifiable by the end user in a text file. Instead, you should serialize settings, replays, etc. that players have no motivation to hack. Knowing that, this is how you save to PlayerPrefs: int Birthday; void Start () { Birthday = 172589; PlayerPrefs.SetInt ("My Birthday", Birthday); } ...


1

If you have trouble getting a good blend in between your animation states you could break it up using Invoke() within your OnTriggerEnter(). I don't normally like to do this but it will most likely solve your state issue and give it time enough to show the idle before your jump. Invoke("PillarAddForceMethod", .6f); void PillarAddForceMethod() { //ToDo ...


1

Select the Light object Go to Inspector Light And change rendered mode "Auto" to "important"


1

Yes, unity does make the calculations itself. Test case: Game Window -> select different resolutions and run the following code: private void Update() { if (Input.GetMouseButtonDown(0)) { firstTouchPos = Input.mousePosition; } else if (Input.GetMouseButtonUp(0)) { endTouchPos = ...


1

Your code doesn't seem to have any "rotation" for me. Plus you're addressing r.eulerAngles, which is why your rotation is failing. Quote from Transform.eulerAngles : Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. And for the next part, what are you ...


1

If you need good image quality you can use Format: Truecolor or ARGB 32 with an high Max Size and avoid compression: You can try playing with Filter Mode and Aniso Level too in the advanced settings:


1

In order to get around this programmatically you can do transform.SetParent(newParent, false); What this does allows you to keep the local values the same instead of leaving the global values the same, idk if this will give you your intended affect since it will move the object but it is an option.


1

Your error here is that you're addressing a non-static variable, with a static method. I recommend you take a time-out of Unity, and get to know better non-static vs static variables, and non-static vs static methods. I will recapitulate in a bunch of words and explain it to you roughly though. So the non-static variable here is waypointObj. As you can see ...


1

Let me first question you on a few things. exampleTwo = gameObject.AddComponent(); Do you really need to ADD a script to your object during runtime? Why? I mean I can understand adding a collider to an object that you didn't want to collide at some point, adding lots of things are explainable. But why Add a script? Why not just keep it attached to the ...


1

public class Example2:MonoBehaviour{ public static Example2 Instance; void Awake() { Instance = this; } //Now you can access the public methods or variable of Example2 from any class by Example2.Instance.YourMethodOrVariable; } //--------------------------------------------------------------------------------- //But if you have a Boolean ...


1

Click on the Title in your hierarchy. In the Rect transform, you can see that square in the editor. Click it, you can then set the position of the Text (Top, in you case.) After that, press Alt, and choose where to place your Anchor. (Top again). If you still don't like it, you can choose to move the Anchor manually in the Scene. Ultimately, keep the Anchor ...


1

Coroutines are the best performance-wise. I.e. void Start () { //Starting the coroutine StartCoroutine (Shooter()); } IEnumerator Shooter () { if (Input.GetMouseButton(0)) { //Shoots the bullet Rigidbody bullet = Instantiate (Bullet).GetComponent<Rigidbody>; bullet.AddForce (Camera.main.transform.forward * 10, ...


1

So this doesn't produce exactly the behavior you asked for but I think it might be what you're looking for anyway. This code (syntax not exact as I don't use c#) would allow someone to sustain a fire rate of one bullet per 0.5 seconds indefinitely, but fire bursts of up to 3 bullets at a rate of one bullet per 0.12 seconds. gunBurst=0.12f gunSustained=0.5f ...



Only top voted, non community-wiki answers of a minimum length are eligible