- Home /
 
How to get a rigidbody to move left and right?
Hey Programmers
I'm making a simple game. And since I'm pretty new to programming I need some help :D How do i make a rigidbody move left and right. Not forward and backwards. Just left and right :D
But I dont know how to write the script for the function... Help please?
Thank you in advance :D
               Comment
              
 
               
              Answer by Bren0831 · Mar 17, 2017 at 10:02 PM
 using UnityEngine;
 using System.Collections;
 
 public class LeftAndRightTest : MonoBehaviour {
 
     public float speed = 6.0F;
     private Vector2 moveDirection = Vector2.zero;
     void Update() {
         CharacterController controller = GetComponent<CharacterController>();
         if (controller.isGrounded) {
             moveDirection = new Vector2(Input.GetAxis("Horizontal"), 0);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
         }
         controller.Move(moveDirection * Time.deltaTime);
     }
 }
 
               This should work. No guarantees. This does require a character controller on the object that the script is on.
Your answer
 
             Follow this Question
Related Questions
Click Meh! Open door/close when clicked. 2 Answers
How to turn your object left and right 1 Answer
(NOOB Q) Whats wrong with my script? Two if's? 3 Answers
What is wrong with my RockPaperSissors script? 1 Answer
Tank controls 1 Answer