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.