Artificial Intelligence
Please fill the required field.

unity 3d c# rigidbody w to walk forward, a to turn left, s to walk backwards, d to turn right


- Stack Overflow Sign up × Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: can someone give me some code on how to walk with the the buttons? using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float speed = 10.0F; public float rotationSpeed = 100.0F; void Update() { float translation = Input.GetAxis("Vertical") * speed; float rotation = Input.GetAxis("Horizontal") * rotationSpeed; translation *= Time.deltaTime; rotation *= Time.deltaTime; transform.Translate(0, 0, translation); transform.Rotate(0, rotation, 0); } } This function handles the basic movement, with the input for Vertical being the up and down keys, and the Horizontal being left and right. Is there anything more you want this code to do? What have you tried? Also, for future reference - this isn't a 'here's the code' type place. You haven't exactly asked a question. the questioner has only been around for 1 day and this is the only question so i assume they are testing the waters – Screamy Dec 13 '13 at 8:21 that's what I thought! :) some info about it's use would be helpful though – ozuna Dec 13 '13 at 11:40 add a comment | up vote 2 down vote Here's a simple implementation, all you need to do is attach this script to your character, then make your character point in the direction you want using your model's rotation. if you want your character to automatically rotate towards the direction she's moving you can do something like this. You need the character constantly facing the direction she's moving, but first let's modify the script a bit. using UnityEngine; using System.Collections; public Rigidbody player; public float speed = 10.0F; public float rotationSpeed = 100.0F; public float h; public float v; public float horizonotalAxis; public float VerticalAxis; // Use this for initialization void Start () { } // Update is called once per frame void Update () { { h = player.transform.eulerAngles.y; v = player.transform.eulerAngles.x; horizonotalAxis = Input.GetAxisRaw("Horizontal"); VerticalAxis = Input.GetAxisRaw("Vertical"); if (horizonotalAxis != 0) { h += Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime; player.transform.eulerAngles = new Vector3(v, h, 0.0f); } if (VerticalAxis != 0) { if (VerticalAxis > 0) { player.transform.position += player.transform.forward * speed * Time.deltaTime; } else { player.transform.position -= player.transform.forward * speed * Time.deltaTime; } } } } First we add 2 more floats to handle our eulerAngles because they're a pain in the ass to work with, Then we use the new script to let the player rotate by the degrees we want, if you don't want the player to rotate automatically then this should be enough. share|improve this answer answered Dec 13 '13 at 8:13 Screamy 1,470114 I forgot to mention that your speed should be 1 - 10 otherwise she walks to fast, and that she needs a collider if you want her to collide with objects or you need a box collider. – Screamy Dec 13 '13 at 8:23 1 and the camera should be on the same gameobject because waht's the use of rotating your character when the camera is constantly fixed on it's direction – Screamy Dec 13 '13 at 8:24 And in order to rotate you need a rigidbody too but it doesn't have to move. – fromnaboo Dec 13 '13 at 11:48 @fromnaboo yeah her ridgedybody is already defined (public Rigidbody player) – Screamy Dec 13 '13 at 11:55 why rotate the whole gameobject if we can just rotate the camera and align it with the gameobject's forward vector? – ozuna Dec 13 '13 at 12:00 | show 6 more comments Not the answer you're looking for? Browse other questions tagged c# 3d unity3d or ask your own question. asked 1 year ago viewed 6808 times active 1 year ago Related 2 Unity 3d - C# - Changing an integer in another script 2 Logarithm max code 1 Walker Character rotation and speed issues in unity3d 2 Walking player on screen using Rigidbody - Unity 3D 0 Unity C# 3rd Person Character Control using Horizontal and Vertical axis 2 How can I get swipe direction? (swipe left, right, up, down, not screen clock position) 1 How to make a unity rigidbody 6 dof spring joint follow a more controlled set of characteristics -1 Why won't my Unity character walk forward? 0 Unity 3D- how to script a “walking transform” to object, to follow frame by frame the saved animation? 0 Cant get my player to move forward in unity all code and explination inside Hot Network Questions - How dangerous was the turbulence on this commercial flight? - Output the Current Time - What was the first story set in or about Middle Earth that J.R.R. Tolkien wrote? - What is the origin of the term Melange in Dune? - A student in my course does well on exams, but doesn't do the homework: Go easy on them, or make them "pay the price?" - How do Aircraft differentiate between Missile Locks? - Federation Technology Adapted From Aliens? - How to explain a dialog naturally to a friend - A word of sudden and shocking realization - How to run `nano` when using a `vim file` command - How do you phonetically pronounce all of the German note names? - Did Tolkien create any characters based on people he knew? - "Throw you with a stone" vs "Throw a stone at you" - Which is more wet: ‘moist’ or ‘damp’? - How long did it take for a letter to arrive in England in the 1830s? - What if the Planck constant was absolute zero? - For an end-user, is HTML5/JavaScript more secure than Flash? - Output the qwerty keyboard - Loop round and remove the consonants - why is python toolbox script not working standalone in PyScripter? - New hanging format for all paragraph in a newly defined environment - What is unit slotting? - Why does this bash conditional check work with [[ -n .. ]] but not [ -n .. ]? more hot questions question feed