- Home /
I WILL GIVE YOU 3 DOLLARS to answer this constantforce question
I'll paypal you 3 dollars(I'm poor as fuck) if you can fix my script so that it works the way I want it to work, it's driving me crazy! For some reason the later if statement cancels out the first one. Here's my code:
private var rotateSpeed : float = 0.0; private var flipSpeed : float = 0.0; var speedness : float = 20.0;
function StartSpinning () {
rigidbody.constantForce.relativeTorque = Vector3 (0, rotateSpeed * Time.deltaTime, 0); }
function StartFlipping () {
rigidbody.constantForce.relativeTorque = Vector3 (0, 0, flipSpeed * Time.deltaTime); }
function FixedUpdate () { if (Input.GetKey("left")) { rotateSpeed = speedness; StartSpinning();
}
if (!Input.GetKey("left")) { rotateSpeed = 0; StartSpinning(); }
if (Input.GetKey("up")) { flipSpeed = -speedness; StartFlipping();
}
if (!Input.GetKey("up")) { flipSpeed = 0; StartFlipping(); }
}
How about some free advice ins$$anonymous$$d:
format your code, if it's not easily readable people (such as myself) aren't even going to waste our time trying to decipher it.
Don't repost your question several times per day. People use the "unanswered" filter (among others) to see who is still waiting for an answer, or has not received a satisfactory answer. There is no need to repost questions over and over again (this is your third time posting the same question in less than four hours. imagine if everybody did that.. this site would be a cesspool.
Leave the CAPS and hyperbole out of your question topic. We don't care how desperate you are, if someone can help you solve your problem they will help. Bad manners will just cause people to avoid your question entirely.
Answer by aldonaletto · Sep 07, 2011 at 03:05 AM
StartSpinning and StartFlipping are zeroing all axes but the one they set, so the later always cancels the former. You should alter only the axis used in each function. Additionally, you should not multiply the speeds by Time.deltaTime (it may even work, since in FixedUpdate it's usually fixed to 0.020):
function StartSpinning () { rigidbody.constantForce.relativeTorque.y = rotateSpeed * 0.02; }
function StartFlipping ()
{
rigidbody.constantForce.relativeTorque.z = flipSpeed * 0.02;
}
If this answer solve your problem, please click the "check" button below the voting thumbs to mark it as accepted - and save your 3 bucks by now: if you get rich someday I'll send a Paypal money request to you with the interest rates added.
Holy shit that works, thank you so much! Sorry about the noobiness, I'm used to immature forums where you need to shout to be heard. If ever you change your $$anonymous$$d, let me know and I'll hook you up with that 3 dollars.
Added an upvote because in addition to being correct, I like the idea of interest rates on the 3 bucks. ;)
Answer by ThomasQ · Sep 07, 2011 at 01:35 AM
Try using Input.GetAxis("Horizontal"), this does what you want without having to go through each individual key.. Here's the scriptreference.
That didn't work. Thanks for giving it a shot though. Anybody else?
I tried this and it didn't work at all.
var flipSpeed : float = 10.0; var rotateSpeed : float = 10.0;
function FixedUpdate () { var translation : float = Input.GetAxis ("Vertical") flipSpeed; var rotation : float = Input.GetAxis ("Horizontal") rotateSpeed;
if (Input.GetAxis("Vertical"))
{
rigidbody.constantForce.relativeTorque = Vector3 (0, 0, flipSpeed * Time.deltaTime);
}
if (Input.GetAxis("Horizontal"))
{
rigidbody.constantForce.relativeTorque = Vector3 (0, rotateSpeed * Time.deltaTime, 0);
}
}
Answer by Spartan301 · Sep 07, 2011 at 03:03 AM
Try replacing your second and fourth if statements, the ones with if(!Input...), with else statements. Also, is the code running or are you getting compile errors.
Try replacing your second and fourth if statements, the ones with if(!Input...), with else statements. Also, is the code running or are you getting compile errors.
Or if you want to use Input.GetAxis, try this:
private var rotateSpeed : float = 10.0;
private var flipSpeed : float = 10.0;
function FixedUpdate () {
var torqueY = Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
var torqueZ = Input.GetAxis("Vertical") * flipSpeed * Time.deltaTime;
rigidbody.constantForce.relativeTorque = Vector3 (0, torqueY, torqueZ);
}
I would recommend the GetAxis approach because it has much cleaner code.
Answer by Scribe · Sep 07, 2011 at 05:17 PM
var rotateSpeed : float = 0.0;
var flipSpeed : float = 0.0;
var speedness : float = 20.0;
function Update (){
if (Input.GetKey("left")){
rotateSpeed = speedness;
}
else{
rotateSpeed = 0;
}
if (Input.GetKey("up")){
flipSpeed = -speedness;
}
else{
flipSpeed = 0;
}
rigidbody.constantForce.relativeTorque = Vector3 (0, rotateSpeed, flipSpeed);
}
try this, it worked for me for what I was guessing you were trying to make it do however as you didn't say exactly the effect you were trying to achieve I might have been doing the wrong thing
Scribe
P.S I don't want your money :)
oh and you can change the vars back to private just it is easier to see where the problems are if I could see there values in the inspector
That makes both the constant force's work but it stops their momentum once the other keys are pressed. aldonaletto's answer is exactly what I was looking for. Thanks for trying though!
Your answer
Follow this Question
Related Questions
jump and backflip(rotate) like in altos adventure 0 Answers
how to rotate velocity/ tying movement to camera 0 Answers
Rotate to look at with physics 2 Answers
add force to rotation 1 Answer