- Home /
Question by
mat_mcloughlin · Jul 31, 2014 at 03:18 PM ·
animation2dcharacter
2d platformer moving character in the z-axis
Hey,
Should probably start this question by saying I'm new to games dev and unity.
I've been playing with 2d gaming in the very basic sense of having a platform and a character that moves left and right on said platform and can jump.
What I'm experimenting with now is the ability to create an animation that will move the character away into the distance (z-axis) upon a key press event (not holding the key).
I've managed to get the animation to work outside of the update method but not within.
Here's my code so far:
public class TheDudeMovement : MonoBehaviour
{
public float maxSpeed = 6.0f;
public bool facingRight = true;
public float moveDirection;
public float jumpSpeed = 600.0f;
void FixedUpdate()
{
rigidbody.velocity = new Vector3 (moveDirection * maxSpeed, rigidbody.velocity.y, 0);
}
void Update()
{
moveDirection = Input.GetAxis("Horizontal");
if (Input.GetButtonDown ("Jump"))
{
rigidbody.AddForce (new Vector2 (0, jumpSpeed));
}
if (Input.GetButtonDown("Fire2"))
{
var toPosition = new Vector3(rigidbody.position.x, rigidbody.position.y, 5f);
rigidbody.position = Vector3.MoveTowards(rigidbody.position, toPosition, Time.deltaTime * 2f);
}
}
}
So any guidance would be appreciated. Sorry for the poorly constructed question but I'm still learning the terminology
Thanks in advance
Comment