- Home /
wrong Vector Assignment
Hello everyone, while I was making my first simple game (Block Breaker Game) in unity I have faced this problem which is strange. My problem is with this code :-
public class BallMovementManager : MonoBehaviour {
public StrikerMovementManager strikermanager;
private Vector3 BallPosition;
// Use this for initialization
void Start () {
BallPosition = this.transform.position - strikermanager.transform.position;
print (BallPosition);
}
// Update is called once per frame
void Update () {
BallPosition= BallPosition + strikermanager.transform.position;
this.transform.position=BallPosition;
print (BallPosition);
}
}
all I wan't is that I need the ball to be centered at the surface of the Striker and this code gives me wrong vector all I've done is that I've subtracted the two vectors (Ball Vector and Striker Vector) to get the difference of the Y-Coordinate and then I added the vector of the Striker into the calculated Y-coordinate so that I've the ball centered at the Striker but that doesn't make sense !
I've solved my problem by editing the code as follows :-
void Update () {
this.transform.position= BallPosition + strikermanager.transform.position;
print (BallPosition);
}
}
that worked perfectly, but I wan't to know why the first code is wrong ?
Thanks a lot in advance.
Answer by ShadyProductions · Aug 08, 2017 at 11:21 AM
Because you keep increasing ballposition
BallPosition= BallPosition + strikermanager.transform.position;
this.transform.position=BallPosition;
Your answer
![](https://koobas.hobune.stream/wayback/20220612132232im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity C# lean script not working properly 1 Answer
Unloading Broken Assembly | How to solve this !? 2 Answers
EventTrigger PointerEnter 1 Answer