But when I then try and add a force in the forward or backward direction, the object is not moving in the object's local forward direction, but instead, in the same forward direction as before. I then use the left arrow key to rotate the object left, which also works. Force and Velocity both are component of Physics. In this blog I have mentioned, how we can make object move by adding force. By adding Force by using AddForce () method. Applied Force is calculated in FixedUpdate or by explicitly calling the Physics.Simulate method. There are two methods to make object move in unity: By changing its Transform (Position). Specifying the ForceMode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change. Force is applied continuously along the direction of the force vector. Here is a gif of what happens.Īs you can see, initially, moving forward and backward using the up and down arrow keys works. Description Adds a force to the Rigidbody. Rotating left and right seems to work, but moving forwards and backwards seems to only work in the world coordinate space for some reason. The object will be accelerated by the force according to the law force mass x acceleration - the larger the mass, the greater the force required to accelerate to a given speed. Also, The left and right arrows should simply rotate the player-object (changing the forward direction in its local coordinate space). The force is specified as two separate components in the X and Y directions (there is no Z direction in 2D physics). I would expect for this code to make my Player object move forward in its local coordinate space when I hold the up-arrow on the keyboard, and move backward in the same space when I hold the down-arrow. Start is called before the first frame updateĭebug.Log(string.Format("Transform.rotation is ", )) ĭebug.DrawRay(transform.position, transform.forward, Color.yellow) ĬarRigidbody.AddRelativeForce(transform.forward * 10) ĬarRigidbody.AddRelativeForce(transform.forward * -10) Public class PlayerScript : MonoBehaviour Here is this script: using System.Collections I have a Player object, which looks like this:Īs you can see, I have a Rigidbody2d component attached to this object, as well as a script, called "PlayerScript". I am building a very simple car-driving game in Unity.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |