- Home /
How can I make an object stop all momentum and hold it's position in air?
I'm creating a 2D platform game and I have a character that I want to be able to move and jump as you'd expect in a platformer game, but then on the right surfaces be able to switch to climbing. When climbing they would not be affected by gravity and could move in all directions, not just left/right. I was able to make that work as expected, but I noticed that if the object is falling when I switch to climbing they still fall as if they were being affected by a weaker gravity. What I'd like is for the character to completely stop when the button is hit and no longer have gravity affect them at all.
Below is the script I have currently for this character that can attach to walls.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LizardScript : PlayerCharacter
{
public CatScript cat;
private float climbingSpeed = 7.0f;
private bool nearWall = false;
private bool onWall = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (GameScript.mainPlayer == "lizard")
{
startClimb();
if (onWall)
{
GetComponent<Rigidbody2D>().gravityScale = 0;
climb();
} else
{
GetComponent<Rigidbody2D>().gravityScale = 2;
baseCharacter();
}
}
}
void climb()
{
var climbing = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
transform.position += climbing * climbingSpeed * Time.deltaTime;
}
void startClimb()
{
if (Input.GetKeyDown(KeyCode.G) && nearWall == true)
{
onWall = !onWall;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Wall")
{
nearWall = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Wall")
{
nearWall = false;
onWall = false;
}
}
}
Below is the parent class for the LizardScript
above. This script is where the jumping and standard movement is handled:
public class PlayerCharacter : MonoBehaviour
{
public float speed = 5.0f;
public Vector2 jumpHeight;
public bool onGround = true;
// Start is called before the first frame update
void Start()
{
this.tag = "player";
}
// Update is called once per frame
void Update()
{
}
public void baseCharacter()
{
run();
jump();
}
public void run()
{
var move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
transform.position += move * speed * Time.deltaTime;
}
public void jump()
{
if (Input.GetButtonDown("Jump") && onGround == true)
{
GetComponent<Rigidbody2D>().AddForce(jumpHeight, ForceMode2D.Impulse);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
onGround = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
onGround = false;
}
}
}
I thought the GetComponent<Rigidbody2D>().gravityScale = 0;
in LizardScript
would've been enough, but clearly it isn't. If there's anything that I might be missing let me know! Thanks in advance for your time and help!
Have you tried setting the rigidbody2d to Is$$anonymous$$inematic? That should stop so velocity and gravity but still able to be moved via script
@Divinitize1 Thanks for the suggestion! I had not tried that, but couldn't get it to work. I did however find a way to set the rigidbody2d's velocity to zero and that worked. Which, I swear I had tried before without it working, but I must've put it in the wrong place lol
Answer by The_Egyptian · Jun 24, 2019 at 11:18 PM
I'm not 100% sure about this but I think that you problem is that GetComponent<Rigidbody2D>().gravityScale = 0;
would only make gravity = 0 but not the falling speed of you object as gravity is an acceleration so it supposed to increase the speed of the falling object every second and when you stop the gravity while the object is still falling it's speed would not be set to 0 only it's acceleration .
so you should add this line too GetComponent<Rigidbody2D> ().velocity = 0;
this would set the velocity to 0 just don't used in an update function or it would only freeze your game object