- Home /
Trying to use a derived Function with C#
Hi, it's all about my BounceBack() Function! In my Teammate Class, my Teammate ist bouncing the Ball back pefectly with a Lerp. But when i call this Function out of my Wall Class, the Ball is not bouncing back from the Wall, its kinda shaking in a weird way.
Some Informations: The static Variables are needed, because every class is calculating with one unique Position. ballCollision is not static, because every Class has it's own Instance of the Variable. The passWay Variable is calculated once when the Ball hits a Teammate or the Wall.
I don't know what to do, i've tried a lot of things like writing a own Function with the same content but still the same! i Think there is a Problem with the passWay or ballPos.. This ist my first game and my first Question here =) I Hope someone can help me. Greetings from Germany
Here's the Code with just the important Functions/Variables
public class ball : MonoBehaviour {
protected static Vector3 ballPos;
protected static Vector3 passWay;
protected bool ballCollision;
void Update () {
ballPos = transform.position;
}
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Teammate" || collision.gameObject.tag == "Wall") {
passWay = new Vector3 (player.playerPos.x, 0, player.playerPos.z + 4);
}
}
}
public class player : ball {
public static bool ballHave
public static bool passRight;
public static bool passLeft;
}
public class teammate : player {
void Update () {
BounceBack ();
}
public void BounceBack () {
if (ballCollision && !player.ballHave) {
Ball.transform.position = Vector3.Lerp (ballPos, passWay, Time.deltaTime *3);
passRight = false;
passLeft = false;
}
}
public class wall : teammate {
void Update () {
BounceBack ();
}
void OnCollisionEnter (Collision collision) {
if (collision.gameObject.tag == "Ball") {
ballCollision = true;
}
}
}
Answer by Spinnernicholas · Oct 07, 2014 at 05:23 PM
You are using Lerp the wrong way.
All Lerp does is take a percentage and return a fixed vector. Giving it 0 returns the start. 1 returns the end. .5 returns half way between the beginning and the end.
So,
You will have to determine how long the bounce will take.
Accumulate the time that has elapsed since the bounce started.
And finally, divide that by the total bounce time and pass it to lerp.
I don't have time to find an example at the moment, but if you are still having trouble later, I will try and help more. There may be an answer on this site on how to use lerp, try searching for it.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Access local method variable in derived class 2 Answers
Inheritance structure 0 Answers
How to pass parameters to function that is stored in variable? 1 Answer
function that returns multiple variables (in this case, lists) 1 Answer