Hi, right now we are working on a race game for school and we place a rigidbody on the spacecraft but when we use rigidbody.MovePosition we are not able to decrement the speed gradually, the vehicle stop immediatly when we stop to hold the forward button. So if you can help it would be cool ty ^^
i would suggest using rigidbody.addforce instead of move direction
I’l post my code because I dunno if I did something wrong the addForce doesnt work either
void Move()
{
if (Input.GetAxis(“Vertical”) == -1 || Input.GetAxis(“Vertical”) == 1)
{
currentSpeed += Input.GetAxis(“Vertical”) * speed;
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
moveDirection = new Vector3(0, 0, currentSpeed);
moveDirection = transform.TransformDirection(moveDirection);
rigidbody.AddForce(transform.position + moveDirection * speed);
}
else
{
currentSpeed *= decceleration;
if (currentSpeed < 0.1f)
{
currentSpeed = 0f;
moveDirection = Vector3.zero;
}
}
A force is a vector (i.e. a ‘direction’) not a point (i.e. a ‘position’), but you are submitting a position as the argument to AddForce(). It’ll work, technically, but the results are likely to be nonsensical.
Try changing this:
rigidbody.AddForce(transform.position + moveDirection * speed);
To this:
rigidbody.AddForce(moveDirection * speed);
Note that you’ll probably want to make ‘moveDirection’ unit length and replace your ‘speed’ variable with something like ‘thrust’. (For more info, look for introductory materials or references on ‘rigid body physics’.)
well the problem with scripting references is that we do our code only in c# in unity and the references are all in java
Yes, that the docs are all in (Unity) javascript can be an inconvenience for C# developers.
However, plenty of people code in C# in Unity and get by just fine. Keep in mind that most of the documentation - the descriptions of the functions and variables and how to use them - is language-independent. It’s only the code examples themselves that are specific to js.
As for the examples, figuring out the equivalent C# is usually pretty straightforward. And in cases where it isn’t, you can always ask here.
yeah dont worry I wasnt complaining its just not a good day you know we cant always have days where the code works lol and since I’m in school I’ve got another work that is a lil complicated since we got to understand some concepts of programming before being able to finish it lol but our teacher helps us a lot