Tag Info

New answers tagged

7

The "normalized direction vector" is how this task is usually approached, and how I often do it, but lately I've simply been clamping the resulting movement vector. It usually achieves the same end result and the code is a lot simpler: var moveSpeed = 6.0f; function Update() { var movement = Vector3.zero; movement.x = Input.GetAxis("Horizontal") * ...


20

You need to take the sum of the directions, normalize that, then multiply by the speed. I tangentially answered this as part of my response to Preventing diagonal movement Specifically: velX = 0; velY = 0; if(keyLeft) velX += -1; if(keyRight) velX += 1; if(keyUp) velY += -1; if(keyDown) velY += 1; // Normalize to prevent high speed diagonals length = ...


10

Separate your direction selection code from actual movement code. Choose Direction by checking which keys are pressed. Store it as a unit (normalized) vector. Multiply your Direction with Speed and with DeltaTime. Apply resulting transform to your object/camera.


1

It's really your tools' responsibility to respond appropriately to events, so I'd recommend representing each of your tools as an object containing functions that handle mouse events and delegating the event handling to the currently active tool. In practice, you can do this by defining your tools var railsTool = { onMouseUp : function(event) { /* ...


2

The HTML5 canvas works... well... like a canvas. It is a collection of RGBA pixels. When you draw something, it replaces whatever was there before. There is no "undo" functionality. The usual approach is what you are doing right now: start each rendering frame by completely erasing the canvas and redrawing the whole scene from scratch. When you have lots ...


1

You should specify clearly what you are trying to accomplish. You speak about progress bar but use a playerEnergy variable. Are you trying to fill or to deplete the bar? In any case this line looks suspicious. playerEnergy=Time.time *0.02; This line has two potential problems (that we cannot state properly until we know exactly what you want to do). The ...


1

I agree with Oliver Schöning's response. I haven't used ImpactJS before, so I'll talk about C2: C2 pros: Very well documented. Many tutorials. Dedicated plugins: Platformer games become very easy to develop. The SDK is extensible with your own plugins and behaviors. C2 cons: To integrate some third party plugin in your game (e.g. Socket.io) you must ...


3

First off, if you are that new to Unity, maybe you would benefit a lot from going through tutorials. Unity website has some pretty handy tutorials, plus some nice step-by-step complete projects. Nevertheless I´m going to give you a general answer to guide you with your question. For my answer I assume you are familiar with some basic Unity3D concepts, as ...


2

Check out colliders. You'd likely want to implement OnCollisionEnter in your character class. When something collides with the player, this method will be activated. Inside this method is where you'll deduct from the player's health. If the player can only collide with one thing (the sphere objects), you can make it pretty simple: function ...


1

In your second if you access the pillar and you set it to an object. When you try to access the pillar-Object, it never has been set and as such is initialized with null. This is your error. //first if { tempPillar = GameObject.FindGameobjectWithTag ("Pillars"); //second if with tempPillar! { pillar =tempPillar; //play ...


3

Here is my suggestions Dealing with Trees You can do any tree by using tables. It can serve as dependency counter for what ever tree you are doing. Already tested by me some time ago were skill tree, knowledge tree and my favorite, item tree. For this example we are going to use "Item" tree. Each item will be a record for the table name ItemTree. They ...


1

You are only checking to see if healthbar is not zero (if(healthbar!=0)), you should rather check if it is greater than 0, like so: if(healthbar>0) { //Do something } But it might be easier to check if it is less than or equal to zero, so that you can use it to end the game easier, like so: if(healthbar <= 0) { EndGame(); } else { ...


2

Your error is on these lines if(healthbar!=0){ healthbar-=30; Because 500 is not divisible by 30, you get something like this: 50, 20, -10, -30... What you really mean is if(healthbar>0){ healthbar-=30; Because you want to decrease if it is bigger than 0, not if it is different than 0 (very important distinction!). Also, in order to make ...


1

It might be related to your Update function: function Update() { if (triggered && Input.GetKeyDown(KeyCode.JoystickButton1)) particle1.SetActive(true); particle2.SetActive(true); particle3.SetActive(true); } Having no brackets, only particle1.setActive(true) is in the condition. The other two will be called on each ...


0

I am making assumptions here about your code. Please clarify your question if this answer don't meet your intentions. I will create a multidimensional array to store your data points in, and will use nested for-loops to do so: var dataPoints = new Array(100); for (var i = 0; i < dataPoints.length; i++) { dataPoints[i] = new Array(100); } for (var x ...


1

If I understand correctly, you have a bullet "object" of size 36x6 colliding with an enemy of size 56x69. Let's take a look at each step of the collision: bullets[a].x + 36 >= enemies[b].x This will test if the bullet right side is to the right of the enemy's left side. Right after, you have this: bullets[a].x <= enemy[b].x + 56 Here, you test ...


2

You problem is in speed of checking for collisions. there are two possible solutions: Check several (n) times each frame (=move 1/n speed n times) Instead of point-rectangle intersection perform line segment - rectangle intersection the first solution is fast and easily implemented, on the other hand it doesnt really solve anything, if your bullets are ...


3

At first glance, I would say you're going to have a problem with splicing during the loop. Say you're at index 1 and you have a collision. You then remove that spot, and now everything moves up a space. But your loop counter still increments. So what used to be in the second index is in the first one, and you went from the first to the second, skipping it ...


0

How I'd do it would be the following: (Sorry for the lack of code, on my lunch break!) First: Create a new class. A door controller, or something similar, give it a number of different (probably non-returning) update functions that correspond to the switches. Give it variables to record the state of the object at the current time. Each update function ...


2

To add a puzzle like quality to this you need to generate some sort of dependency on the switch values (the door shouldn't really know about this, the switches should notify the door it can open.) I would use a parent object much like indeed005 is suggesting but with some important changes: //List references import System.Collections.Generic; //Always a ...


0

Think about this: Should the second switch even be able to be flipped if the first switch is not already ON? If so, it may lead to some strange logic. Assume door checks if switch1 was activated before switch 2 Player flips switch 2 to ON Door does not open Player flips switch 1 to ON Door does not open Player flips switch 2 to OFF Door does not open ...


0

There are a number of ways. This is a really open ended problem, you can solve it however you want. Three options off the top of my head... Store the game tick or timestamp that each switch was pressed on, and then compare ticks/timestamps to ensure Switch1 was pressed first. Have Switch2 trigger an event on the door if it is pressed while Switch1 is down. ...


0

You could use a bitwise OR to just truncate: GUI.Label(Rect(1,1,100,20), "Health: " +cur_health | 0); Or, if you care about accuracy, you can use Unity's API to round it: GUI.Label(Rect(1,1,100,20), "Health: " Mathf.Round(+cur_health));


1

You should not use GetKeyDown inside the OnTriggerStay. It should only be called from the Update method. Therefore, you need to implement OnTriggerEnter and OnTriggerExit to check whether the condition is satisfied or not by holding a flag something like 'triggered'. Inside the Update method of Switch1 and Switch2, you need to check the flag; if it is true, ...


6

Don't forget that every object in .NET has a ToString() method. Many of the built-in value types accept an optional formatting argument; numeric types in particular are easy to format with standard formatting or custom formatting. For example, instead of just passing cur_health, you could pass cur_health.ToString("n0"). For the specific question, you could ...


3

I will explain it line by line: var time = new Date().getTime() * 0.002; new Date().getTime() returns the current time in milliseconds (1 second = 1000 milliseconds). This is multiplied by 0.002 to scale the time -- slow it down, in this case. var x = Math.sin( time ) * 192 + 256; Scaling down the time is important, because it is used in this ...


0

public GameObject newSprite; private Vector3 currentSpritePosition; void Start(){ newSprite.renderer.enabled = false; } void OnMouseEnter(){ //getting the current position of the current sprite if ever it can move; currentSpritePosition = transform.position; //then make it invisible renderer.enabled = false; //give the new ...



Top 50 recent answers are included