# Rotation and force?

 0 Hello, I have two questions regarding force and rotation? 1: Is there anyway to have force dependent on rotation (the object moves towards its relative forward direction, etc.) rather than vectors (my current script is this): ``````function Update() { if (Input.GetButton ("Fire1")) { rigidbody.velocity = Vector3(0,0,40); }} `````` 2: Is there any way to have an continually object rotate from -45 degrees to 45 degrees? Its for a bowling game is that helps put the questions into context. thanks. more ▼ asked Nov 26 '10 at 06:37 AM Tyler 2 1.1k ● 212 ● 247 ● 264 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 To add a force in the direction of the object's own forward direction, you can use AddForce and use transform.forward as the vector direction. This vector (along with .right and .up) has a length of 1, so you can multiply it by any value you like to give the total power of the force applied. For example: ``````var power = 40.0; function Update() { if (Input.GetButtonDown("Fire1")) { rigidbody.AddForce(transform.forward * power); } } `````` To rotate an object using physics, you generally should use the rotational equivalent of AddForce, which is AddTorque. However, if you want to precisely rotate an object from -45 to 45 degrees, this might be better done without using the physics engine (since using physics forces tends to yield imprecise results). I'm going to take a wild guess here and assume this is a power meter needle for a power gauge which goes from -45 to 45. To implement this, I would have the power range go from 0-1 while you hold the fire button down, and then add the force when you release the button. The code might look something like this: ``````// public vars (settable in inspector) var powerNeedle : Transform; var maxPower = 40.0; var powerMeterSpeed = 0.2; // private vars (for internal use) private var powerFactor = 0.0; function Update() { if (Input.GetButtonDown("Fire1")) { // start the meter at 0 when the button is first pressed powerFactor = 0.0; } if (Input.GetButton("Fire1")) { // increase power factor smoothly while button is held down powerFactor += Time.time * powerMeterSpeed; } if (Input.GetButtonUp("Fire1") || powerFactor >= 1) { // apply force when button is released, or power reaches max rigidbody.AddForce(transform.forward * maxPower * powerFactor); } // set rotation of needle gameobject: var needleAngle = Mathf.Lerp( -45, 45, powerFactor ); powerNeedle.rotation = Quaternion.Euler(0, 0, needleAngle); } `````` more ▼ answered Nov 26 '10 at 07:09 AM duck ♦♦ 41.4k ● 95 ● 152 ● 415 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x3419
x2246
x1426

asked: Nov 26 '10 at 06:37 AM

Seen: 1134 times

Last Updated: Nov 26 '10 at 06:37 AM