This is likely an easy one. I can see the issue but am unsure what the most efficient way to fix it would be. I am basically making an endless run type prototype.
I have the player moving forward on the z axis over time and when the space bar is pressed I want the player to move a distance to the left or right, but they movement in the update is conflicting.
I will include the script below. This script is attached to the player object.
using UnityEngine;
using System.Collections;
using System;
public class PlayerMove : MonoBehaviour
{
//////////////////////////////////////////////////////////////////////////////
// //
//Variables //
// //
//////////////////////////////////////////////////////////////////////////////
public enum SIDE { Left, Right }
static public PlayerMove instance;
public float speed;
private Vector3 newPos;
private SIDE side;
private bool mbMagDown = false;
private Vector3 moveVector = new Vector3(0, 0, 1);
//////////////////////////////////////////////////////////////////////////////
// //
//Unity Functions //
// //
//////////////////////////////////////////////////////////////////////////////
void Awake()
{
instance = this;
}
void Start()
{
transform.position = new Vector3(-2.5f, 1.5f, 0);
newPos = transform.position;
side = SIDE.Left;
}
void Update()
{
// Move forward over time.
transform.Translate(moveVector * speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
if (side == SIDE.Left)
{
newPos = new Vector3(2.5f, transform.position.y, transform.position.z);
side = SIDE.Right;
}
else
{
newPos = new Vector3(-2.5f, transform.position.y, transform.position.z);
side = SIDE.Left;
}
}
// SUPPOSED to move player to either side.
transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * 10);
}
//////////////////////////////////////////////////////////////////////////////
// //
//Custom Functions //
// //
//////////////////////////////////////////////////////////////////////////////
}
↧