- Home /
Question by
Elmaghraby1 · Feb 16, 2020 at 09:54 AM ·
c#movementvrcontrollerplayer movement
Player Movement
i am creating a VR cardbaord game and i want the player to move forward with acc. and dodge objects by going left and right. i have already created the forward movement but i can`t create the left and right movement
here is the code i used
public float acc = 0.5f;
public float maxSpeed = 10.0f;
public float speedSlowdown = 0.4f;
[SerializeField] private float speed = 3.5f;
[SerializeField] private float leftLimit = -5.0f;
[SerializeField] private float rightLimit = 5.0f;
public float jumpingForce;
public bool canJump = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerMovement();
}
void PlayerMovement ()
{
// acc logic
speed += acc * Time.deltaTime;
if (speed > maxSpeed)
{
speed = maxSpeed;
}
float moveHorizontal = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
Vector3 direction = new Vector3(transform.forward.x, moveHorizontal, transform.forward.z);
transform.position += direction.normalized * speed * Time.deltaTime;
// player stay in area
if (transform.position.x < leftLimit)
{
transform.position = new Vector3(leftLimit, transform.position.y, transform.position.z);
}
else if (transform.position.x > rightLimit)
{
transform.position = new Vector3(rightLimit, transform.position.y, transform.position.z);
}
//player jump logic
if (Input.GetButton("Fire1") && canJump)
{
canJump = false;
GetComponent<Rigidbody>().AddForce(0, jumpingForce, 0);
}
}
Comment
Can you please post the code of how you create the movement so far?
i added the code i hope you can help thank you