K, here's the scoop. I'm making a game where your craft hovers just above the ground and the arrows accel/turn/decel. So, everything worked fine with my test arena (which was just a big cube), but I changed it to a new scene which consists of a track I made in Blender. But once I added this scene, the craft stopped doing anything except for turning. It falls to the ground (no floating), and the horz axis keys turn the ship, but the up/down don't accelerate/decelerate. I added Print ("hi") to all the keys and they all returned "hi". I don't understand! Code (csharp): function Update () { if (Physics.Raycast (transform.position, Vector3.down, .8)) { rigidbody.AddRelativeForce (Vector3.up * 4.5); } if (Input.GetKey ("up")) { rigidbody.AddRelativeForce (Vector3.forward * 2); } if (Input.GetKey ("down")) { rigidbody.AddRelativeForce (Vector3.back * 2); } var h = Input.GetAxis ("Horizontal"); transform.Rotate (0, h, 0); } That's my good working code.
Code (csharp): var speed = 6.0; var liftSpeed = 8.0; var powerBoost = 20.0; var smooth = 2.0; var ZtiltAngle = 30.0; var steeringAngle = 30.0; var thrustAngle = 5.0; var downPressure = 5.0; private var moveDirection = Vector3.zero; private var currentRotation : Quaternion; private var targetRotation : Quaternion; private var totalSpeed = 0.0; function Update() { currentRotation = transform.rotation; targetRotation = Quaternion.Euler(Input.GetAxis("Vertical") * thrustAngle, Input.GetAxis("Horizontal") * steeringAngle, Input.GetAxis("Horizontal") * -ZtiltAngle); currentRotation.eulerAngles.y += targetRotation.eulerAngles.y; currentRotation.eulerAngles.x = targetRotation.eulerAngles.x; currentRotation.eulerAngles.z = targetRotation.eulerAngles.z; transform.rotation = Quaternion.Slerp(transform.rotation, currentRotation, Time.deltaTime * smooth); } function FixedUpdate() { moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection.y -= downPressure * Time.deltaTime; rigidbody.AddForce(moveDirection * totalSpeed); if (Input.GetButton ("Jump")) { totalSpeed = speed + powerBoost; moveDirection.y = liftSpeed; } if (!Input.GetButton ("Jump")) { totalSpeed = speed; } } this is set up a little different than what i pointed you to earlier. i have a parent capsule with the above script attached. my graphics have a capsule collider that is below the rendered graphics. it's still a little quirky for my game but maybe it'll work for what you're doing. i don't have it in the script but you'll want to add a rigid body and freeze rotation on the parent capsule. and i am using downPressure instead instead of gravity. the script also rotates the vehicle on all 3 axes depending on the key press. it's kind of like a wave runner behaves. the input "jump" stuff makes hitting the space bar give you a speed boost that also gives a little lift. [edit: just looked at the project and i had the rig wrong. fixed above]
bah! i didn't like my explanation. here's a web player and the project it's built from. this purposely has a lot of drift in it and it's tough to judge your speed etc because there's no landmarks on the plane. so don't fall off the edge. helps if you do a 180 and hit space to slow down and reverse direction. wasd controls... http://www.oculardgi.com/hoverSamp.html
No, that's not really like what I had. On mine you could visible tell that the craft was floating above the ground, and it bobbed up and down too. I was more looking for an explanation on how to fix mine than how to work around it...
Looking a little closer, it seems that my problem is that AddRelativeForce isn't adding any force. That seems a little odd to me... any ideas? :wink: Oh, and pete, sorry if I sounded a little ungrateful there, I'm not. EDIT: Ack. Double post! Sorry.
no worries. the only thing i can think of is gravity or mass/drag might be set too high. instead of *4.5 or * 2 try * 200 and see if it moves. you can make those variables so you can tweak in the inspector. did you do a Debug.Log to see what the force is? if you have a physics material on the track, maybe friction is too high? a higher speed should overcome it too. on the one i posted you can make it higher off the ground by moving the collider down and a blob shadow would help too. yeah bobbing/drifting at idle is something i planned on adding at some point.
Well, making the force 200 certainly made something happen! It went waaay too extreme. So then I tried making it around 40, but it still didn't get the same effect. Then I made it its original value but made the craft have less mass. That resulted in the craft going way too fast and not bobbing at all. What the hell happened!? It used to work fine, and I didn't change anything... >.> Except for making the craft a prefab for use later on.
it's all about mass, drag, gravity i'd guess. just got to tweak them until their right. scale also impacts it. best to have everything scaled at 1. on the bobbing, maybe gravity at 9.81 is still pushing down harder than *4.5 is pushing up - so net you're still 5.31 down?
I just put an upward force of 100 in. It went all the way to the ground (too far) once and the second time it just fell and landed. WTF?
do you have a collider attached to your ship that's further than .8 and between the ship and the ground? the ray might be hitting that and never getting to .8?
Well, the ground has a Mesh Collider, and the Craft has a box collider perfectly centered around it. I don't think that would put .8 difference in between. It's the same collider I used to have on the ship (back when it worked). Edit, bumped the ray up to two. Works okay again! Thanks for the advice and the help!
glad you got it working. typing this when you replied. a couple alternates that i have not tested... Code (csharp): var speed = 2.0; var bobUp = 0.1; private var distance = 1.0; function Update () { var hit : RaycastHit; if (Physics.Raycast (transform.position, Vector3.down, hit)) { distance = hit.distance; } // you might want to look at something else here like slerping // seems like this would be pretty abrupt and should go to back zero when the key is released. var h = Input.GetAxis ("Horizontal"); transform.Rotate (0, h, 0); } function FixedUpdate() { moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); if (distance <= 0.8) { moveDirection.y += bobUp * Time.deltaTime; } rigidbody.AddForce(moveDirection * speed); } or if you really need to use GetKey up and down Code (csharp): var speed = 2.0; var bobUp = 0.1; private var distance = 1.0; function Update () { var hit : RaycastHit; if (Physics.Raycast (transform.position, Vector3.down, hit)) { distance = hit.distance; } // you might want to look at something else here like slerping // seems like this would be pretty abrupt and should go back to zero when the key is released. var h = Input.GetAxis ("Horizontal"); transform.Rotate (0, h, 0); } function FixedUpdate() { if (Input.GetKey("up")) { moveDirection = new Vector3(0, 0, Input.GetKey("up")); moveDirection = transform.TransformDirection(moveDirection); if (distance <= 0.8) { moveDirection.y += bobUp * Time.deltaTime; } rigidbody.AddForce(moveDirection * speed); } if (Input.GetKey("down")) { moveDirection = new Vector3(0, 0, -Input.GetKey("down")); moveDirection = transform.TransformDirection(moveDirection); if (distance <= 0.8) { moveDirection.y += bobUp * Time.deltaTime; } rigidbody.AddForce(moveDirection * speed); } }
yead i didn't test it. which did you use - the bottom one? i see i forgot some if's in fixed update... as in if (Input.GetKey(...)) and a } at the end of it. whoops. fixed above...
You should not call AddRelative force inside Update. Physics manipulations should be done from inside FixedUpdate.
>>You should not call AddRelative force inside Update. >>Physics manipulations should be done from inside >>FixedUpdate. moldy... keli's right, of course, and i should have mentioned it. instead i just posted scripts like that. your forces become frame rate dependent in update. so your game play will be different on different machines. fixed update happens in fixed time steps. so your forces act the same on different machines. it's miles per hour versus miles per x number of frames...