How to fix spite character changing position when turning?
I have a simple movement left and right on my 2d character. My problem is that when it turns/changes direction the character changes position. How can I have my character change direction/turn around while staying in the same position? Any help would be much appreciated!!
using UnityEngine;
using System.Collections;
public class playercontroller : MonoBehaviour {
public float speedForce= 50f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2 (-speedForce,GetComponent<Rigidbody2D>().velocity.y);
transform.localScale = new Vector3(-1,1,1);
} else if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2 (speedForce,GetComponent<Rigidbody2D>().velocity.y);
transform.localScale = new Vector3(1,1,1);
}
else
GetComponent<Rigidbody2D>().velocity = new Vector2 (0,GetComponent<Rigidbody2D>().velocity.y);
}
}
Answer by Vuzok · Dec 26, 2015 at 05:24 PM
Worked it out for anyone who may run into a similar issue my object had some animations attached to it that were causing the center point to change.
Your answer
Follow this Question
Related Questions
Does anyone know how to make an object(enemy) follow a path set by the player in a 2D side scroller? 1 Answer
How do you Isolate Physics2D.OverlapArea to a single script/gameObject/Instance? 0 Answers
Need Help Getting Basic Mechanics Started (2D Puzzle Platformer) 0 Answers
"Invalid argument"? 0 Answers