- Home /
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.
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);
}
}
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
Follow this Question
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