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 NoahKr · May 19, 2014 at 08:58 PM · programminggravitybeginnersidescroller

Need help applying gravity using C#

First off, I want to make clear that I do know you're able to use rigidbody to create gravity, but I want to do it using C#, since I want to be a game developer one day, and i would like to learn some programming beforehand.

My game is still in it's early stages, actually the only thing it currently has is a player (a simple box really) that can float around, and a floor which the player can't interact with yet. Ow, And I almost forgot to mention that it is in fact a sidescroller game.

This is the coding i currently have:

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 //Player Handling
 public int GroundSpeed = 12;
 public int Gravity = 20;



 


 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
     // Amount to Move
     float MoveHorizontal = Input.GetAxis("Horizontal") * GroundSpeed * Time.deltaTime;
 
     // Move the Player
     transform.Translate(Vector2.right * MoveHorizontal);

     // Apply gravity



 }

}

If anyone knows a solution, please try to keep it simple, I am after all still a noob in programming.

  • NoahKr

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · May 19, 2014 at 09:34 PM

How to learn

https://unity3d.com/learn/tutorials/modules

One of many ways to implement gravity, use physics

https://docs.unity3d.com/Documentation/Manual/Physics.html

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

Answer by Chris_Dlala · May 19, 2014 at 10:06 PM

You're already off to a good start. If you are after simple movement you can do something very similar to your horizontal movement:

 transform.Translate(-Vector2.up * Gravity * Time.deltaTime);

So here you take Vector.up which is a normalised vector pointing down the positive Y or (0,1,0) - that is why we take the negative of it or (0,-1,0). Scaling to time means that we now move this object down at a rate of 20 units (20 metres) per second. You can wrap the gravity code in a check as to whether you are touching the ground - because you don't want to fall when you are touching the ground.

         // If we are NOT grounded, apply gravity
         if(!IsGrounded)
         {
             transform.Translate(-Vector2.up * Gravity * Time.deltaTime);
         }

You then need a collision to check for the floor. A simply check of the Y position can simulate a base floor but later more complex collisions will be needed. Here's a brief example:

 //Player Handling
 public int GroundSpeed = 5;
 public int Gravity = 1;
 public bool IsGrounded = false;

 // Update is called once per frame
 void Update ()
 {
     // Amount to Move
     float MoveHorizontal = Input.GetAxis("Horizontal") * GroundSpeed * Time.deltaTime;
     
     // Move the Player
     transform.Translate(Vector2.right * MoveHorizontal);

     CheckForGround();

     // If we are NOT grounded, apply gravity
     if(!IsGrounded)
     {
         transform.Translate(-Vector2.up * Gravity * Time.deltaTime);
     }
 }

 void CheckForGround()
 {
     if(transform.position.y < 0)
     {
         // Cache the position
         Vector3 position = transform.position;
         // Set Y component to floor level
         position.y = 0;
         // Assign new position
         transform.position = position;
     }
 }

In this example the floor is at 0 in the Y, set the two 0's to your floor's Y position. I hope this is enough to get you started. And hope it wasn't too much/fast.

This implementation gives a constant falling/movement speed. Ideally, you would store a velocity vector and apply acceleration/deceleration to it so that velocity can change over more than one frame.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

How to start in Unity? 1 Answer

Best point to start teaching programming with Unity? 1 Answer

What is the best way to learn Unity3D? 6 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