- Home /
Using rigidbody to move the player
I'm trying to use rigidbody.velocity to move my player, however it doesn't seem to work at all. Here is the rigidbody settings on the player: Here are the input settings: And here is the file controlling player movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update () {
Vector3 desiredVelocity = Input.GetAxis("Horizontal") * transform.right + Input.GetAxis("Vertical") * transform.forward;
rb.velocity = Vector3.Lerp(rb.velocity, desiredVelocity, Time.deltaTime * 5f);
}
}
Can someone explain to me why this doesn't work?
Answer by Gamer206 · Jul 25, 2017 at 12:46 PM
Use this:
Rigidbody rb;
public float speed;
void Start () {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate () {
float mH = Input.GetAxis ("Horizontal");
float mV = Input.GetAxis ("Vertical");
rb.velocity = new Vector3 (mH * speed, rb.velocity.y, mV * speed);
}
This will make troubles with diagonal moving, the diagonal moving is going to be faster than vertical or horizontal.
Is there a way of making the rigidbody move where the camera is facing? Because I'm trying to use this script on a first person game and the Player is only moving in 4 directions only
Answer by TWicked · Jul 15, 2017 at 03:29 AM
You can use this code to move player
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial/moving-player?playlist=17141
Answer by SilverScales · Jun 13, 2018 at 08:52 PM
Is there no way to move the player in a "snappy" fashion, without the slow responsiveness?
$$anonymous$$aybe this could help: float h = Input.GetAxis("Horizontal");
if(h * GetComponent<Rigidbody2D>().velocity.x < maxSpeed)
{
rb.AddForce(Vector2.right * h * moveForce);
}
if($$anonymous$$athf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
{
rb.velocity = new Vector2($$anonymous$$athf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
Better later than never. SilverScales, the slow responsiveness is because the examples above are using an Axis input, those go from -1 to 1 (where 0 is the resting value) interpolation. You can just normalize those numbers and go using rounded values to get the snappy movement you mentioned.
Yes absolutely, allot of the people here are using Input.GetAxis("Horizontal") or Vertical. These are actually predefined inputs. Here is the thing, you can edit these inputs or even create your own.
CLIC$$anonymous$$ $$anonymous$$E TO SEE HOW
Allot of the inputs have a growth or decay factor. $$anonymous$$eaning how quickly they ramp up or slow down. Great for realistic movement. But if you want snappy then simply set the growth the same speed as the boundries. That will give you instant movement and stopping.