Duplicate Question
How to move the Character using WASD
Hi!
First of all, I dont want to use the FPS Controller Prefab and a C# script (peace hahaha)
So how do I move my character to go forward where Im currently looking at.
Thnks!
Answer by efeguclu · Jul 04, 2017 at 02:23 PM
try this(Don't Forget To Attach a rigidbody to your object and make sure script is attached to object that is going to move) :
private void Update() {
Rigidbody rb = GetComponent<Rigidbody>();
if (Input.GetKey(KeyCode.A))
rb.AddForce(Vector3.left);
if (Input.GetKey(KeyCode.D))
rb.AddForce(Vector3.right);
if (Input.GetKey(KeyCode.W))
rb.AddForce(Vector3.up);
if (Input.GetKey(KeyCode.S))
rb.AddForce(Vector3.down);
}
and JS :
private function Update() {
var rb = GetComponent.<Rigidbody>();
if (Input.GetKey(KeyCode.A))
rb.AddForce(Vector3.left);
if (Input.GetKey(KeyCode.D))
rb.AddForce(Vector3.right);
if (Input.GetKey(KeyCode.W))
rb.AddForce(Vector3.up);
if (Input.GetKey(KeyCode.S))
rb.AddForce(Vector3.down);
}
It's C# I'll write JS into answer wait..
Isnt this about rolling a ball? I need the FPS controller one
How do I add the object? (And can I have another script to move to camera with mouse?)
Please help me do this https://www.youtube.com/watch?time_continue=42&v=df5G8s-$$anonymous$$VQ4.
Answer by FunIsDangerous · Jul 04, 2017 at 02:41 PM
Here is my code with character controller. It doesn't have gravity by default so I added it. I assume you want it for a 3d game
public float speed = 5;
public float gravity = -5;
float velocityY = 0;
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
velocityY += gravity * Time.deltaTime;
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0 Input.GetAxisRaw("Vertical"));
input = input.normalized;
Vector3 temp = Vector3.zero;
if (input.z == 1)
{
temp += transform.forward;
}
else if (input.z == -1)
{
temp += transform.forward * -1;
}
if (input.x == 1)
{
temp += transform.right;
}
else if (input.x == -1)
{
temp += transform.right * -1;
}
Vector3 velocity = temp * speed;
velocity.y = velocityY;
controller.Move(velocity * Time.deltaTime);
if (controller.isGrounded)
{
velocityY = 0;
}
}
This will also work with a controller. The main advantage of using a character controller over rigidbody is it can go up slopes. Also, if you want jumping, it's simple enough, the axis is Input.GetAxisRaw("Jump"), and just reset velocityY to how far you want to jump
check my answer I have C# And JS And If you see void in script it is C# but if you see function so it isi JS
Answer by Shrikky23 · Jul 04, 2017 at 02:32 PM
private int speed = 10;
public void Move()
{
Vector3 Movement = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical");
player.transform.position += Movement * speed * Time.DeltaTime;
}
Make sure the Project settings -> Input has Horizontal and vertical assigned to WASD/ Arrow keys.
public float speed =20;
public float turnSpeed = 50f; void Update () { if (Input.GetKey (KeyCode.W)){ transform.Translate(Vector3.forward*speed*Time.deltaTime); } if (Input.GetKey (KeyCode.S)){ transform.Translate(-Vector3.forward*speed*Time.deltaTime); }
if (Input.GetKey (KeyCode.D)){
transform.Rotate(Vector3.up,turnSpeed * Time.deltaTime);
} if (Input.GetKey (KeyCode.A)){ transform.Rotate(Vector3.up,-turnSpeed * Time.deltaTime); } }
Follow this Question
Related Questions
Constant Player Spinning to the left. 1 Answer
How to flip my character with this script 0 Answers
Multiplayer 2 Answers