how I can move character 2d??????
how I can move character 2d
Comment
Answer by Darkathion · Sep 13, 2015 at 09:27 AM
Try using this
using UnityEngine; using System.Collections;
public class CharController : MonoBehaviour {
float movementTangent;
public float speed = 10f;
public float jumpSpeed = 120f;
float verticalVelocity;
bool jumpKeyUp;
void Start () {
}
void Update () {
if(Input.GetAxis("Horizontal")<0){
transform.localScale = new Vector3(-1, 1, 1);
}else if(Input.GetAxis("Horizontal")>0){
transform.localScale = new Vector3(1, 1, 1);
}
jumpKeyUp = jumpKeyUp || (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.UpArrow));
verticalVelocity = 0;
if((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) && jumpKeyUp){
verticalVelocity = Mathf.Max(jumpSpeed, (-GetComponent<Rigidbody2D>().velocity.y)+jumpSpeed);
jumpKeyUp = false;
}
if(Input.GetAxis("Horizontal")!=0 || Input.GetAxis("Vertical") != 0){
movementTangent = Input.GetAxis("Horizontal")*speed;
float movementScale = 0;
if(movementTangent>0){
movementScale = (speed - GetComponent<Rigidbody2D>().velocity.x)
}else{
movementScale = (speed + GetComponent<Rigidbody2D>().velocity.x) / speed;
}
movementTangent *= movementScale;
Vector2 tmpVect = new Vector2(movementTangent/Time.fixedDeltaTime,verticalVelocity/Time.fixedDeltaTime);
GetComponent<Rigidbody2D>().AddForce(tmpVect);
}
}
Your answer
Follow this Question
Related Questions
Is it possible to test Browser Communication inside the Unity Editor without build the game? 0 Answers
How can I make Prefabs a Solid, so I cant walk through them? 2 Answers
I Have A Strange Bug 0 Answers
While trying to install the iOS editor package separately the component gives an error in the end. 1 Answer
Is there a way to disable a script in the project tab? 1 Answer