Unity - How To Make a 2d grid movement system - unity3d

So, I am trying to make a game that requires the user to move up or down on a vertical axis, it needs to be precise however so I want the character to move on a grid, how would this be possible? if anyone could help it would be well appreciated, thank you :)

#voldemort is right, but I can try to recommend a strategy.
Think of checking for each frame, then in the Update () function, if you have pressed a stast, then:
void Update()
{
if (Input.GetKeyDown(KeyCode._upKey)
transform.Translate (Vector2.up * _size_of_the_grid_box);
else if (Input.GetKeyDown(KeyCode._downKey)
transform.Translate(Vector2.down * _size_of_the_grid_box);
}
This code simply moves the gameObject up a certain value when a button is pressed, and down when another button is pressed.

Related

Move During playing Jump Animation

So the case is that I have a 36 frames long jumping animation and the jump itself starts at frame 12, where my object raises along y axis.
What would be the most easiest way to move my object forward while it's in air(during frames 12-36)?
How is jumping with animation generally done in games? I don't think my way is the best one. But I would also like to know how its usually done in my way too. Thanks
I have just read the comments and your question. Why not write this code:
Whenever You want him to jump at whatever condition:
Here I'll show you how to do it onClick of Space i.e, whenever you click space the jump animation plays whatever animation you have now the object moving in y-axis
if(Input.GetKeyDown(Keycode.Space))
{
yourPlayerAnimator.setBool("TheAnimatorCondition",true); //Bool for your animation to play if u have a idle with you.
}
if(yourPlayerAnimator.GetBool("TheAnimatorCondition")== true)
{
transform.Translate(Vector3.forward * Time.deltaTime)
}
All these Happens inside void update
Animation transition is your friend.
Put any condition when jumping to move to another animation. In this case i.e. when pressed left displacement key, move from "jump" to "jump&moveleft" or similar and when unpress return to jump state increasing the frames you spent moving to left.

Is there a way to break out of Update() function

So, I'm trying to make my own movement script for Unity with a Rigidbody component attached to my character, and I'm using this code inside of the update function:
// Up above, in public scope, before Start and Update functions
public float speed = 1f;
public Rigidbody body;
void Update() {
if (Input.GetKey(KeyCode.LeftArrow())) { // Left arrow key
body.AddForce(transform.forward * speed * Time.deltaTime);
}
}
My problem is, since it's inside of the Update() function, it keeps going, it keeps turning, even if I don't have the key pressed.
Does anyone know how to fix this problem? I have tried taking away Time.deltaTime but Brackeys said you use it so that it's frame rate dependent, so if you have a higher frame rate it doesn't turn faster, and same with a low frame rate, you turn slower. Thanks in advance.
It keeps turning even after releasing the key? Yep that's what AddForce will do.
After you apply a force to an object it will continue to move based on that force uless it has drag or an opposing force is applied. Go into the rigidbody to set the drag or create a script that applies an opposing force once you stop pressing the button.
If you put a Debug.Log within the if statement you will see it is not called every frame but only when you have the button held down.
And last, I don't think that your keycode should have () after LeftArrow, does this not produce an error?
An object in motion stays in motion with the same speed and in the
same direction unless acted upon by an unbalanced force.
---- Isaac Newton
So if you want to stop the object, you can do:
Add an oppsite force
if (Input.GetKey(KeyCode.LeftArrow())) {
...
} else if (!body.IsSleeping()) {
body.AddForce(-body.velocity * factor);
}
Give a drag value
body.drag = dragValue;
You have two questions, one on the title and another one on the description.
For the title one:
The only way to stop/break Update() cycle is to disable the script, like this:
enabled = false;
For the description one:
You have to stop the force applied to the object (as the rest of the answers exemplify).
You can apply an opposite force, or just set velocity to 0.
rigidbody.velocity = Vector3.zero;

Delete animation of a certain bone for a number of frames Unity3d

I have an animation of a Mecanim. I wonder how I can delete the animation of only the right-hand for the next couple of frames once I press a certain key in the LateUpdate() function
void LateUpdate ()
{
if (Input.GetKeyDown (KeyCode.D))
{
//delete the next lets say 100 frames of only the right-hand
}
}
If anyone could please advise how this can be done?
Comment is getting Bigger So I will paste it in here.
https://www.youtube.com/watch?v=Xx21y9eJq1U
In this video try to check the 44:51 ish or something. That is what you need. Then Control it with a script when to activate it.
It is a Masking. You can mask the animation and so on. You will get the idea when you watch this.

Check if camera if facing a specific direction

I'm working to let a character push objects around. The problem is that as soon as he touches an object he starts moving it, even when the touch was accidental and the character isn't facing the direction of the object.
What I want to do is get the direction the object when a collision happens, and if the camara is really facing that direction allow the player to move it.
Right now I only managed to get the direction of the object, but I don't know how to compare that with the direction of the camera.
This is what I'm trying now:
void OnCollisionEnter(Collision col) {
float maxOffset = 1f;
if (col.gameObject.name == "Sol") {
// Calculate object direction
Vector3 direction = (col.transform.position - transform.position).normalized;
// Check the offset with the camera rotation (this doesn't work)
Vector3 offset = direccion - Camera.main.transform.rotation.eulerAngles.normalized;
if(offset.x + offset.y + offset.z < maxOffset) {
// Move the object
}
}
You can try to achieve this in several different ways. And it is a bit dependent on how precise you mean facing the box.
You can get events when an object is visible within a certain camera, and when it enters or leaves using the following functions. With these commands from the moment the box gets rendered with that camera (so even if just a edge is visible) your collision will trigger.
OnWillRenderObject, Renderer.isVisible Renderer.OnBecameVisible, OnBecameInvisible
Or you could attempt to calculate whether and objects bounding box falls within the camera's view frustum, there for you could use the following geometry commands
GeometryUtility.CalculateFrustumPlanes, GeometryUtility.TestPlanesAABB
Or if you rather have a very precise facing you could also go for a Physics.Raycast So then you will only trigger the event when the ray is hitting the object.
Hope this helps ya out.
Take a compass obj and align it to ur device sorry object than when you move it you can always know where it points to.
theoretically it should work but maybe your object just moves around because of a bug in your simulator motion engine.

Programming controls for sprite

I am trying to make a snake kind of game on the iPhone. For that I plan to have a simple dot as the sprite which draws a line. The dot is controlled by two buttons on the left and the right of the screen enabling it to turn left or right. Now I really do not have any idea how to make the sprite move automatically forward and how to program the turning. It should not be exactly as in snake where it abruptly turns left or right but it should more be a smooth curve. I hope you're getting my point and I'd appreciate all kinds of thoughts! Thanks a lot!
Somewhat trying to make it like this:
http://www.bnet.im/images/screen_curve.png
There are a lot of ways of doing this, but one simple way would be to store the angle of the snake's trajectory (I've called it theta) and move it a fixed amount in every call to Update.
Assuming that the snake class inherits from CCNode:
-(void)Update:(ccTime)dt {
self.position = ccp(self.position.x + cos(theta)*dt,
self.position.y + sin(theta)*dt);
}
You could then update theta from your event handling logic by increasing it or decreasing it when the user taps to turn left or right.