- Home /
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
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
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.
Your answer
Follow this Question
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