Struggling with bounce pad in platformer game.
I have recently started experimenting with a custom character controller while following a tutorial and I am loving it so far. I have stumbled upon an issue where I can't get a bounce pad working correctly and I cant figure out why. Any help would be appreciated.
There is an error saying "Operator '+' cannot be applied to operands of type 'float' and 'Vector3'" on the line saying "player.moveDirection.y = player.moveDirection.y + (transform.up * bouncePower);".
public class BouncePad : MonoBehaviour {
PlayerController player;
public float bouncePower;
// Use this for initialization
void Awake()
{
player = GameObject.Find("Player").GetComponent<PlayerController>();
}
void OnTriggerEnter(Collider mycollision)
{
if (mycollision.gameObject.tag == "Player")
{
print("Bounce");
player.moveDirection.y = 0f;
player.moveDirection.y = player.moveDirection.y + (transform.up * bouncePower);
}
}
Answer by sparkzbarca · Jan 05, 2018 at 02:12 AM
firstly if your going to write
player.moveDirection.y = player.moveDirection.y +
you should just write.
player.moveDirection.y +=
though i assume this is kind of temporary code.
I mean saying player.moveDirection.y = 0f; then immediatly saying it equals something else, but also adding to it.
I mean this should really just be
if (mycollision.gameObject.tag == "Player")
{
print("Bounce");
player.moveDirection.y = transform.up * bouncePower;
}
that is the exact same code.
I can tell you it's saying your trying to add a single number and a vector (which is a collection of three numbers) together and basically it doesn't know which of the three numbers in the vector you want the single number added to.
I'd need to see the data type for bounce power but i'm guessing that at some point it says
Vector3 bouncePower;
in your code and your trying to take a part of a vector, it's Y value and add it to it.
you could try multiplying which is probably what you want instead of adding, but you still have the issue of
player.movedirection.y = transform.up * bouncePower
because just imagine for example bouncePower which is a x,y,z and its x = 1, y = 2, z =3; let's assume transform.up equals 0,1,0 because that's global up and whatever. it's y of 1, zero everythign else. your trying to say
player.movedirection.y = transform.up(0,1,0) * bouncePower(1,2,3)
so we can multiple two vectors and thats fine but what you end up with is
player.movedirection.y = (0,2,0) and that doesn't make sense because Y is a single number, it can't make sense of you assinging it three different numbers.
now if bouncePower is just an int. we've still got a problem because again, transform.up is a vector.
so 0,1,0 times say 2 is
0,2,0 but then we have
player.movedirection.y = 0,2,0
and were back where we started. all that is to explain the error so that you can realize why it's wrong and not just have me fix it.
to fix it.
player.moveDirection.y is a single number, you can't have vectors in it, at least not all three parts, you can try.
player.movedirection.y = (transform.up * bouncePower).y;
that will probably work, but i can't be positive without seeing all the code.
Thank you, your explanation helped me understand a lot more but unfortunately your code did not work. I walk over my bounce pad and nothing happens. The print for bounce shows up in the console so it is certainly detected.
I have since found out the code works as intended, ONLY if the bounce pad is a certain height off the ground, that is my current progress.
Possibly you have an is grounded check and you need to press the jump button or be in the air to move upwards.
Alternately your colliding into the terrain collide r maybe
Your answer
Follow this Question
Related Questions
Double Jump using character controller 0 Answers
Using gravity, CharacterController.isGrounded still is unreliable 0 Answers
Hold the jump button to jump higher with CharacterController 1 Answer
Help! Collision will not work, but all others will 0 Answers
3D side scroller following Sebastian Lague platformer controller 0 Answers