- Home /
2D Plane Component "Gravity"
looked around the site trying to find a understandable way to do this couldnt so i tried it out myself for the last two days been kinda stumped. I figured the simplest way to say whenever not colliding with an object tagged as a floor you would move downward, is with the OnCollisionStay function as in the code below. Yet no such luck, im not looking for pure code or anything just maybe an point in the right direction at least. The plane does not have a rigidbody btw.
var mspeed : float = .3;
var jspeed : float = 3;
var jumpnumber : int = 1;
var dbljump : boolean = true;
var player : GameObject;
var weight : float = 150;
function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "floor")
{
Debug.Log("TOUCHING GROUND");
}
}
function OnCollisionStay(col : Collision){
if(!col.gameObject.tag == "floor")
{
transform.Translate(Vector3(0,0,-weight));
}
}
function Update(){
transform.position.z=Mathf.Clamp(transform.position.z, -0,0);
if(Input.GetKey("left"))
{
transform.Translate(Vector3.left*-mspeed);
}
if(Input.GetKey("right"))
{
transform.Translate(Vector3.left*mspeed);
}
if(Input.GetKeyDown("up") && dbljump)
{
Jump();
Debug.Log("jump number"+jumpnumber);
Debug.Log("dbljump="+dbljump);
}
}
function Jump()
{
if(jumpnumber <= 1)
{
transform.Translate(Vector3(0,0,-3));
jumpnumber++;
}
else
{
dbljump=false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Jump and run on a circle (2D) 0 Answers
how to jump at fixed height but faster 0 Answers
2D - Change jump direction 1 Answer
[C#] Platform Effector 2D really glitchy 0 Answers
Changing the '2D Axis' 1 Answer