- Home /
2d Movement Jumps to the Left
Hello,
I'm beginning to use Unity and I've been following the excellent tutorials on http://unity3d.com/learn/tutorials/modules/beginner/2d.
I'm on the last beginners video "2d Character controllers" https://www.youtube.com/watch?v=Xnyb2f6Qqzg.
My character now have basic left and right movement that can flip. Only problem is when I move left his sprites jumps halfway across the cameras screen. I'm concerned that it may be because I'm using many sprites to build my character instead of the single sprite character that makes up the character in the tutorial. I may be wrong but I can't seem to find a solution.
I've gone over the code a few times and still cannot see my problem. In C# I have the following.
using UnityEngine; using System.Collections;
public class RobotController : MonoBehaviour {
public float maxspeed = 10f;
bool facingRight = true;
void Start ()
{}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2(move * maxspeed, rigidbody2D.velocity.y);
if(move > 0 &&!facingRight)
Flip ();
else if(move < 0 && facingRight)
Flip ();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Any help is much appreciated.
Answer by Dogg · Aug 02, 2014 at 07:29 AM
I swear if I get down voted for this I'll...
I tried your script. I moved left and right no problem. I did the same thing as you(not having a single sprite sheet) and yet it still works for me. Maybe it's something that's not the script like your animations, rigidbody2D, or even colliders. I've done that tutorial before when I first started Unity, so if I remember correctly it happened to me once. Make sure you go over the video again and again. Even the smallest difference can affect your game. Sorry that's all I can say right now.
Answer by LSPressWorks · Aug 02, 2014 at 08:57 AM
Usually when this happens the object is moving too far per frame(the updates between fixedUpdate(), which I assume you're using)
The usual culprits are mass, drag and using the wrong update function...
Answer by Dun4773124 · Aug 02, 2014 at 04:21 PM
Thank you for your help. I was actually able to figure out my problem. Everything was correct except by character was not lined up evenly with my game object.
What I had to do was select game object and hit center on children. The parts making up my character moves in a few different directions and I had to redo my animations. Fortunately once I had everything lined up again it worked like a charm. Still learning the program so I'm sure it was a pretty rookie mistake.
Hopefully this helps someone else. Thank you.
Your answer
Follow this Question
Related Questions
Type"PathDefinition" does not contain a definition for "GetPathEnumerator" 0 Answers
Flipping parent object in 2d with negative x scale causes rotation issues for children gameobjects 2 Answers
Flipping a 2D Game object over time 1 Answer
2D games; Javascript or C#? 1 Answer
Spikes for a platformer game 1 Answer