- Home /
Question by
Michael_Hotte · Jun 10, 2016 at 12:08 PM ·
2dmovementsprite2d-physicsjoystick
Moving 2D Sprite Player
So I managed to move my 2D Sprite Player with a joystick using C# code. The only problem is I want to make my player stop moving immediately after I am no longer touching the joystick. I know I could use angular drag and such to slowly stop the player, but it isn't instant. Any Ideas?
Here is my code that I used:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class FloatingPlayer2DController : MonoBehaviour {
public float moveForce = 5;
Rigidbody2D myBody;
// Use this for initialization
void Start ()
{
myBody = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical")) * moveForce;
myBody.AddForce(moveVec);
}
}
Comment
Your answer
Follow this Question
Related Questions
Joystick 2D Sprite Rotation and Movement 0 Answers
Joystick problems 1 Answer
Question on Sprite and Movement 0 Answers
My sprite is teleporting when I change my scale to -1 to flip it 1 Answer