- Home /
Question by
Creative Inventory · Aug 02, 2015 at 06:25 PM ·
player movement
How do I get my player to move up and down
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float Speed = 3f;
public float MaxJumpTime = 2f;
public float JumpForce;
private float move = 0f;
private float JumpTime = 0f;
private bool CanJump;
void Start () {
JumpTime = MaxJumpTime;
}
void Update ()
{
if (!CanJump)
JumpTime -= Time.deltaTime;
if (JumpTime <= 0)
{
CanJump = true;
JumpTime = MaxJumpTime;
}
}
void FixedUpdate () {
move = Input.GetAxis ("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * Speed, GetComponent<Rigidbody2D>().velocity.y);
if (Input.GetKey (KeyCode.W) && CanJump)
{
GetComponent<Rigidbody2D>().AddForce (new Vector2 (GetComponent<Rigidbody2D>().velocity.x,JumpForce));
CanJump = false;
JumpTime = MaxJumpTime;
}
}
}
At first my player just keep falling once I added a rigid-body 2D, so I click on "is Kinematic". My game was working (going left and right as I used the arrows) but when I tried to go up and down my player just doesn't go up or down, so ... could you please help me. Thank you!!! (p.s. I'm a beginner)
Comment
Answer by Garross · Aug 02, 2015 at 07:27 PM
Kinematic objects aren't affected by forces and have to be moved via changing the transform.postition e.g transform.translate. However what I would recommend is that you turn off kinematic and turnoff gravity for your rigid-body , so they won't fall.