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 Le-Capitaine · May 11, 2014 at 12:35 PM · c#carwheelstransform.rotatearound

Wheels that just plain won't spin

Here's the whole code in case there's anything I missed that you might catch.

 using UnityEngine;
 using System.Collections;
 
 public class CrimsonSisters : MonoBehaviour {
 
     float thrust;
 
     private Animator sistersAnim;
     
     public GameObject[] wheels;
     Vector3[] wheelsPos;
     Vector3[] prevContact;
     float[] wheelSpin;
     float[] wheelVel;
 
     private float steering;
     private float sliding;
     private float fall;
     private float speed;
     public float fallThreshold;
     public float speedThreshold;
 
     public float grip;
 
     public GUIText kmh;
 
     public GameObject dirt;
 
     public Camera[] cameras;
     int currentCam = 1;
 
     WholeGame game;
 
     void Start () {
         wheelSpin = new float[4];
         wheelVel = new float[4];
         wheelsPos = new Vector3[4];
         sistersAnim = this.GetComponent<Animator> ();
         game = GameObject.Find ("Supercontroller").GetComponent<WholeGame> ();
 
         for (int i = 0; i < 4; i++) wheelsPos[i] = wheels[i].transform.localPosition;
     }
 
     void OnCollisionStay (Collision other) {
         for (int i = 0; i < 4; i++) {
         if (other.contacts[0].thisCollider == wheels[i]) {
             wheelSpin[i] = transform.InverseTransformDirection (rigidbody.velocity).z * 2;
             if (other.gameObject.tag == "Dirt" && rigidbody.velocity.magnitude > 0.5f) Instantiate (dirt, other.contacts [0].point, transform.rotation);
             float wheelCollision = (other.contacts [0].point - prevContact[i]).magnitude / speedThreshold;
             wheels[i].transform.localPosition = new Vector3(wheelsPos[i].x, Mathf.Lerp (wheels[i].transform.localPosition.y, wheelsPos[i].y, wheelCollision), wheelsPos[i].z);
             prevContact[i] = other.contacts [0].point;
             }
         }
     }
 
     void FixedUpdate () {
         steering = Mathf.Lerp (steering, game.steering, Time.deltaTime * 15 / (rigidbody.velocity.magnitude / 10 + 1));
         sliding = Mathf.Lerp (sliding, game.sliding, Time.deltaTime * 15);
         fall = Mathf.Lerp (fall, rigidbody.velocity.y / fallThreshold, Time.deltaTime * 100);
         speed = Mathf.Lerp (speed, rigidbody.velocity.magnitude / speedThreshold, Time.deltaTime * 20);
         grip = Mathf.Lerp (0f, 1f, speed);
         
         sistersAnim.SetFloat ("Steering", steering * 0.5f + 0.5f);
 
         thrust = thrust * 0.95f;
         thrust += game.accel * 2;
         thrust -= Mathf.Abs (sliding) * 0.4f;
         thrust = Mathf.Clamp (thrust, -10, 100);
 
         for (int i = 0; i < 4; i++) {
             wheels[i].transform.RotateAround (wheels[i].transform.position, wheels[i].transform.up, wheelSpin[i]);
             wheelSpin[i] = wheelSpin[i] * 0.95f;
             wheels[i].transform.localPosition = new Vector3(wheelsPos[i].x,
                                                             Mathf.SmoothDamp (wheels[i].transform.localPosition.y, wheelsPos[i].y-0.05f, ref wheelVel[i], 0.1f),
                                                             wheelsPos[i].z);
             wheels[i].transform.localPosition = new Vector3(wheelsPos[i].x,
                                                             Mathf.Clamp(wheels[i].transform.localPosition.y, wheelsPos[i].y-0.05f, wheelsPos[i].y),
                                                             wheelsPos[i].z);
             }
 
         // The important part
         wheels [0].transform.localRotation = Quaternion.Euler (wheels [0].transform.localRotation.eulerAngles.x,
                                                                wheels [0].transform.localRotation.eulerAngles.y,
                                                                Mathf.Lerp (-45, 45, steering * 0.5f + 0.5f));
         wheels [1].transform.localRotation = Quaternion.Euler (wheels [1].transform.localRotation.eulerAngles.x,
                                                                wheels [1].transform.localRotation.eulerAngles.y,
                                                                Mathf.Lerp (-45, 45, steering * 0.5f + 0.5f));
         wheels [2].transform.RotateAround (wheels [2].transform.position, wheels [2].transform.up, thrust);
         wheels [3].transform.RotateAround (wheels [3].transform.position, wheels [3].transform.up, thrust);
         
         rigidbody.AddForce (Vector3.down * 15);
 
         rigidbody.angularVelocity = Vector3.zero;
 
 
 
         kmh.text = (rigidbody.velocity.magnitude * 3).ToString ("F0") + " km/h";
         dirt.transform.localScale = new Vector3 (rigidbody.velocity.magnitude,
                                                  rigidbody.velocity.magnitude,
                                                  rigidbody.velocity.magnitude) * 0.1f;
     }
 }

I mean for the back wheels to rotate and drive a car and the front wheels to rotate left and right 45 degrees so as to steer it. But no matter what, nothing rotates at all whether I'm using transform.RotateAround or directly setting the wheels' rotation. I made sure not to have the wheels use a skinned mesh renderer, the wheels are properly referenced in the inspector and adding a Debug.Log() line referencing thrust/steering reveals that they're not the problem. I do see, however, that the rotation doesn't move at all in the inspector at runtime.

Am I missing something obvious?

edit: I might as well add that the wheels are childed to the car, and that they do spin when de-childed. In which case they don't follow the car and collide with it, so it's something I really don't want to do.

Comment
Add comment · Show 2
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 Addyarb · May 13, 2014 at 09:06 PM 0
Share
  game = GameObject.Find ("Supercontroller").GetComponent<WholeGame> ();

Can you explain what's happening there?

avatar image Le-Capitaine · May 14, 2014 at 08:01 AM 0
Share

This function's syntax is pretty odd. The "supercontroller" is a controller I'm using that handles game-wide stuff across scenes (so far just input). This line gets it so the car gets values from it at runtime (input again). The supercontroller is in the scene and input functions correctly.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WheelCollider rotates mesh wrong 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make a functional steering wheel for a car game? 2 Answers

tachometer??? Help!! 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