I can only make my object jump, but not move left and right? I can’t see why my code wouldn’t work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public CharacterController controller;
private float gravity = 12.04f;
private float jumpForce = 6f;
private float verticalVelocity;
public float speed;
public Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody> ();
controller = GetComponent<CharacterController> ();
}
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, moveVertical, 0.0f);
rb.AddForce (movement * speed);
}
private void Update(){
if (controller.isGrounded) {
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space)) {
verticalVelocity = jumpForce;
}
} else {
verticalVelocity -= gravity * Time.deltaTime;
}
Vector3 moveVector = Vector3.zero;
moveVector.y = verticalVelocity;
controller.Move (moveVector * Time.deltaTime);
}
}