Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Tyler 2 · Nov 26, 2010 at 06:37 AM · rotationmovement

Rotation and force?

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by duck · Nov 26, 2010 at 07:09 AM

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);

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Player Controller Rotation Script Help 2 Answers

How do I create a spaceship 1 Answer

Rotate character to the moving direction problems? 2 Answers

How to make a RigidBody not go into ground when tilted foward? 2 Answers

Enemy controller.move script slows down and speeds up. How to get it consistent? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges