Hi,
I’m total new on unity, sorry if I don’t ask this question correctly or if it’s a double post…
I have an strange problem with my character and his movement.
When my character with a box collider 2D and rigidbody 2D move, sometime, he stop… Without reason ! The ground is a simple sprite of 60px with a box collider 2D.
Here my script for control my character (if you have a suggestion for this script, say me
)
using UnityEngine;
using System.Collections;
public class playerControlScript : MonoBehaviour {
public float speedMax = 10;
public float speedUp = 5;
public float speedDown = 8;
private Vector2 movement;
private float currentSpeed;
private bool isRight;
void Update () {
float inputX = Input.GetAxis("Horizontal");
Debug.Log (inputX);
movement = new Vector2(
inputX * getSpeed(inputX),
0);
if (inputX > 0 && isRight == true) {
Flip();
isRight = false;
}
if (inputX < 0 && isRight == false) {
Flip();
isRight = true;
}
}
float getSpeed(float input){
if(input != 0)
currentSpeed += speedUp;
if (input == 0)
currentSpeed -= speedDown;
if (currentSpeed > speedMax)
currentSpeed = speedMax;
if (currentSpeed < 0)
currentSpeed = 0;
return currentSpeed;
}
void FixedUpdate()
{
rigidbody2D.velocity = movement;
}
void Flip(){
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
But I don’t understand why the character stop without reason sometime. It’s not always at the same position, that total random and the move work ( tested with Debug.log() ).
Thank you for your help and sorry for my bad English ;).
Flechenoir.