- Home /
Making a sphere jump on specific surfaces only
Hi,
I'm having some problems with my 2D Plattformer.
I have a Sphere (imported from Blender) and would like it to only be able to jump of specific points of the ground or other plattforms. My problem is that right now it can jump off anything, which, as an example, includes the sides of plattforms. Even just colliding with the side o the plattform sets my isGrounded bool to true, granting yet another jump in the air (I unintentionally implement Doublejump and decided to keep it in the game). This is my first game, but I do have quite a bit of Unity/C# knowledge.
Here is my my playerMovement scrip (C#):
using UnityEngine; using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class playerMovement : MonoBehaviour {
public Rigidbody rb;
public Vector3 jump;
public float movementForce = 1000f;
public float jumpForce = 7f;
public bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void FixedUpdate () {
if (Input.GetKey("d"))
{
rb.AddForce(movementForce * Time.deltaTime, 0, 0);
}
if(Input.GetKey("a"))
{
rb.AddForce(-movementForce * Time.deltaTime, 0, 0);
}
if (rb.position.y < -0.08323183f)
{
FindObjectOfType<Gamemanager>().EndGame();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void OnCollisionStay()
{
isGrounded = true;
}
}
How can I tell my Sphere to only be able to jump off the top of an object?
Thanks in advance
Answer by ggmini · Apr 05, 2018 at 01:55 PM
Hello again,
I did some more resarch and ended up fixing the problem using Raycasts. Here is the code:
public class playerMovement : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Space) && Grounded()) {
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
}
}
bool Grounded()
{
return Physics.Raycast (transform.position, Vector3.down, coll.bounds.extents.y + 0.1f);
}
}
Answer by Topthink · Apr 04, 2018 at 02:10 PM
Thinking out loud mostly, but you might try adding a 2nd collider and changing the center/size so that it can be accessed only from the "top" area where you want to jump from. I don't know in practice how difficult that would be but it seems as though it would at least be possible.
Answer by davidcox70 · Apr 04, 2018 at 02:35 PM
Your OnCollisionStay() function can be used to give you more information about what your object has collided with. So if you change the function to OnCollisionStay(Collision colDetails), then colDetails can give you that information. For example, you could look at colDetails.contact[0].normal to see the normal direction of the first point of contact. If it is straight up, then your object hit a horizontal platform and you can set isGrounded to true. The docs about contact points are here if you would like to investigate further.
I understand what you are saying, but how do you implement it, if you don't $$anonymous$$d me asking.
Answer by tormentoarmagedoom · Apr 04, 2018 at 02:58 PM
Good day.
You should read the oficial unity tutorial about NavMesh. Which simplify all movement issues as you need.
Your answer
Follow this Question
Related Questions
How to only Jump on Ground Items 1 Answer
Distribute terrain in zones 3 Answers
C#: Jump function, grounded stays True 1 Answer
Still constant jumping even with raycasting. 1 Answer
Multiple Cars not working 1 Answer