- Home /
change the forward, or maybe normalize forward, depending on rotation
I'm prototyping a skateboarding game so I need the board to continue to travel quite a distance after the force has been applied so I've got friction and mass down and using
public float moveSpeed; // forward speed
public float rotateSpeed; // speed when turning
public float jumpSpeed; // force applied to a jump
public Rigidbody boardbody; // call the rigidbody
private bool isGrounded; // raycast to the ground
private bool isNotGrounded; // raycast to the ground
private Quaternion xrotation; // trying to normalize forward
private Quaternion rotCur; // Quat for alignment
void Move()
{
// forward
if (Input.GetButton ("A") && isGrounded)
boardbody.AddForce (transform.forward * moveSpeed);
// slowdown
if (Input.GetButton ("B") && isGrounded)
boardbody.AddForce (-transform.forward * (moveSpeed / 2));
}
to go forward and slowdown which is working nicely. I've got a few rotation things to tilt and to properly rotate, for example my rotation >>
void Rotate()
{
if (isGrounded)
{
float rotateAroundX = Input.GetAxis ("LeftStickX") * rotateSpeed;
Quaternion xrotation = Quaternion.Euler (0, rotateAroundX, 0);
boardbody.MoveRotation (boardbody.rotation * xrotation);
}
}
However, after I stop applying forward motion with ("A") the rotation no longer effects the "forward" or rather I should say if not holding down ("A") then the board just spins (rotates correctly lol) and continues traveling in whatever direction.
I'd like the board to change its "forward" (I think that verbalizes what I'm aiming for) while continuing to coast or "roll" forward even if I'm not holding down the ("A")
I started another method to try to maybe normalize the forward or update it I'm not sure what I need to do but his is where I am at
void FixForward() // set apply forward magnitude to new rotation angle
{
if (isGrounded)
{
Vector3 targetForward = xrotation * Vector3.forward;
}
}
because a Quaternion isn't really a vector right so I tried to define one. I am just not sure how to apply it, when I do something like >boardbody.transform.rotation = targetForward * moveSpeed;< I get a constant force that pushes the board ha ha and it flies off the world.
I'm sure it has to do with the fact that I'm applying forces to transform rather than world space V3, Anyway if you guys have any ideas I really appreciate it!
So i tried changing the way the I added force to the rigidbody forward, so ins$$anonymous$$d of boardbody(rigidbody).addforce I tried boardbody.veloctiy, and addrelativevelocity, and after I got each try working in a different way, the problem with turning persisted. I assume then I need to change the rotation to something else, or figure out how to update the forward position with each turn?
So using this guide >> link text
it said not to apply a Quaternian to a rigidbody so ins$$anonymous$$d I tried this bit of code >>
float rotateAroundX = Input.GetAxis ("LeftStickX");
boardbody.AddTorque(transform.up * rotateSpeed * rotateAroundX);
nonetheless the object rotates but does not carry momentum into its forward facing position.
Tried this, I think I'm getting close, well I hope so,
void FixForward() // set apply forward magnitude to new rotation angle
{
if (isGrounded)
{
Vector3 direction = boardbody.transform.position - transform.position;
boardbody.AddForceAtPosition (direction.normalized, transform.position);
}
}
I put in AddForceAtPosition like the docs recomend, though it hasn't had an effect I hope this is what I need!
$$anonymous$$aybe I'm going about this all wrong, I have very little mass and very very little friction trying to get the player to roll for a long distance, perhaps this is why when turning it just spins? How can I get the player to keep moving forward after applying the force, and either turn up friction or mass or both I wonder?
Answer by Light_ · Jul 31, 2016 at 08:26 PM
Well I have a viable solution, and after almost a week of working on this one problem I'm pretty happy to move on, I added this method:
void FIxForward() // set apply forward magnitude to new rotation angle
{
if (boardbody.velocity.magnitude < 3.0f) {
isMoving = false;
}
else if (boardbody.velocity.magnitude > 3.0f) {
isMoving = true;
}
if (isMoving && isTurning) {
boardbody.AddForce (transform.forward * (moveSpeed));
} else if (isMoving && !isTurning) {
boardbody.AddForce (transform.forward * 0.0f);
}
}
I'll add a timer or something so eventually the board comes to a stop, but YAY it turns like its supposed to!!
Just want to add your if statement in line 6 is unnecessary since it's the reverse of the statement in line 3. Also at the moment you have a "hole" in the velocity range at "3.0f". It's very unlikely that this ever happens but it's still a logic error.
Next thing is if you just want to enable a boolean value when a condition is true and disable it when it's false, you don't need an if statement at all. The condition inside your if statement is already a boolean expression. You can simply "store" it in your boolean:
is$$anonymous$$oving = boardbody.velocity.magnitude > 3.0f;
This will make "is$$anonymous$$oving" true when the velocity is greater than 3, otherwise it's false.
Next thing is this line:
boardbody.AddForce (transform.forward * 0.0f);
is completely pointless ^^. If you multiply a vector by 0.0f it becomes Vector3.zero which is (0, 0, 0). Adding zero force is pointless so you can remove the whole line including the if statement.
So this would do the same:
is$$anonymous$$oving = boardbody.velocity.magnitude > 3.0f;
if (is$$anonymous$$oving && isTurning) {
boardbody.AddForce (transform.forward * (moveSpeed));
}
Your answer
Follow this Question
Related Questions
Wrong contineous rotation to car 0 Answers
Flip over an object (smooth transition) 3 Answers
how to ignore transform.position.y 3 Answers
How to get all gameObjects with a script on them 1 Answer
TRouble with quaternion 1 Answer