- Home /
Unity 2D Movement Script
My script in MonoDevelop seems to have no problems with no errors or warnings are displayed, but when I run the game and try to move my character, there is no movement. I see nothing wrong with the script or input and I am having issues troubleshooting the problem. If it is a simple mistake it is only because I am only beginning to use Unity. Link to file: https://drive.google.com/file/d/1Jh_JKPGwWWMzmmysRTEX4MjJ_pLeUmJc/view
The file you've provided isn't enough to get anything from. You have to copy the entire project folder rather than copying the .unity scene file. If the script isn't doing anything, maybe it isn't attached, maybe it isn't referencing the player correctly. There are various causes for this.
I Agree with BDCakeRules maybe it isn't attached?
You've accidentally uploaded the scene file ins$$anonymous$$d of the script ^^; Could you just show the relevant code in your question?
I have used many different scripts and one or two of them have worked, but it is no the type of movement I want, some glide too much and is not responsive and some do not have jump movement, the rest don't work.
Answer by tormentoarmagedoom · Jan 03, 2018 at 01:46 AM
use something like this in the Update
if (Input.GetKey(KeyCode.W))
{
gameObject.transform.position = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
}
if (Input.GetKey(KeyCode.S))
{
transform.position = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
}
if (Input.GetKey(KeyCode.A))
{
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
}
if (Input.GetKey(KeyCode.D))
{
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
}
Thank you for the help, but this is not the movement I'm looking for as there is no jump and I can't seem to make the movement less than 1.
Could you edit your question and put there the code you use :)
sure, this is my code right now but apparently, the public VAR "rb" isn't showing up in the inspector and the movement itself doesn't work either thx again for the help -$$anonymous$$asterrcoder
using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public Rigidbody rb; //these are the forces that we use; public float forwardForce = 1000f; public float backwardForce = -1000f;
public float rightForce = 1000f; public float leftForce = -1000f;
void FixedUpdate () { if (Input.GetKey("d"))
{ rb.AddForce(rightForce Time.deltaTime, 0, 0); } if (Input.GetKey("a")) {
rb.AddForce(leftForce Time.deltaTime, 0, 0); } if (Input.GetKey("w"))
{ rb.AddForce(0, 0, forwardForce Time.deltaTime);
} if (Input.GetKey("s"))
{ rb.AddForce(0, 0, backwardForce Time.deltaTime);
} }
thank you, you have helped a lot but whenever I copy and paste ur code it give me 24 errors, thank u for trying to help tho, bye
Answer by CyberGamer1907 · Oct 29, 2020 at 10:35 AM
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
Vector3 movement = new Vector3(moveHorizontal, 0, 0);
transform.Translate(movement);
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement2 = new Vector3(0, moveVertical, 0);
transform.Translate(movement2);
}
Answer by BeginnerPlzHelp · Oct 27, 2020 at 10:00 PM
I have been struggling to find a good 3d movement script that was actually tested but I couldn't find anything. This was a struggle and I want people to not have to go through what I went through so after 100000 trips to StackOverflow and YouTube I created my own
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
//these are the forces that we use;
public float forwardForce = 1000f;
public float backwardForce = -1000f;
public float rightForce = 1000f;
public float leftForce = -1000f;
void FixedUpdate ()
{
if (Input.GetKey("d"))
{
rb.AddForce(rightForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rb.AddForce(leftForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("w"))
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, backwardForce * Time.deltaTime);
}
}
}
Your answer

Follow this Question
Related Questions
How do I stop momentum in 2D sidescroller? 1 Answer
How to fix my movement? 0 Answers
Player Dash doesn't work properly 0 Answers
Switching characters with camera follow? 0 Answers
How to Apply Animations to Script 1 Answer