I have a marble that can jump normally, but there is one problem. If I roll the marble next to a wall and rapidly press the jump button, it can climb the wall. This game is based off of the old Indie PC game Marble Blast, but for mobile. On Marble Blast, the ball would always jump in the perpendicular direction of the ground.
So I was thinking, how can I implement something similar into my game? I am a beginner at scripting so I really need help on these kinds of things.
Here is my current C# code:
using UnityEngine;
using System.Collections;
public AudioSource JumpSound;
public float JumpStrength = 1000;
void OnCollisionStay () {
//Jump effects.
if (Input.GetButtonDown ("Jump")) {
GetComponent<Rigidbody> ().AddForce (0f, JumpStrength, 0f);
JumpSound.Play ();
}
You can see that OnCollision can give you contactpoints, which can give you the normal of the contact. You can probably use something like this to determine in what direction should you be adding your jump force.
So it would be something like “myRigidbody.AddForce(contactNormal * JumpStrength)”
You would need to filter out the contactpoints and get the one you desire. Not sure how that will be done since I forgot how contactpoints work.
You would probably compare all the contactpoint normals and just pick one depending on some certain criteria.
This grenade script from the example shown in the link you gave to me seems to be what I am looking for! It looks like the script instantiates an explosion in the direction the ground is facing.
it also could be because your script is just looking for any collision so counts contact with the wall as a collision. If you only want them to be able to jump when in contact with the ground you can test for what they are in contact with. Probably not the best way but you could tag your ground & then only enable the jump if that’s what they are touching.
fair enough (I never played Marble blast). There is a way to find the collision point vector but I haven’t used it & can’t remember where I saw it but Mr Google can probably help. If you found that you could apply the force at 90-degrees to it, you’d just have to work out how to handle multiple collision points (e.g. floor & wall - will you average the contact points & apply force?)
I’m trying to get this grenade script into my ball movement script, but it just comes up as errors.
using UnityEngine;
using System.Collections;
public AudioSource JumpSound
public float JumpStrength = 1000;
void OnCollisionStay (Collision collision) {
//Jump effects.
ContactPoint contact = collision.contacts [0];
Quaternion rot = Quaternion.FromToRotation (Vector3.up, contact.normal);
if (Input.GetButtonDown ("Jump")) {
GetComponent<Rigidbody> ().AddForce (rot * JumpStrength);
JumpSound.Play ();
}
Errors:
“Assets/Scripts/PlayerMovement.cs(53,62): error CS0019: Operator *' cannot be applied to operands of type UnityEngine.Quaternion’ and `float’”
“Assets/Scripts/PlayerMovement.cs(53,52): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3)’ has some invalid arguments”
“Assets/Scripts/PlayerMovement.cs(53,52): error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Vector3’”
It works, but my ball is jumping in the direction the ball is pointing, which is making it jump in all sorts of directions even when there isn’t even a wall.
It seems to me that the problem in question can be solved with a simple conditional statement on the jump itself. The “Collision” data will give you contact points, so I suggest looping through them and averaging the normals and then only allowing “jump” if the averaged normals are within set bounds, or just a simple “y must be at least twice x+z” or something. A hack-ish comparison would be fine at first, you can fine-tune it later with more complicated math if needed.
EDIT: Better yet, loop through the normals from the collision data and simply exclude any that are outside of the set “bounds”, then, if you have any normals left, allow the player to jump. All you need is one point “underneath” the sphere to be able to leverage a jump, right?