- Home /
2d Character movement script (unity 4,3)
I found this script on google, because there isn't many tutorials, im learning from the script itself. The script is from 2010 and represents 2d movement. But it does not work with the new update 4,3 with the 2d features. The script can't needs a ridgidbody. But now there is a ridgidbody 2D feature, and that is the ridgidbody i use. It says in the console log, that no ridgidbody is attached to my 2d player but the script is trying to acces the ridgidbody, and i use the 2d ridgidbody. Ive tried modifying it and saying ridgidbody2d instead and some other stuff but it does not work. Can you help?
// cause the object to move more rapidly. var keyboardSpeed = 20.0; // FixedUpdate is a built-in unity function that is called every fixed framerate frame. // According to the docs, FixedUpdate should be used instead of Update when dealing with a // Rigidbody. function FixedUpdate () { // This is where we move the object. // Get input from the keyboard, with automatic smoothing (GetAxis instead of GetAxisRaw). // We always want the movement to be framerate independent, so we multiply by Time.deltaTime. var keyboardX = Input.GetAxis("Horizontal") keyboardSpeed Time.deltaTime; var keyboardY = Input.GetAxis("Vertical") keyboardSpeed Time.deltaTime; // Calculate the new position based on the above input. // If you want to limit the movement, you can use Mathf.Clamp // to limit the allowed range of newPos.x or newPos.y. var newPos = rigidbody.position + Vector3(keyboardX, keyboardY, 0.0); // Move the object. rigidbody.MovePosition(newPos); }// The horizontal speed of the keyboard controls. A higher value will
Answer by aphixe · Nov 21, 2013 at 07:34 AM
this code below works for me for moving 2d objects. i am new to coding but found a youtube video on 2d platforming. i managed to edit this code by duping it and changing Horizontal axisName to Vertical and right transform to 'up' hope this helps. but i am a noob so who knows
using UnityEngine;
using System.Collections;
public class moveLeftRight : MonoBehaviour {
public float speed =1.0f;
public string axisName = "Horizontal";
public Animator anim;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate () {
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis(axisName)));
if (Input.GetAxis (axisName) < 0)
{
Vector3 newScale = transform.localScale;
newScale.y = 1.0f;
newScale.x = 1.0f;
transform.localScale = newScale;
}
else if (Input.GetAxis (axisName) > 0)
{
Vector3 newScale =transform.localScale;
newScale.x = 1.0f;
transform.localScale = newScale;
}
transform.position += transform.right *Input.GetAxis(axisName)* speed * Time.deltaTime;
}
}
First i couldn't attach the script to my character, but i found out that i needed to rename it to:"moveLeftRight", and then i could attach it. But the character can still not move.
this is C# script. you can use js and C# in a project, but it could be your issue. also i have an animator attached to the object. try commenting out.. // public Animator anim; // anim = gameObject.GetComponent (); // anim.SetFloat("Speed", $$anonymous$$athf.Abs(Input.GetAxis(axisName)));
as these will cause the script not to work, unless you setup a simple animator setup.
also the code i got from this video, its kinda long. and i am still going thru it.. Youtube - Unity3d 4.3 SPECIAL: Learn 2d with $$anonymous$$ax! - Cooking With Unity live
Answer by sattri99 · Dec 30, 2013 at 09:21 AM
I've made a project recently and uploaded it on my blog which demostrates efficient and convinient way of 2D character movement and jumping using rigidbody. you can view my blog by clicking here
You can download the latest version of mY 2D character game From the below link. -- Download
Answer by corriedotdev · Mar 18, 2014 at 08:54 AM
There is a option in the inspector under animator, where you need to check the option 'Apply root motion'. This might solve your problem. If not let me know and ill look into it, but that error flags if you don't check it with the new unity 2d engine.
Your answer
Follow this Question
Related Questions
2D Character Flip 2 Answers
Changing Direction While In Mid-Air? 1 Answer
Unity HingeJoint2D issues when Flipping ( Negative Scaling ) 1 Answer
Unity 2D Update Release (Tile Map)? 0 Answers
Unity 2D - Make Player Auto-Move? 1 Answer