Moving the player tile by tile smoothly in Unity [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to make the player move by a specific amount (1 tile/unit in this case) and then stop him. Just like in this video:
http://www.youtube.com/watch?v=DotOAbNngc4
By pressing a key the player should move smoothly to the next tile, than stops and so on.
How can I archieve this? (in C#)

You could take a look at GridMove which is from UnifyWiki.
You would probably get more of a response if you posted this question to answers.unity3d.com or forum.unity3d.com as these forums are specifically target towards Unity3d.

Here's what I would do: you give your player class a target field, e.g. a 2d vector. This will either contain the current position of the player (when initializing or when the target has been reached) or the target that you want to move the player to.
Now in your update you do this:
if target equals your current position then check user input for movement.
if user requests movement set target to position adjacent to current position.
else slowly move player towards target.
You may want to create a field for the current position of the player besides the normal position in GameObject. You could then update GameObject.position in the update-cylce by a small delta until the positions of GameObject.position and target have the (roughly! use epsilon when comparing) same value.
Some hints
Vector2.MoveTowards can help you
To check if the target and position are "close enough" you could subtract one from the other and look at the resulting vector's magnitude.
Beware of overshooting when you move towards the target. If your movement distance is more then or equal to the distance between position and target, just set the position to the target.

Related

How to add a score text in a unity game? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 days ago.
Improve this question
I am currently working on a Unity Game , so basically it is a school project ,but next level . I build an arena , and two balls on it . The balls have to push each other on the arena and the last one standing on the arena wins . But i am stuck with my code ... i want that the first ball who falls down , immediatly to respawn the both balls in the respawn point , and then the score to go higher with 1 point every time one of the balls falls down. It seems so difficult for me and i am struggling with it. If anybody cand save me i will be happy ! Thanks !! <3
I tried a lot of methods but the only bug i have is that if a ball falls down , the game automatically spawns a lot of balls in the place the ball has fallen . And after 1 minute my map is full of balls .... and i do not want that to happen. So please i am asking for your help !
Assuming you have two ball objects, you could make a Player.cs script, that is applied to the two balls. The script could have the following fields:
The other player object
The player's score object
The position on which the player is to respawn
Then, inside your Update method, you could check whether the ball has fallen below a certain Y value. If so, it can get the other player's score object, and increase the number on it. After that, just set the position of the current object to the specified position.
Currently, I can't provide any specific APIs or code, since I don't have Unity installed, but I'll be sure to edit the answer as soon as it installs.
For what you want to do, my advice is to create an empty GameObject, add a collider, and activate the isTriggered property. To make a previously prepared vector2 or vector3 the position of the players when one of the players is triggered and to set all the forces affecting the rigidbody of the players to 0 . You can also figure out which player to score according to the tag of the triggered GameObject every time it is triggered.
I'm not a professional but I hope it helped.

How do I limit bouncing of physics objects [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to implement a lot of poker chips using Unity.
But poker chips are moving like a spring.
I want to implement poker chip in real world using Unity physics.
I tried modifiying the value on Rigidbody (Mass, Drag, Angular Dag, etc.)
and also tried modifiying Physics Material.
But Those didn't work.
I want to remove bounce of poker chips.
Sorry for my bad english.
please help me.
Here is my link
https://www.youtube.com/watch?v=Lw02Jkpfw2I
and a poker chip prefab
enter image description here
Maybe your problem can be solved by adding a physics material.
Right click in the project hierachy and create a new Phsyic Material and set its values to something like this. I have named the object ZeroBounce.
Add the Physics Material to the colliders of your chips and your floor.
You should immediately see a better result.
If the above solution doesn't satisfy you you could also add a script that limits the velocity of the chips. It will slowly lower its vertical speed to zero if it is going up. This will result in a small bounce which I think is a bit more realistic. A damp value of 10 looks pretty smooth to me. You can also just straight up set the y velocity to zero like this: currentVelocity.y = 0; instead of the lerp method. Then you won't have any bounce at all.
public float damp;
void FixedUpdate(){
var currentVelocity = rigidbody.velocity;
if (currentVelocity.y <= 0f)
return;
currentVelocity.y = Mathf.Lerp(currentVelocity.y, 0, damp * Time.fixedDeltaTime);
rigidbody.velocity = currentVelocity;
}

How to move object based on rotation another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I've got an issue in my VR project. I'm trying to move one object based on rotation of another one and first one should move only in kind of borders. I'm using a raycast to detect and control these objects. Both should go in x direction.
1 - Object that I rotate
2 - Object that should move
Any tips? Thanks in advance!
Well this looks like a lathe so I asume that you want to move the tool post over one axis when rotating the wheel.
You want to "link" them, so what I would do is rotate the wheel using a method that also moves the tool post.
Try this:
public float ratio = 1.0f;
public GameObject wheel;
public GameObject tool;
public void RotateWheel(float amount)
{
wheel.transform.Rotate(Vector3.forward * amount);
tool.transform.Translate(Vector3.left * amount * ratio);
}
Note the following:
Amount is passed as degrees.
Ratio is the ratio between wheel
rotation and tool translation. At 1, for each degree, the tool moves
1 unit. Adjust it at your will. I assumed the orientation of your
moving parts based on the model. Try changing the vector directions if they do not work as expected.

Unity, C# - can't increase gravity in endless runner? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am fairly new to Unity and am building an endless runner in Unity 3d off this tutorial: http://catlikecoding.com/unity/tutorials/runner/
I have followed the tutorial verbatim and have tried several approaches but can't increase the gravity in game - right now I have the block moving and jumping, but no matter what vectors I apply in the jump or any other variable I change the block does not "jump up" and come back down "quickly" or like most endless runners.
The gravity is too little - my cube floats in the air a while before coming down.
I am trying to achieve a gravity affect like in Jetpack Joyride; a normal strength of gravity. Gravity is checked on my Runner object and It seems as though Unity's gravity is "fixed" at a low strength.
I have tried going to Input Settings and increasing gravity from 1000 up, I have tried altering Vector of jump.
I have increased mass of the Runner object. Nothing affects gravity.
How can I increase gravity in Unity?
If you want to access the global gravity in a unity game, you can access it through Physics.gravity. You can either set it to a new vector3 or you can multiply it by a factor.
Examples:
Physics.gravity = new Vector3(0, 10, 0)
or
Physics.gravity *= 3;
If you go to the menu Edit> Project Settings> Physics, you can alter the global gravity of your project.
However, falling slowly USUALLY means that the scale of your models are wrong. for example, typically 1 unit in unity is 1 m. But if your model is 100x100 units, then that means it's 100m tall. Say it jumps 3x the height of itself, then it has to fall 300m. a fall from 300m would take a hell of a long time at earth's gravity compared to something like 2m.
So: 1) make sure your gravity is set to something reasonable, and 2) make sure things are in the correct scale. A humanoid character should be somewhere in the range of 2 units (2m ~= 6 feet).

Best way to attach a string variable onto a moving texture, and have it drawn? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently working on a game (similar to a space defender game with random enemy spawns) and need the enemies to have a random.next attached to label the enemy.
However I'm having trouble figuring a way to do so, since the enemy is also moving on its Y axis at a speed of 4; "position.Y = position.Y + 4. Also whats making this difficult is the fact that i need my bullets that collide with the enemy to somehow also delete the label, and refreshing the label with a new random number on the next spawned enemy.
Sadly I'm currently away from my work station, so i can't present any code, but as of this moment, the way I'm managing my enemy spawning is through a list, it loops to check if the list has < 5 in its .count, and if so, add another enemy with a random Y, and X position. My method of reseting an enemy after it reaches the bottom of the screen, is that once it hits Y == 950, the enemy is set to "isvisible = false", the list is then looped in a check ,where if an enemy is found with isvisible = false, it is removed from the list.
You can say that the list is slightly useless, but I'm currently using it to limit the amount of spawns on screen.
see Drawing Text with a Sprite, I would just create a Label string property in your enemy class set it to whatever you need to in its constructor, and then when you loop over your list it to draw it also draw the text that you assign when you create the enemy at whatever offset from the sprites location.

Categories