- Home /
Ridgidbody cube
So my issue today is that my cube wont accelerate properly... is there any pointers or tips I could have???
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Player : MonoBehaviour
{
public float movementSpeed;
private float movementSpeedMaximum = 4f;
Rigidbody playerRigidbody;
Vector3 movement;
// Start is called before the first frame update
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.z = Input.GetAxisRaw("Vertical");
movement = new Vector3(movement.x, 0, movement.z).normalized;
if(playerRigidbody.velocity.magnitude < movementSpeedMaximum)
{
playerRigidbody.AddForce(movement * movementSpeed);
}
}
}
my cube wont move, it shows a change in cords but its so small u cant even tell
take off the .normalized and see if that helps.
Sure it's a trivial question, but it's best to make sure. Have you set a value for the "movementSpeed" variable in the inspector?
Decrease the mass of the object to something like 0.1. Or alternatively, just increase the value of movementSpeed
variable.
Answer by s4shrish · Jul 05, 2021 at 11:30 AM
I am guessing the Cube is not moving at all? That may be because the "force" multiplier you are using is too low. You have named that variable as Movement Speed when it is in fact Force Magnitude Multiplier.
A good way to have a responsive character that is still force driven, but kinda good to control is to crank up the ForceMagnitude for movement to like 1000 or something, but also set the DRAG in Rigidbody properties to something like 500. This way it accelerates to max speed quickly, and automatically decelerates. This works well in 2D, but in 3D that drag will also affect you jumping so it becomes tricky.
Try that. And ofcourse you can simply set the Rigidbody's velocity directly as well, but that means your player may simply cancel any force that is applied to it, as the next frame it will immediately snap to a value determined by input.
I did that, it works, but if I un freeze the rotation axes it'll pick up momentum quicker due to the cube rolling.
Your answer
Follow this Question
Related Questions
Why wont my character jump? (using rigidbody and addForce) 1 Answer
Consistent AddForce distance/direction 2 Answers
How to move forward constantly and right left when it's necessary 0 Answers
How prevent addforce when clicking resume button? 1 Answer
Projectile not moving properly in top down 2d shooter 2 Answers