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 RealBoy · Dec 28, 2014 at 10:51 AM · vector3addforceeuleranglescontrolracing

Car drives sideways, doesn't turn

So i am making a racing game, and i had the controls nice, but collisions didn't work. I attempted to fix this, and my character no longer falls through the ground, but now i have even more problems. could somebody help me out on where i went wrong with my driving script? as in the title, when i run the game, pressing "W" makes the car go left, and "S" makes the car go right. and now for some reason it doesn't turn at all. I also noticed that if you quickly alternate between "W" and "S", you can make the car inch forward very slightly. I'm probably making a simple mistake, but i just can't figure it out. here's my driving script:

 using UnityEngine;
 using System.Collections;
 
 public class ThirdControl : MonoBehaviour {
     // the first capital letter in the variable names mean the key that is pressed (W and S), the second capital letter means direction (North, South, East, West)
     private Vector3 DefaultRot = new Vector3 (270, 0, 0);
     public Vector3 turnA = new Vector3(0, -10 ,0);
     public Vector3 turnD = new Vector3(0, 10 ,0);
 
     public Vector3 moveSN = new Vector3(0, 0 ,-10);
     public Vector3 moveWN = new Vector3(0, 0 ,10);
 
     public Vector3 moveSS = new Vector3(0, 0 ,10);
     public Vector3 moveWS = new Vector3(0, 0 ,-10);
 
     public Vector3 moveSE = new Vector3(-10, 0 ,0);
     public Vector3 moveWE = new Vector3(10, 0 ,0);
 
     public Vector3 moveSW = new Vector3(10, 0 ,0);
     public Vector3 moveWW = new Vector3(-10, 0 ,0);
 
     void FixedUpdate() {
                 //checks if the car is rotated within these angles
                 if (transform.rotation.eulerAngles.y >= 0 && transform.rotation.eulerAngles.y <= 45 || transform.rotation.eulerAngles.y >= 315 && transform.rotation.eulerAngles.y <= 360) {
                         //moves the car forward using physics, i can't use transform.Translate becuase i need collisions to happen
                         if (Input.GetKey ("w")) {
                                 rigidbody.AddForce (moveWN);
                         } else if (Input.GetKey ("s")) {
                                 rigidbody.AddForce (moveSN);
                         }
                 } else if (transform.rotation.eulerAngles.y >= 135 && transform.rotation.eulerAngles.y <= 180 || transform.rotation.eulerAngles.y >= 180 && transform.rotation.eulerAngles.y <= 225) {
                         if (Input.GetKey ("w")) {
                                 rigidbody.AddForce (moveWS);
                         } else if (Input.GetKey ("s")) {
                                 rigidbody.AddForce (moveSS);
                         }
                 } else if (transform.rotation.eulerAngles.y >= 45 && transform.rotation.eulerAngles.y <= 90 || transform.rotation.eulerAngles.y >= 90 && transform.rotation.eulerAngles.y <= 135) {
                         if (Input.GetKey ("w")) {
                                 rigidbody.AddForce (moveWE);
                         } else if (Input.GetKey ("s")) {
                                 rigidbody.AddForce (moveSE);
                         }
                 } else if (transform.rotation.eulerAngles.y >= 225 && transform.rotation.eulerAngles.y <= 270 || transform.rotation.eulerAngles.y >= 270 && transform.rotation.eulerAngles.y <= 315){
                 if (Input.GetKey ("w")) {
                         rigidbody.AddForce (moveWW);
                 } else if (Input.GetKey ("s")) {
                         rigidbody.AddForce (moveSW);
                 }
             //if the car's rotation reaches 360, revert it back to zero
         if (transform.rotation.eulerAngles.y > 360){
             transform.eulerAngles = (DefaultRot);
         }
         //turns the car
         if (Input.GetKey ("a")){
             rigidbody.AddTorque(turnA);
         }else
         if (Input.GetKey ("d")){
             rigidbody.AddTorque(turnD);
         }
         }}}

   



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gstier · Dec 29, 2014 at 08:19 PM

Try something like this

     void FixedUpdate() {
         Vector3 fwd = rigidbody.transform.forward;
         
         if (Input.GetKey ("w")) {
             rigidbody.AddForce (fwd * moveSpeed);
         } else if (Input.GetKey ("s")) {
             rigidbody.AddForce (fwd * -moveSpeed);
         }        
         
         if (Input.GetKey ("a")){
             rigidbody.AddTorque(Vector3.up * -turnSpeed);
         }else if (Input.GetKey ("d")){
             rigidbody.AddTorque(Vector3.up * turnSpeed);
         }
     }
Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Character Jumping 2 Answers

AddForce to the Left? 3 Answers

Transforming slowly 1 Answer

Drag to rotate with forces 0 Answers


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