- Home /
My player returns to default position when pressing play
Hey guys I have a problem. I already looked for an answer from someone else who may have had the same problem but I couldn’t find one. So this is what happens : I wrote a script to make my ball (Player) move always up and go on one side or the other using the left and right keys and it works fine. The problem is that I want my ball to have an original position of something like (-1.3, 0, 0) but when I hit play the ball returns to 0 on the x axis while going up and I can’t figure out why. I had kind of the same problem for my camera-following-player script but then I could resolve it just by setting the transform.position.x to 0, and can’t do that for player otherwise it won’t go right or left anymore. By the way there is no animation on the ball and I just have a character controller in addition to the script on the player. So please if anyone knows how to resolve that problem it would be great thanks :)
This is the script on the player :
private const float TUBE_DISTANCE = 1.5f;
private CharacterController controller;
private float verticalVelocity;
private float speed = 2.0f;
private int desiredTube = 1; // 0 = Left, 1 = Middle, 2 = Right
private void Start()
{
controller = GetComponent<CharacterController>();
transform.position = new Vector3(-1.3, 0, 0);
}
// Update is called once per frame
private void Update()
{
// Gather the inputs in which tube we should be
if (Input.GetKeyDown(KeyCode.LeftArrow))
MoveTube(false);
if (Input.GetKeyDown(KeyCode.RightArrow))
MoveTube(true);
// Calculate where we should be in the future
Vector3 targetPosition = transform.position.y * Vector3.up;
if (desiredTube == 0)
targetPosition += Vector3.left * TUBE_DISTANCE;
else if (desiredTube == 2)
targetPosition += Vector3.right * TUBE_DISTANCE;
// Let's calculate our move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
moveVector.y = speed;
// Move the ball
controller.Move(moveVector * Time.deltaTime);
}
private void MoveTube(bool goingRight)
{
desiredTube += (goingRight) ? 1 : -1;
desiredTube = Mathf.Clamp(desiredTube, 0, 2);
}
And this is the script on the camera :
public Transform lookAt; // Our ball // object we're looking at
public Vector3 offset = new Vector3(0, 1.29f, -34);
private void Start()
{
transform.position = lookAt.position + offset;
}
private void LateUpdate()
{
Vector3 desiredPosition = lookAt.position + offset;
desiredPosition.x = 0;
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime);
}
Answer by Txandy · May 03, 2018 at 02:18 PM
In line 30, you don't have condition, so Update function put the player to Vector3.zero position
Vector3 moveVector = Vector3.zero; // You set here to 0
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
moveVector.y = speed; // Speed = 0
// Move the ball
controller.Move(moveVector * Time.deltaTime); // Move to 0,0,0
moveVector its 0,0,0
You need to put this in one condition.