- Home /
How do I make soft body stick to ground?
Hello everyone, I am making a game and I need help. I want to make my soft body stick to the ground like in the game stick with it but there are no videos on how to do that. I am using a trajectory line to move my player so I want to make it so when some one lets go of the mouse, then the character unsticks and moves. Thanks!
If the object is grounded or stuck to a wall, you could freeze its position, until the player adds a force to it.
Would I attach the script to the player or the ground though?
The player. Freezing the position needs access to the rigidbody component (which the player has), or the transform component (depending on which way you want to freeze the player).
Ok so I got it working kind of, but if I try to move, it won't unstick. do you think I should use a bool variable instead?
Answer by thegoatman989 · Jun 01, 2021 at 08:44 PM
FIXED: I finally got it working! Here's the script!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Stick : MonoBehaviour
{
bool canMove = false;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D(Collision2D collison)
{
if (collison.transform.tag != "Player")
{
if (!Input.GetMouseButtonDown(0))
{
rb.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
canMove = true;
//print ("Col");
//print ("Enter");
}
}
}
void OnCollisionExit2D(Collision2D collision)
{
if (collision.transform.tag != "Player")
{
rb.constraints = RigidbodyConstraints2D.None;
canMove = false;
//print ("Exit");
}
}
void OnCollisionStay2D(Collision2D collision)
{
if (collision.transform.tag != "Player")
{
//body.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
canMove = true;
//rigidbody2D.v = new Vector2 (0, 0);
//print ("Stay");
}
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
rb.constraints = RigidbodyConstraints2D.None;
}
}
}
Original credit goes to this person! I just modified it UWU link text