Question by
xfalconsx2 · Feb 20, 2020 at 02:08 PM ·
c#movement script
Object Won't move down on y axis until It moves up on the y axis or on any way of x axis
I want a cube to move in a 3x3x1 area, but when I run the Project and the first Input is 's', the object won't move down. After 'a', 'w', or 'd' the script works perfectly. The script has other problems, but I have figured out a workaround already. The cube starts at x=0 y=1 z=0. (Using "Unity 2019.3.1f1 Personal")
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
Vector3 tempPos;
// Start is called before the first frame update
void Start()
{
Vector3 tempPos = transform.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("s") && tempPos.y > 0 )
{
tempPos.y -= 1f;
transform.position = tempPos;
}
if (Input.GetKeyDown("w") && tempPos.y < 2)
{
tempPos.y += 1f;
transform.position = tempPos;
}
if (Input.GetKeyDown("a") && tempPos.x > -1)
{
tempPos.x -= 1f;
transform.position = tempPos;
}
if (Input.GetKeyDown("d") && tempPos.x < 1)
{
tempPos.x += 1f;
transform.position = tempPos;
}
}
}
Comment