- Home /
Anti-Gravity Vehicle Controls and Acceleration
Hi there, I've been haveing some trouble with controlling my vehicle. I'm working with an anti-gravity craft that can't fly, but doesn't rely on friction with the ground for movement. I almost have my controls working, but what I really need is to be able to rotate the ship along the ship's Y and Z axes with MouseX, and make its speed gradually accelerate as Input Vertical postive is held down, as well as use inertia to prevent instant stopping, and create gradual slowdowns. I've been using a mixture of an edited MouseLook, and another edited script from the site, but there must be a way to accomplish these tasks in a single script. For reference I have included the two scripts I'm using.
// A very simplistic car driving on the x-z plane. var speed = 10.0; var rotationSpeed = 100.0; static var acceleration : float; static var niceMouseDelta : float ;
function Update () { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 var translation = Input.GetAxis ("Vertical") speed; var rotation = Input.GetAxis ("Mouse X") rotationSpeed;
// Moves the rigidbody forward along its own z-axis function FixedUpdate () { rigidbody.AddRelativeForce Vector3.forward * 0.1 ; }
// Make it move 10 meters per second instead of 10 meters per frame... translation = Time.deltaTime; rotation = Time.deltaTime;
// Move translation along the object's z-axis transform.Translate (0, 0, translation); // Rotate around our y-axis transform.Rotate (0, 0, 0); }
And...
using UnityEngine;
using System.Collections;
/// MouseLook rotates the transform based on the mouse delta. /// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character: /// - Create a capsule. /// - Add a rigid body to the capsule /// - Add the MouseLook script to the capsule. /// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it) /// - Add FPSWalker script to the capsule
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform. /// - Add a MouseLook script to the camera. /// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.) [AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXAndY; public float sensitivityX = 15F; public float sensitivityY = 15F; public float sensitivityZ = 15F;
public float minimumX = -360F; public float maximumX = 360F;
public float minimumY = -60F; public float maximumY = 60F;
public float minimumZ = -90; public float maximumZ = 90;
float rotationX = 0F; float rotationY = 0F; float rotationZ = 0F;
Quaternion originalRotation;
void Update () { if (axes == RotationAxes.MouseXAndY) { // Read the mouse input axis rotationX += Input.GetAxis("Mouse X") sensitivityX; rotationY += Input.GetAxis("Mouse Y") sensitivityY; rotationZ += Input.GetAxis("Mouse X") * sensitivityZ;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
rotationZ = ClampAngle (rotationZ, minimumZ, maximumZ);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
Quaternion zQuaternion = Quaternion.AngleAxis (rotationZ, Vector3.up);
transform.localRotation = originalRotation * xQuaternion * yQuaternion * zQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
else;
{
rotationZ +=Input.GetAxis("Mouse X") * sensitivityZ;
rotationZ = ClampAngle (rotationZ, minimumX, maximumX);
Quaternion zQuaternion = Quaternion.AngleAxis (rotationZ, Vector3.up);
transform.localRotation = originalRotation * zQuaternion;
}
}
void Start () { // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; originalRotation = transform.localRotation; }
public static float ClampAngle (float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; return Mathf.Clamp (angle, min, max); }
}
Thank you!
Your answer
Follow this Question
Related Questions
Why does a rolling ball not accelerate past a slow speed? 1 Answer
Sci Fi Racing Controls and Physics? 0 Answers
Cheap gravity script 2 Answers
Input.gyro.gravity and Input.acceleration can't support wp8. 1 Answer
How do i make the Impulse i give to my Player not be lowered by acceleration of gravity? 1 Answer