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
0
Question by Dakota · Oct 09, 2010 at 07:19 AM · gravityaccelerationracing

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!

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

0 Replies

· Add your reply
  • Sort: 

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

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


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