New answers tagged

0

One way to look at it is that you're possibly putting too much into your game objects. There's no reason that code that actually hooks up the HUD to your in-world game needs to be in a component/system that lives in some particular space. That code will perhaps be better off living in a central manager or global script that has access to all spaces and all ...


0

You want a directed (possibly cyclic) graph. You'd model the nodes as objects, and all the outgoing arrows in a graph's node are modeled as separate objects, too. The node has a list of outgoing arrows, and each "arrow" object has a text to display and a reference to the destination. Not sure, but I think in C# objects are always referenced, so you just ...


2

There may be two reasons why it's not working. You have not attached the script to any object which is in the hierarchy window. You have not assigned a prefab in the stone_3 variable field.


0

I often like to give myself a little convenience method: Vector3 CameraRelativeFlatten(Vector3 input, Vector3 localUp) { // If this script is on your camera object, you can use this.transform instead. Transform cam = Camera.main.transform; // The first part creates a rotation looking into the ground, // with "up" matching the input ...


2

Easiest way to do it by adding an Event Trigger component to the knob for Pointer Enter or Pointer Down and call a public method through it.


0

So the problem you're having is that the Y movement is also used when applying the force. Easiest solution is to simply set the Y movement to 0 when adding the force: rb.AddForce((new Vector3( CameraController.finalMovement.X, 0, CameraController.finalMovement.Z).Normalize() * speed);


0

I'm not sure if I understand the question completely but here goes: Converting the clock from 12h and 24h: The 12 hour clock is basically the 24h clock limited to 12: 12h_time.hours = 24h_time.hours % 12; To convert the 12h clock to 24h you need to know if it is morning or afternoon: 24h_time.hours = (morning?12h_time.hours:12h_time.hours+12); Now ...


0

In the end, keeping the code from the question and changing the public field of the Prefab to type GameObject (not Transform) did the trick. (I did not need to use Resources.Load("PATH");) I hope this saves someone some time


1

For what I've seen in the comments your prefab is null because there is nothing assigned at runtime. Drag n dropping stuff in the inspector of your script is ignored by the engine at scene load. Once your script is assigned as component to a gameObject in your hiearchy then public slots will appear and you should do there your drag n drop. Otherwise you ...


0

In addition to @lvictorino answer, maybe what you're looking for is in fact the AddForce method. I'm not exactly familiar on how it could work with Unity, but roughly, what you could do is // Rough pseudocode difference = touchPosition - objectPosition distance = difference.lenght direction = difference.normalize radiusForMaxForce = 50 // tweak this ...


2

First of all, all your physics related stuff should be done in FixedUpdate method and not in Update method. Update is called every frame, while FixedUpdate is called at fixed interval. When you ask Physics movement computation in FixedUpdate Unity will predict the new position in Update until it'll be able to compute the actual position in the next ...


-1

if ((lamp.activeSelf == true)&&(test == true) ) brackets missing in if condition.


0

You're not raycasting, you're linecasting: Physics.Linecast(vert, Vector3.up, out hit,planeLayer); origin | destination | hit info | layer mask The second argument here is the end of the line you want to check along, not the direction of the line. So you're checking for an intersection along the line from your vertex to the point (0, 1, 0) ...


1

The solution to this is to use a Coroutine that waits for the variable to be destroyed. This guarantees that it will be able to make the MeshCollider. Here is a piece of sample code that could be used to implement a similar solution. Vector3[] v3 IEnumerator Start(){ PolygonCollider2D polyCollider = this.gameObject.GetComponent<PolygonCollider2D>()...


0

Here is one theoretical solution you can try. Your moving object (that I'll call Dot) is moving around other objects (that I'll call Gravity Points). The Dot knows what Gravity Point it is attached to and should know the position of other Gravity Points. Also as they are defined as circles I imagine that they have a radius that you can easily know. When ...


1

I'm not sure filling sharedMesh.vertices will be enough to convert your PolygonCollider2D into a MeshCollider. From my (not long enough and too basic) tests, when you try to create a MeshCollider and only give it vertices it doesn't work. Your MeshCollider will need vertices but also triangles. After getting the polygon points you might have to compute all ...


0

The client will already have the sound ready to be played, it just needs to know when to play it. All you need to do, is ensure you are playing the sounds at the right time. In most cases the sound will play due to another action or event happening. Did the server send out a notification telling you that someone died? Play the death sound. Did you ...


1

You should have a script attached to your Pen object. Inside this script you have references (wether public or just visible in editor) where you drag the children via editor. class YourPenScript { public GameObject Tint; public GameObject Case; public GameObject Ring; public GameObject Shape; private SpriteRenderer sr_tint; private ...


0

I haven't tested it but it seems you may want to use the Berp method from here, instead of Lerp. public static float Berp(float start, float end, float value) { value = Mathf.Clamp01(value); value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) * (1f + (1.2f * (1f - value))); return ...


2

The thing is, in slither.io, the snake isn't following a spline at all. Each segment simply follows the previous segment. So create a head and a bunch of segments. The head is just a special segment with your movement and other logic in it: class SnakeHead:SnakeSegment { List<SnakeSegment> Body = new List<SnakeSegment>(); //keep the ...


0

I rearrange code here(for the jump and attack part): if (Input.GetKeyDown("space")) { if (anim.GetFloat("input_x") < 0) { anim.SetBool("is_shooting_left", true); anim.SetBool("is_shooting_right", false); } if (anim.getFloat("input_x") > 0) ...


-1

This is so simple, Just checked that loop object have your specified componentes or not if it is not null then add in the list. foreach(var go in list) { if(go.GeComponent<Component1>() != null) { compList.GeComponent<Component1>(); } }


1

Unity's InputManager isn't well suited for this task, I strongly recommend interfacing directly with the APIs that SteamVR provides for Unity games: SteamVR_Controller.Device.GetAxis is most likely going to be the easiest way to get the behavior you want: public Vector2 GetAxis(EVRButtonId buttonId = EVRButtonId.k_EButton_SteamVR_Touchpad) So to treat it ...


3

You just need to set some boolean value indicating you should pause your game when you detect any of the conditions you listed. It sounds like you're using Windows Forms; this means you can detect all of your conditions as follows: "don't have focus" Listen for the activated and deactivated events. A form must be activated to have input focus. "a menu is ...


0

You describe your problem rotating a cube, but it seems to me that you are having the camera orbit around the cube. However, the vectors you are using for the axes of rotation are in world space, and do not rotate with the camera. After you have rotated left by 90 degrees, Vector3.left is now facing directly into the camera. As such, your next swipe for a "...


0

Like dnk drone.vs.drones said, LoadLevelAdditive is likely going to help with how you've got it set up currently. You might want to look into something more like a chunk-based loading system, however. Only rendering near chunks and using appropriate LOD's seems to be more standard in the industry. I've got a feeling it's better for your performance as ...


0

LINQ is probably the best way to go, but switch your Select and Where so you don't have to call GetComponent twice var components1 = list.Select(r => r.GetComponent<Component1>()).Where(g => g!= null).ToList();


1

It's reading one character at a time, so it's not properly escaping the new line. I would suggest looking for escape characters in your Stampa() function, and if it's an escape character, read the next character in at the same time. Something like: void Stampa(string stringa, int i) { string r = stringa[i]; if (r == '\\') r += stringa[i+1]; ...


2

Well, everything is simple and complicated, and simple again at the same time. Despite the visual changes in the inspector - it doesn't mean that the data were actually changed. It looks like everything works fine, but......In my opinion it is shortcoming of the Unity For correctly work you should use few things: GUI.changed - returns true if any ...


0

ScrollRectHolder holds ScrollRect component. The content of it is ScrollContent. Add Vertical Layout Group component on ScrollContent. It's also the parent of the buttons that will be instantiated each time you need. Button is the button prefab that will be instantiated. The variable initialHeight is the initial height of ScrollContent. Have to get it from ...


0

So, I've currently found a solution using Resources folders and organizing work that way, but it's far from ideal. However, this seems to be the best possible solution. I'm not going to mark this as an accepted answer just yet, though. But, at least for anyone coming after this same question, it'll be here as a reference.


0

It's not the cleanest method, but an easy starting point would be to make a char[] array filled with all the letter, then randomize the order. Create an array of Vector3 points that can contain the letters (obviously at least 26 points). Then instantiate an object containing the letter from the next char[] value randomly to one of the Vector3 points. That ...


2

The solution by Qbix works when the list of possible functions is static. But what if the list is only known at runtime? First you need to wrap the function calls into delegates, so you can treat them as variables. A premade delegate from the .NET framework which takes no arguments and returns no arguments is Action (There are also template delegates for ...


1

You can try Application.LoadLevelAdditive Unlike LoadLevel, LoadLevelAdditive does not destroy objects in the current level. Objects from the new level are added to the current scene. This is useful for creating continuous virtual worlds, where more content is loaded in as you walk through the environment.


1

Simplest way in the world: Random random = new Random(); // it will roll from 0 (inclusive) to 2 (exclusive) so 0 and 1 int number = number.Next(0, 2); if(number == 0) func1(); else if(number == 1) func2();


-1

By definition, all variables SHOULD be private. Every function or variable is private by default in Unity, but there's no problem writing the 'private' reserved word. If you want to get or set one of them from another class, you have to create a getter/setter function, but these functions must be public, otherwise they will be useless. There is no problem ...


1

I found the solution; it turned out to be a beginner's mistake. I locked the Y-axis in the Rigidbody2D component (to prevent the gravity effect pulling my character downwards all the time). I unchecked that and set the Gravity Scale to 0 and it is working!


0

The problem is that you are moving the object too much up and down every time you check if it is already on the right spot. Therefore it jumps up and down because if(Pos.y < 2) Pos = Vector2.Lerp(Pos, new Vector2(Pos.x, Pos.y + 1.2f), 0.1f); moves it up so far that else if(Pos.y >= 2) Pos = Vector2.Lerp(Pos, new Vector2(Pos.x, Pos.y - 1.2f),...


3

Separated grid Randomly pick a box that hasn't been chosen yet, then randomly place the item within that grid space. The space between the boxes is enough so that items placed on the edge of a box doesn't intersect with an item placed on the edge of an adjacent box. This can be implemented pretty simply with a list and a bit of math. Try, and try again ...


0

Firstly, my Moving Boolean was a static variable(this was changed before I asked this question). Secondly, I failed to reference the object I was colliding with thus building on my first logic error of the static Boolean - which made all instances share the same value. Lastly, after realizing the later of the two and the fact that my code was not properly ...


4

C++ and OpenVR Solution: You can use IVRSystem::GetTrackedDeviceProperty for this exact purpose and everything you want to know about a tracked device/controller: TrackedPropertyError error; int32_t controllerRole; for (auto i = 0; i < numTrackedControllers; i++) { controllerRole = GetInt32TrackedDeviceProperty(i, ETrackedDeviceProperty::...


1

you can use Vector3.Lerp Linearly interpolates between two vectors. Interpolates between the vectors a and b by the interpolant t. The parameter t is clamped to the range [0, 1]. This is most commonly used to find a point some fraction of the way along a line between two endpoints (e.g. to move an object gradually between those points). in below script ...


2

Imagine it as a simple state machine. Keep track of your current state, and when you get an incoming state change request (the key presses), compare it to the current state to see what you should do. //At the top of your script public enum Lane {Left, Center, Right} public enum MoveDirection {Left, Right, None} Lane currentLane = Lane.Center; //In the ...


0

Setting data in the texture is an expensive operation at that resolution size. It should be avoided if you plan to do that every frame. I don't know why you want to set the data in the source image like that, but for the texture there are a couple of options: If you just want the blue square at the current mouse position: Draw the texture to the screen ...


1

I'd do this with a coroutine: // Keep track of the current coroutine so we don't stack up multiple. Coroutine translation; void Update() { if(Input.anyKeyDown) { translation = StartCoroutine( TranslateTo( /* Put the final position you want here */, 1....


0

You may use smooth translation with Vector2.Lerp(). One new variable isKeyPressed to detect key pressed which is changed when target position condition meets, until it translates towards target position with 1.2 unit in y-axis/downward with 0.1 frequency. See Lerp() Ref. Vector2 Pos; bool isKeyPressed = false; void Update() { Pos = transform.position; ...


-1

The accepted answer can be found at Animate Sprite Along a Curve path in XNA I appreciate the help people have tried to post. static float mFlightTime = 0.0f; Vector2 mVelocity = new Vector2(-12, -8); Vector2 mAcceleration = new Vector2(0f, 1f); Vector2 mStartingPosition = new Vector2(260.0f, 31.0f); Vector2 mPosition = Vector2.Zero; public void update(...


2

Parameterized equations of (2D) motion for a parabola are as follows: x = x0 + t * Vx y = y0 + t * Vy - 0.5 * g * t^2 with a sign convention of increasing X rightward, increasing Y upward. These are a simple adaptation of the kinematic equations of motion under constant acceleration.


0

You could create a Boolean eg. canCrash and make sure it is true when ever the car changes from the previous, then on collision you check if this Boolean is true also if it isn't but the speed of the car was higher than the car it collided into. I think that's a right direction to go.


0

Silly me, I assumed I already made the Player's Rigidbody a trigger. Also, you need to make each and every child of the planet (like the tops of the planet) that you want to let the player bounce on to be static; not just the empty object parent. I'm very sorry I never double-checked, but I hope this helps others out.



Top 50 recent answers are included