Question by
Karim991996 · Dec 06, 2020 at 09:14 AM ·
unity4
My player isn't moving !
Hey guys , I'm trying to come up with another alternative for players movement , but my player isn't moving i don't know why , can someone please clarify for me why isn't my code working !
Here's the Code :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { private float speed = 10.0f; private Rigidbody playerRB; // Start is called before the first frame update void Start() { playerRB = GetComponent();
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// playerRB.AddForce(Vector3.forward * speed * verticalInput); // up and down
// playerRB.AddForce(Vector3.right * speed * horizontalInput); // right and left
if (Input.GetKey(KeyCode.W) && (Input.GetKey(KeyCode.S)))
{
playerRB.AddForce(Vector3.right * speed * verticalInput); // right and left
}
if (Input.GetKey(KeyCode.A) && (Input.GetKey(KeyCode.D)))
{
playerRB.AddForce(Vector3.forward * speed * horizontalInput); // up and down
}
}
}
Comment
Your answer
Follow this Question
Related Questions
UV Mapping problem. 0 Answers
File.WriteAllBytes problem 0 Answers
In what situation Component.gameObject method throws NullReferenceException? 2 Answers
Errors in building to APK 2 Answers