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 instead 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?
– LightSo using this guide >> [link text][1] [1]: http://www.cuelogic.com/blog/how-to-rotate-object-in-unity3d-using-rigidbody/ it said not to apply a Quaternian to a rigidbody so instead 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.
– LightTried 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!
– LightMaybe 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?
– Light