- Home /
The position of my player always returns to (0, 0, 0)
Hey guys I’m having an issue with Unity. I already looked for an answer from someone else who may have had the same issue but I couldn’t find one. So basically here 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 on the computer and it works perfectly fine but 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 :)
If you don't post the script you wrote, it is unlikely anyone can help you fix it. :p
Answer by megabrobro · May 02, 2018 at 02:46 PM
somewhere in your code/project you must be reassigning the value for position to Vector3.zero (0,0,0), after the point where you have set it to -1.3 on x (or at very least the x value is being set back to zero). Without seeing code its hard to help, but maybe you can make a variable of type Vector3 called 'startPosition' and when your player is instantiated be sure to set it to that position after the players been instantiated
Answer by tormentoarmagedoom · May 02, 2018 at 02:58 PM
Good day.
We need more information to help you. post the code, the inspector screenshot.. something. How you initiate the game? the ball is always theer? or is instantiated?
IS clear the code is making the ball to be in that position, why dont just change its position once started?
Answer by Jake33011 · May 02, 2018 at 03:42 PM
Alright I’ll post the code in a few hours when I get back home thanks
Answer by Jake33011 · May 02, 2018 at 04:28 PM
```cs using UnityEngine; using System.Collections; using System.Collections.Generic; using System;
public class PlayerController : MonoBehaviour { private const float TUBE_DISTANCE = 1.5f; // TO CHANGE !
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>();
}
// 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);
}
} ```
So this is the script on my player
```cs using UnityEngine; using System.Collections; using System.Collections.Generic; using System;
public class CameraMotor : MonoBehaviour { 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);
}
} ```
And this the script on the camera following the player
As far as I can tell, your Lerp in the player script is not working correctly. You should check this out.
Your answer
Follow this Question
Related Questions
Networked position interpolation implementation (not Dead Reckoning)? 1 Answer
Help!How to validate the client position in headless unity? 0 Answers
how to make an object move like the one it is in contact(moving using Animator) with in unity? 1 Answer
Ignoring the transform.y position on movement 2 Answers
Problem with a motion script 0 Answers