Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Xaos95 · Aug 17, 2017 at 08:54 AM · 2dmobilesimplecar gametop down

How to make a car move the direction it is facing?

I am making a rather simple top down 2D mobile car game and i wan't it so that the car will always move automatically and whenever i press "space" the car will slightly rotate left. So the longer i hold "space" the more the car will rotate so eventually it will make an circle.

This is the best way i could put this but i feel like it's not enough, so i am going to show you an image.alt text

So as you can see there is a lane and the car will always move automatically but if it hits any of the sides it will explode and you will lose so the player must be able to rotate left all the time. Whenever i make one lap i want the speed to increase everytime.

Any kind of help is appreciated!

skarmavbild-2017-08-17-kl-104551.png (155.5 kB)
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
Best Answer

Answer by giraffeseatwindmills · Aug 31, 2017 at 05:49 AM

Do you have any code written already? If not here is a little bit of what I would do. For this example I am going to use rigidbody physics on the car.

Here is the bit of code I wrote that should generally accomplish what you asked.

 #pragma strict
 var rigidBodyPhysics : Rigidbody;//The rigidbody physics component.
 var accelerationSpeed : int = 100;//How fast the car will accelerate.
 private var eulerAngleVelocity : Vector3;//Which way the vehicle will rotate.
 var turnSpeed : float = 30;
 
 function Start()
 {
     rigidBodyPhysics = GetComponent.<Rigidbody>();//Getting the data for the rigidbody physics component
 }
 
 function Update()
 {
     rigidBodyPhysics.AddForce(transform.forward * accelerationSpeed);//This moves the car forward by adding the force of the acceleration speed to the car.
 
     if(Input.GetKey(KeyCode.Space))//If space is pressed (this can be changed to any form of input, i.e a key, screen press, mouse click, etc.
     {
         eulerAngleVelocity = Vector3(0, turnSpeed, 0);//Set the rotation values to 0 on the X axis, turnSpeed on the Y axis, 0 and the Z axis.
         //Turn speed is is value given for the Y axis (vertical) rotation. Setting turnSpeed to a negative value will turn the opposite direction.
     }
     else
     {
         eulerAngleVelocity = Vector3(0, 0, 0);//Set the rotation values to 0 on the X axis, 0 on the Y axis, 0 and the Z axis.
     }
 
     var deltaRotation : Quaternion = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);//Set rotation values relative to the variable "eulerAngleVelocity" which is defined above.
     rigidBodyPhysics.MoveRotation(rigidBodyPhysics.rotation * deltaRotation);//Rotate the rigidbody relative to the defined values in "deltaRotation".
 }

Note that this requires you have a rigidbody component on your vehicle. It will be necessary to set proper weight and drag settings on the rigidbody component. This can be used to determine how fast the car moves / how well it controls. You can also edit the values in the code such as "turnSpeed" and "accelerationSpeed".

I hope this helps. If you have any questions feel free to ask.

Comment
Add comment · Show 1 · 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
avatar image giraffeseatwindmills · Aug 31, 2017 at 05:51 AM 0
Share

By the way, the bits of code used can be referenced here: Add Force: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

$$anonymous$$ove Rotation: https://docs.unity3d.com/412/Documentation/ScriptReference/Rigidbody.$$anonymous$$oveRotation.html https://docs.unity3d.com/ScriptReference/Rigidbody.$$anonymous$$oveRotation.html

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

187 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 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 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 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 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 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 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 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 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

Mobile Left and right move input for the player in 2D (Top Down) 0 Answers

How to make so that the player can jump on a object, but he can't stand on it? (2D) 0 Answers

How to make an object keep move when pressing a button? 0 Answers

Re sizing an object within a confined space 0 Answers

Graphics.PresentAndSync (Device.Present) on mobile devices problem 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