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
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.
[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.
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;
}
}