- Home /
2D Lock x Axis on character
I am creating an endless runner with a flying character.
I want to lock any movement in the x axis so that the character object can only move up and down but still move forward through the scene. I have looked into freezing axes and joints, but this stops the forward movement.
Is it possible to edit the below to lock the x axis for character movement only, or is there a setting where i can apply this?
using UnityEngine;
using System.Collections;
public class moveupdown : MonoBehaviour {
public Vector2 velocity;
public float uservelocity;
public Rigidbody2D rb2D;
void Start() {
rb2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate() {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
rb2D.MovePosition (rb2D.position + touchDeltaPosition * Time.fixedDeltaTime / uservelocity);
}
}
}
Thanks maccabbe, works to an extent. X is now frozen, but as soon as i touch begins the character stops moving forward, as x becomes frozen.
I need the character to keep moving at a constant rate along the x axis, but stop the user from moving along x by dragging.
Your answer
Follow this Question
Related Questions
Collision detection for 2D topdown game 2 Answers
MovePosition not as precise as Translate with Kinematic rigidbodies (example unitypackage included) 1 Answer
Rigid Body Movement (Mobile Issue) 3 Answers
Moving horizontally a rigidbody2d 0 Answers
Need Advice: How to alternate FixedUpdates for GameObjects 2 Answers