- Home /
simple 2d controller with a and d.
I need help making a 2d survival game and I need this script just to even make it a working game I've tried many scripts but they all failed this is my last resort please help me out.
Answer by Glenomat · Aug 11, 2020 at 09:08 PM
This should do the trick, make sure the script is attached to the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Testing : MonoBehaviour
{
public Rigidbody2D playerRB;
public float movmentSpeed = 10f;
private void Start()
{
playerRB = GetComponent<Rigidbody2D>();
}
private void Update()
{
if(Input.GetKey("a"))
{
playerRB.velocity = new Vector2(playerRB.velocity.x * movmentSpeed * Time.deltaTime, playerRB.velocity.y);
} else if(Input.GetKey("d"))
{
playerRB.velocity = new Vector2(playerRB.velocity.x * -movmentSpeed * Time.deltaTime, playerRB.velocity.y);
}
}
}
Don't multiply Time.deltaTime
into a velocity vector - deltaTime will already be applied when Unity adds that vector to the RB's position.
Your answer

Follow this Question
Related Questions
,Spawning snow or changing tilesets to snow as the player walks past them 0 Answers
Adding knockback to a 2D game (in C#) 2 Answers
How can I stop hurt animation and bounce the player when hit by an enemy? 0 Answers
2d Iteam respawn in Spawn point 0 Answers
How do I do a Snappy Jump in 2D with a 2D Jump Script? 0 Answers