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 Zarack · Dec 19, 2012 at 02:13 AM · rotationrigidbodytransformaddforceforce

AddForce not influenced by rotation

Hey folks, I've been scouring all kinds of related questions and none of them have had my answer.

I'm currently trying to drive a cube toward the local Z-axis while turning. The code has no problem pushing you in the Z-axis once both fingers are pressed onto the screen, but when I lift one the cube turns, but the force does not follow. It continues to head in the direction it initially intended on. I'm thinking it may have something to do with my if-statements? I've been working on this all day to no avail.

The default values may need changing.

 using UnityEngine;
 using System.Collections;
 
 public class ShipController : MonoBehaviour {
     public float speed = 6.0F;
     public float gravity = 20.0F;
     public float handling = 50.0F;
     public GameObject Ship;
     float regionWidth = Screen.width / 2;
     void Update() {
         if (Input.touchCount == 1 & Input.mousePosition.x < regionWidth) {
             Ship.rigidbody.transform.Rotate(Vector3.up * -handling);
         }
         else if (Input.touchCount == 1 & Input.mousePosition.x < regionWidth * 2) {     
             Ship.rigidbody.transform.Rotate(Vector3.up * handling);
         }
         else if (Input.touchCount == 2) 
             Ship.rigidbody.AddForce(transform.forward * speed);
         
     }
 }

I wanted to also mention that my cube has gravity turned off, and its rotation is frozen for X,Y, and Z.

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
Best Answer

Answer by aldonaletto · Dec 19, 2012 at 02:37 AM

Yes, the ifs are causing your problem: force is being applied only when both fingers are on the screen, thus lifting one finger will rotate the object without applying any force. You could change a little the logic, and also improve a little the performance by caching values in local variables:

 void Update(){
   int q = Input.touchCount; // cache touchCount in q
   if (q == 1){ // if one finger only...
     float x = Input.mousePosition.x; // cache mouse X in x
     if (x < regionWidth) { // rotate left?
       Ship.transform.Rotate(Vector3.up * -handling * Time.deltaTime);
     }
     else if (x < regionWidth * 2){ // rotate right?
       Ship.transform.Rotate(Vector3.up * handling * Time.deltaTime);
     }
   }
   if (q >= 1){ // if any touch, move the ship forward
     Ship.rigidbody.AddForce(transform.forward * speed);
   }
 }
Comment
Add comment · Show 1 · 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
avatar image Zarack · Dec 20, 2012 at 06:23 PM 0
Share

This is super close to what I need. You were correct about the turning left and right! The cache was a great idea, too. However, the issue kinda remains with the last 'if' statement. I don't want the ship to be propelled forward if the user only places one finger on the screen. I really just wanted the force to carry over to the new rotation. Using the code you gave me, I came up with this:

using UnityEngine; using System.Collections;

public class ShipController : $$anonymous$$onoBehaviour { public float speed = 6.0F; public float gravity = 20.0F; public float handling = 50.0F; public GameObject Ship; float regionWidth = Screen.width / 2; Vector3 still = Vector3.zero; void Update(){ int fingers = Input.touchCount; // cache touchCount in q Vector3 movement = Ship.rigidbody.GetPointVelocity(transform.position); if (fingers == 1){ // if one finger only... float touchPoint = Input.mousePosition.x; if (touchPoint < regionWidth & movement == still) { // rotate left Ship.transform.Rotate(Vector3.up -handling Time.deltaTime); } else if (touchPoint < (regionWidth 2) & movement == still){ // rotate right Ship.transform.Rotate(Vector3.up handling Time.deltaTime); } else if (touchPoint < regionWidth) { Ship.transform.Rotate(Vector3.up -handling Time.deltaTime); Ship.rigidbody.AddForce(transform.forward (speed / 3)); } else if (touchPoint < regionWidth 2) { Ship.transform.Rotate(Vector3.up handling Time.deltaTime); Ship.rigidbody.AddForce(transform.forward (speed / 3)); } } if (fingers > 1){ // if any touch, move the ship forward Ship.rigidbody.AddForce(transform.forward * speed); } } }

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make object tip over 1 Answer

Rigidbody.AddForce with Raycast? 1 Answer

how to tilt boat on turning 2 Answers

Rigidbody spins out of control when colliding into corner 0 Answers

Is it advisable to apply forces and rotations on a same object? 2 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