Question by
Aniimo · May 16, 2016 at 02:50 PM ·
movement scriptvisual studio2d physics2dmovement
UNITY 2D: Player moving up when space/mousebutton is holded.
So, I have 2D platform, and i want my player to moves up when space or mouse button is holded. My gameobject is flying (something like flappy bird) and ground is not necessery. So when mouse button is holded I want my gameobject (player) to moves up untill it's unholded. And, when it's unholded it' can be holded again (it doesn't need to hit the ground). I set:
rigidbody mass :0 gravity scale:0.5, linear and angular drag:0 enable "freeze rotation"
using UnityEngine;
using System.Collections;
public class PixelMovement : MonoBehaviour {
public float playerSpeed;
public Vector3 gravity;
public float maxSpeed = 5f;
Vector3 velocity = Vector3.zero;
public float fowardSpeed = 1f;
bool didPress = false;
void Update()
{
if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
{
didPress = true;
}
}
void FixedUpdate()
{
velocity.x = fowardSpeed;
transform.position += velocity * Time.deltaTime;
if (didPress == true)
{
didPress = false;
float amntToMove1 = playerSpeed * Time.deltaTime;
transform.Translate(Vector3.up * amntToMove1);
}
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
}
}
Comment
Your answer

Follow this Question
Related Questions
I have problem with using vs with unity 0 Answers
How to set and change enemy path in real time in game ? 1 Answer
Character Movement bug. 0 Answers
How to move a Gameobject in an Android Game with ScreenLandscape? 0 Answers
Random barriers 0 Answers