Reputation: 49
hi my character is stacking in the same position even if i move my character forward and backward here is my code so far, hope you'll help me the code below for the setting of character position is from the Load area where the x,y,z position is from the position where the character is saved
using UnityEngine;
using System.Collections;
public class MainCharacterMove : MonoBehaviour {
public Animator anim;
public GameObject main;
// Update is called once per frame
void Update ()
{
anim.SetFloat ("Directions",0);
Vector3 tempx = main.transform.position;
tempx.x = PlayerPrefs.GetFloat ("x");
main.transform.position = tempx;
Vector3 tempy = main.transform.position;
tempx.x = PlayerPrefs.GetFloat ("y");
main.transform.position = tempx;
Vector3 tempz = main.transform.position;
tempx.x = PlayerPrefs.GetFloat ("z");
main.transform.position = tempx;
if(Input.GetKey(KeyCode.W))
{
anim.SetFloat("Directions",1);
transform.Translate(Vector3.forward*2*Time.deltaTime);
}
if(Input.GetKey(KeyCode.D))
{
anim.SetFloat("Directions",2);
transform.Translate(Vector3.right*2*Time.deltaTime);
}
if(Input.GetKey(KeyCode.A))
{
anim.SetFloat("Directions",3);
transform.Translate(Vector3.left*2*Time.deltaTime);
}
if(Input.GetKey(KeyCode.S))
{
anim.SetFloat("Directions",4);
//transform.Translate(Vector3.left*2*Time.deltaTime);
}
}
}
Upvotes: 0
Views: 81
Reputation: 8193
There is so much wrong going on here i dont know where to start.
First of all Vector3 Tempx
Vector3 Tempy
Vector3 Tempz
? You only need a single Vector3 temp
which will contain all x
y
and z
.
Vector3 tempx = main.transform.position;
tempx.x = PlayerPrefs.GetFloat ("x");
main.transform.position = tempx;
Vector3 tempy = main.transform.position; // Why new var? This is not needed.
tempx.x = PlayerPrefs.GetFloat ("y"); // Tempx.x again... did you mean Tempx.y?
main.transform.position = tempx;
Vector3 tempz = main.transform.position; // Why new var? This is not needed.
tempx.x = PlayerPrefs.GetFloat ("z"); // Tempx.x again... did you mean Tempx.z?
main.transform.position = tempx;
not to mention 3 calls to main.transform.position = tempx;
in a row
I think this is what you meant:
Vector3 temp = new Vector3(PlayerPrefs.GetFloat ("x"),
PlayerPrefs.GetFloat ("y"),
PlayerPrefs.GetFloat ("z"));
main.transform.position = temp;
// or
main.transform.position += temp;
// or
main.transform.position += temp * Time.deltaTime;
Upvotes: 0
Reputation: 12258
It seems that the problem is with every Update()
loop, you are resetting your main.transform.position
to something partially set from your PlayerPrefs
- meaning even if you change that particular value in response to a keypress, by the next frame it will jump back to its initial value. I'd advise moving this code to Start()
, since that would be the correct place to initialize a position in this case.
Your logic for loading the player position from memory seems to be a bit erroneous as well - although you declare and initialize Vector3 tempy
and Vector3 tempz
, you never actually use them. Rather, you overwrite your x-value repeatedly with your x, y, and z values. (So I expect it is only the x-value of the position that gets reset each frame.)
So, taking the position-loading code out of your Update()
loop and into Start()
, then fixing the logic:
// Use this for initialization
void Start ()
{
Vector3 tempPosition = new Vector3();
tempPosition.x = PlayerPrefs.GetFloat ("x");
tempPosition.y = PlayerPrefs.GetFloat ("y");
tempPosition.z = PlayerPrefs.GetFloat ("z");
main.transform.position = tempPosition;
}
Hope this helps! Let me know if you have any questions.
Upvotes: 1