- Home /
Modifying height directly on a Rigidbody Object without gravity
I am making a UFO spaceship type object controller, and one of the controls is if I press the Space key, it raises the height of the ship, and pressing Control lowers the height.
The spaceship currently has a Rigidbody component component with "Use Gravity" disabled, and I am manipulating its Transform.position variable directly inside of code that looks basically like this:
throttle = Input.GetAxis("Vertical");
height += throttle * verticalSpeed; //height is a Property which maps to transform.position.y
And it is working quite nicely in general, but I've read that I should not be modifying Transforms on objects with a Rigidbody component, so I am wondering what the proper way would be to do this?
I need the object to have a Rigidbody because I want it to have collisions. Is there another way to achieve physical collisions without a rigidbody? Or is this a perfectly ok way to do this?
Ins$$anonymous$$d of using transform to change the position, you can use rigidbody.position to change it. It is similar to transform.position, but with transform.position, the rigid body will give incorrect collisions as it appears to teleport ins$$anonymous$$d of move. You can use rigidbody.$$anonymous$$ovePosition() also.
I was staring at the script reference for Rigidbody and somehow completely missed both $$anonymous$$ovePosition and rigidbody.position. So obvious now that I know, thanks!
Answer by PumpkinHead · Apr 25, 2014 at 02:41 PM
Don't know if this is right but Float up;
Throttle = Input.GetAxis("Vertical");
Height + up += th...........
Don't know if that's what you are looking for but fort Il give it a go as a noob to unity and coding
I'm sorry PumpkinHead, that code is wrong and will result in an error. If you are a noob and don't know what you're doing, please do not post answers.
Your answer
Follow this Question
Related Questions
Detect collision/trigger between two body without rigidbody? 3 Answers
How to test for a collision with a Terrain 1 Answer
How to properly move a rigidbody/collider? 2 Answers
Is there a solution to when colliders bypass? 2 Answers
Physics - increase gravity speed without making balls fall through planes 1 Answer