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 Foolsbry · Nov 20, 2014 at 08:28 AM · vector3gravitycarrunner

Gravity Problems on Rigidbody with new Vector3

Hey there, I'm currently making an endless runner style game where you control a car. The car itself never moves on the z-axis, instead the road is sent towards the car. I have the car moving on just the x-axis using most of the script from the Space Shooter tutorial from the Unity website, however I'm having a bit of trouble.

Here is the code,

 public class SimplePlayerControl : MonoBehaviour 
 {
     public float speed;
     public float turnAngle;
 
     void FixedUpdate () 
     
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
         rigidbody.velocity = movement * speed;
 
         
         rigidbody.rotation = Quaternion.Euler (0.0f, rigidbody.velocity.x * turnAngle, 0.0f);
     }
 }

Now it's almost working fine except when the car goes over jumps that it encounters, it comes down very slowly. I'm assuming it's because of what I've defined with the new Vector3 (movement).

If I change the code to;

Vector3 movement = new Vector3 (moveHorizontal, -1.0f, 0.0f)

The car slams against the ground and doesn't move on the x-axis very easily. Also when it goes over a jump it descends too quickly. I'd prefer it if it flew threw the air and had a bit of hang-time.

Is there anything I can replace in the code so the car doesn't look to the new Vector3 for gravity, and instead uses the rigidBody mass?

Thanks for your time.

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 Baste · Nov 20, 2014 at 08:56 AM

I would suggest that you set only the x-coordinate of your car's velocity, like so:

 Vector3 velocity = rigidbody.velocity;
 velocity.x = Input.GetAxis(Horizontal);
 rigidbody.velocity = velocity;

This allows the gravity to move the car without interference.

If that's not enough, and the falling is still to slow, you'll want to start using raycasts to check if the car's on the ground or not, and add a negative vertical movement when the car's in the air.

Comment
Add comment · Show 2 · 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 Foolsbry · Nov 21, 2014 at 12:11 AM 0
Share

Awesome thanks for the tip! I tried adding that in however and now the car barely moves at all and if it does move it moves on the z-axis. Any thoughts?

I really appreciate the time you took to help, by the way!

avatar image Foolsbry · Nov 21, 2014 at 03:51 AM 0
Share

I've changed the code now, it works much better.

     public float xSpeed;
     public float turnAngle;
     private float xInput;
     private float x$$anonymous$$ove;
 
 
     void Update()
     
     {
             xInput = Input.GetAxis ("Horizontal");
             x$$anonymous$$ove = xInput * xSpeed;
     }
 
     void FixedUpdate () 
     
     {
         rigidbody.velocity = new Vector3(x$$anonymous$$ove, -5f, 0.0f);
         rigidbody.rotation = Quaternion.Euler (0.0f, rigidbody.velocity.x * turnAngle, 0.0f);
 
     }

I got it from another tutorial for a game similar to what I want to make. $$anonymous$$y problem now is that the car won't rotate when going over jumps, it stays horizontal as it climbs the ramp.

Its because of the rigidbody.rotation line of course, but what values can I use ins$$anonymous$$d of 0.0f to make it so it behaves normally with the ramps? I tried putting in rigidbody.rotation.y and rigidbody.rotation.z but it makes it do the same thing.

I got that line of code from the space shooter tutorial, it's to make the car turn slightly as you move left or right.

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

Endless Runner Car Physics Issues (Again) 1 Answer

Character Controller Not Allowing Gravity 4 Answers

Need very specific help with Quaternions 1 Answer

Rotation according to gravity in a 3 dimensional space 0 Answers

Car Front Tires rotation clamped between 270° - 90° 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