- Home /
 
               Question by 
               theshaggyking347 · Jan 04 at 02:32 AM · 
                c#movementrigidbody.addforcegetaxis  
              
 
              Input.GetAxisRaw("horizontal") not working
whenever I move moveHorizontal is always 0 how can I change this? //setting up variables private Rigidbody2D rb2D;
 private float moveSpeed;
 private float jumpForce;
 private bool isJumping;
 private float moveHorizontal;
 private float moveVertical;
 void Start()
 {
     //defining variables
     rb2D = gameObject.GetComponent<Rigidbody2D>();
     moveSpeed = 3000000f;
     jumpForce = 60f;
     isJumping = false;
 }
 void Update()
 { //input for left right movement
     float  moveHorizontal = Input.GetAxisRaw("Horizontal");
     moveVertical = Input.GetAxisRaw("Vertical");
     Debug.Log("moveHorizontal " + moveHorizontal);
     Debug.Log("Velocity " + rb2D.velocity.x + " " + rb2D.velocity.y);
 }
 void Addforce()
 {
     //function that adds a force to rigidbody
     rb2D.AddForce(new Vector2( moveHorizontal * moveSpeed, 0f), ForceMode2D.Impulse);
    
 }
 private void FixedUpdate()
 {
     if (moveHorizontal  > 0.1f || moveHorizontal < -0.1f)
     // if moveHorizontal is greater then 0.1f or if moveHorizontal is less than -0.1f then put a force to a rigidbody
     {
         Addforce();
     }
 }

 
                 
                rgggh.png 
                (23.0 kB) 
               
 
              
               Comment
              
 
               
              Answer by wfreeman480 · Jan 04 at 03:21 AM
Tweaked your code a bit and I was able to get horizontal movement. Let me know if this is along the lines of what you're looking for. Hope it helps.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class Move : MonoBehaviour
 {
  private float moveSpeed;
  private float jumpForce;
  private bool isJumping;
  private float moveHorizontal;
  private float moveVertical;
  Rigidbody2D rb2D;
  void Start()
  {
      //defining variables
      rb2D = GetComponent<Rigidbody2D>();
      moveSpeed = 900f;
      jumpForce = 60f;
      isJumping = false;
  }
  void Update()
  { //input for left right movement
  Addforce();
      //float  moveHorizontal = Input.GetAxisRaw("Horizontal");
      //moveVertical = Input.GetAxisRaw("Vertical");
      //Debug.Log("moveHorizontal " + moveHorizontal);
      //Debug.Log("Velocity " + rb2D.velocity.x + " " + rb2D.velocity.y);
      
  }
  void Addforce()
  {
      if(Input.GetKeyDown(KeyCode.D))
      {
           rb2D.AddForce(new Vector2( 1, 1f), ForceMode2D.Impulse);
      }
       if(Input.GetKeyDown(KeyCode.A))
      {
           rb2D.AddForce(new Vector2( -1, 1f), ForceMode2D.Impulse);
      }
      //function that adds a force to rigidbody
     //rb2D.AddForce(new Vector2( moveHorizontal * moveSpeed, 0f), ForceMode2D.Impulse);
     
  }
  
 }
Also you can add move speed to the addforce function rb2D.AddForce(new Vector2( moveSpeed, 1f), ForceMode2D.Impulse);
Your answer
 
 
             Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Making RB movement smoother/curvy. C#. 1 Answer
Smooth dashing issues 0 Answers
Making RB movement smoother/curvy. C#. Unity 0 Answers
Multiple Cars not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                