Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NinjaRubberBand · Nov 20, 2013 at 06:39 PM · 2dcharacterupdate

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?

// The horizontal speed of the keyboard controls. A higher value will

// 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); }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
 
     }
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NinjaRubberBand · Nov 21, 2013 at 03:40 PM 0
Share

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.

avatar image aphixe · Nov 21, 2013 at 09:24 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lukin · Dec 19, 2014 at 09:22 AM 0
Share

@sattri99

you have nsfw pics on your blog bro.....

other than that, thanks for the code!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges