Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why cant i move my character?

Discussion in 'Scripting' started by FastFlight, Jun 16, 2018.

  1. FastFlight

    FastFlight

    Joined:
    Jun 16, 2018
    Posts:
    2
    Heres the code, how to I bind a for left d for right and space for jump?


    using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player_Move_Prot : MonoBehaviour { float moveX; public int playerspeed = 50; private bool facingRight = false; public int playerJumpPower = 1250; // Use this for initialization void Start() { } // Update is called once per frame void Update() { //CONTROLS moveX = Input.GetAxis("Horizontal"); if (Input.GetButtonDown("Jump")) { Jump(); } //ANIMATIONS //PLAYER DIRECTION if (moveX < 0.0f && facingRight == false) { FlipPlayer(); } else if (moveX > 0.0f && facingRight == true) { FlipPlayer(); } //PHYSICS gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerspeed, gameObject.GetComponent<Rigidbody2D>().velocity.y); } void Jump() { //JUMPING COD GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower); } void FlipPlayer() { facingRight = !facingRight; Vector2 localScale = gameObject.transform.localScale; localScale.x *= -1; transform.localScale = localScale; } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player_Move_Prot : MonoBehaviour { float moveX; public int playerspeed = 50; private bool facingRight = false; public int playerJumpPower = 1250; // Use this for initialization void Start() { } // Update is called once per frame void Update() { //CONTROLS moveX = Input.GetAxis("Horizontal"); if (Input.GetButtonDown("Jump")) { Jump(); } //ANIMATIONS //PLAYER DIRECTION if (moveX < 0.0f && facingRight == false) { FlipPlayer(); } else if (moveX > 0.0f && facingRight == true) { FlipPlayer(); } //PHYSICS gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerspeed, gameObject.GetComponent<Rigidbody2D>().velocity.y); } void Jump() { //JUMPING COD GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower); } void FlipPlayer() { facingRight = !facingRight; Vector2 localScale = gameObject.transform.localScale; localScale.x *= -1; transform.localScale = localScale; } }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi FastFlight. Did you read the text of your post after you posted it? Can you read that lot?

    Could I suggest you may want to post it neatly (using code tags) so that people with knowledge of javascript (not me) can read it. :)
     
  3. FastFlight

    FastFlight

    Joined:
    Jun 16, 2018
    Posts:
    2
    1 no 2 technically yes
    3 ok
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure about jump, but a/d should be set by default to go left/right (using GetAxis) with that code.
    Check the input manager to see what "Jump" is set to by default to be sure, same with the horizontal axis if for some reason yours is not working..
    https://docs.unity3d.com/Manual/class-InputManager.html

    It looks like the same code is there at least 3 times... though I didn't actually look so closely to confirm they're identical. lol

    Read this to see how to post code with code tags (and edit your post): https://forum.unity3d.com/threads/using-code-tags-properly.143875/