2D walk cript

Hi
I need help whiv a 2D walk script or well I only have 3D walk script.
I have look on the “2D Gameplay Tutorial” but the 2D script is to complicate.
So i need to help whiv how i can do a 2D walk script whiv just the command left and right and jump.

//txzzo
(If you are from sweden write the anserw on swedish might be easyer to understand)

oo and one more thing.
Lets say that the “goal” is to the right than i want the char to always run whiv the nose first so not walk left whiv the tail first

public float maxWalkSpeed;
public float jumpSpeed;

Vector3 playerVelocity;

void Start()
{
	playerVelocity = Vector3.zero;
}

void Update()
{
	playerVelocity.x = Input.GetAxis("Horizontal") * maxWalkSpeed;
	
	if(Input.GetKeyDown(KeyCode.Space))
	{
		playerVelocity.y = jumpSpeed;
	}
	else
	{
		playerVelocity.y = rigidbody.velocity.y;
	}
	
	rigidbody.velocity = playerVelocity;
}

This is the simplest I can think of. You’d need to add a Rigidbody component to your GameObject. The character should fall back to the ground if you have gravity turned on.

I get a streang error from it

[Invalid UTF-8] 
Cannot determine the text encoding for argument 34 (Assets/2d g\xe5 .js).
Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.

do some one know how i solve this problem??

I am not sure how to solve the whole problem or if the script below will work, but I think I know how to get rid of your errors…

This is a C# script that he gave you and you were naming it .js. Create a C# file, name it “walk2D.cs” then copy and paste the following.

using System;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Walk2D : MonoBehaviour
{
     public float maxWalkSpeed;
     public float jumpSpeed;

     private Vector3 playerVelocity;
     private Rigidbody rBody;

     void Start()
     {
          playerVelocity = Vector3.zero;
          rBody = GetComponent<Rigidbody>();
     }

     void Update()
     {
      playerVelocity.x = Input.GetAxis("Horizontal") * maxWalkSpeed;
   
      if(Input.GetKeyDown(KeyCode.Space))
      {
          playerVelocity.y = jumpSpeed;
      }
      else
      {
          playerVelocity.y = rigidbody.velocity.y;
      }
   
      rBody.velocity = playerVelocity;
     }
}

that one did work but i want it to always go nose first how do i change that??
I have never work whiv C#

try changing the “maxWalkSpeed” to a negative number in your properties.

But the maxwalkspeed dosent it just change how fast he walk??

yeah but if its a negative number then the walk direction will be flipped.

Sorry i dont understand what you mean, shod i add some code to the script??

There’s a simple transform script you can use in JS. Check the scripting documentation out. Also play with the fps prefab.

I get an error saying that velocity isn’t defined… please help

Same here, anybody know why?

Velocity is part of rigdidbody. You need to get the instance first:

either (dirty): getComponent().velocity

or like this:

public Rigidbody rigid;

public void Start()
{
rigid = GetComponent();
}

[…]

rigid.velocity = …

Make sure you have attatched a rigidbody in the inspector. If you are using 2D Rigidbody, you need to write Rigidbody2D instead of Rigidbody in the script.

This was a post from 2010 lol. Yeah the gameobject’s no longer have rigidbody as a public member apparently so you will have to reference the rigidbody in Start before you can use it.

using System;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Walk2D : MonoBehaviour
{
     public float maxWalkSpeed;
     public float jumpSpeed;

     private Vector3 playerVelocity;
     private Rigidbody rBody;

     void Start()
     {
          playerVelocity = Vector3.zero;
          rBody = GetComponent<Rigidbody>();
     }

     void Update()
     {
          playerVelocity.x = Input.GetAxis("Horizontal") * maxWalkSpeed;

          if(Input.GetKeyDown(KeyCode.Space))
          {
               playerVelocity.y = jumpSpeed;
          }
          else
          {
               playerVelocity.y = rigidbody.velocity.y;
          }

          rBody.velocity = playerVelocity;
     }
}

hey my code doesent work i need help im trying to make my stickfigure walk but i cant i need help

Hey,
if I can help you here is a very very very simple script:

using UnityEngine;
using System.Collections;
public class basicmovementunity : MonoBehaviour {
    public float speed;             
    private Rigidbody2D rb2d;     
    void Start()
    {
      
        rb2d = GetComponent<Rigidbody2D> ();
    }
  
    void FixedUpdate()
    {
    
        float moveHorizontal = Input.GetAxis ("Horizontal");
      
        float moveVertical = Input.GetAxis ("Vertical");
    
        Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
      
        rb2d.AddForce (movement * speed);
    }
}

im looking for an ad spacebar movement script

Please don’t reply to a thread that is nearly ten years old. Make your own thread. It’s FREE!

How to report problems productively in the Unity3D forums:

http://plbm.com/?p=220