So I'm attempting to make a movement system for the player. I've gotten quite far, with camera rotation and player rotation, but I haven't quite found a good tutorial on creating a movement system that isn't for 2D or utilizes a component such as Rigidbody which is something I don't necessarily want to start experimenting with or having to deal with repercussions or changes in a system that works quite well, just to accommodate something that might end up in me completely rewriting a week's worth of code-writing and musing over what code to write. Before you ask, I HAVE read the unity documentation, and that does require Rigidbody, so I'm just asking if you know of any tutorials that teach me how to create such a system without the use of Rigidbody or other odd components. I'm sort of ok with CharacterController, but if there's a way that I don't have to use even that, I'd prefer it.
You should learn to use Rigidbody, because it's basically the main component of unity Physics, and it's not so difficult to learn. The only way (i know) of moving something without rigidbody is transform.Translate, and it just ignores walls. Edit: Sorry, there's quite a few ways to move things without rigidbody, but every one of them ignores walls
Here's the z movement code in jscript, it's not that hard, and it's faster and smaller as a transform.Translate code Code (JavaScript): var rb = GetComponent(Rigidbody); function Update () { rb.velocity.z = Input.GetAxis("Horizontal"); if (Input.GetAxis("Horizontal") == 0) { rb.velocity.z = 0; } }
When I do that, it says, "Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable". Not quite sure if I'm doing something since I'm converting code to C#, but the code actually looks exactly the same, so I don't think it's that: Code (CSharp): void Update() { rb.velocity.z = Input.GetAxis ("Horizontal"); if (Input.GetAxis ("Horizontal") == 0) { rb.velocity.z = 0; } }
Ok, here you go, my far-far complicated script from the game i'm working on: Code (CSharp): if (Input.GetAxis("YAxis") > 0) { rb.AddForce(0,0,MoveSpeed,ForceMode.VelocityChange); } if (Input.GetAxis("YAxis") < 0) { rb.AddForce(0,0,-MoveSpeed,ForceMode.VelocityChange); } if (Input.GetAxis("XAxis") > 0) { rb.AddForce(MoveSpeed,0,0,ForceMode.VelocityChange); } if (Input.GetAxis("XAxis") < 0) { rb.AddForce(-MoveSpeed,0,0,ForceMode.VelocityChange); } if (Input.GetAxis("YAxis") == 0) { rb.velocity.z = 0; } if (Input.GetAxis("XAxis") == 0) { rb.velocity.x = 0; } if (rb.velocity.magnitude > 3f) { rb.velocity = rb.velocity.normalized * 3f; }
Ohh, i see what's the problem you're using C#, my script is in Jscript, and maybe that makes the error. I looked it up, and you can simply solve it by doing this: Code (CSharp): rb.velocity = Vector3(rb.velocity.x,rb.velocity.z,Input.GetAxis("Horizontal")); instead of this: rb.velocity.z = ...
This is the correct code for C# Code (CSharp): var rb = GetComponent(Rigidbody); void Update () { rb.velocity = Vector3(rb.velocity.x,rb.velocity.y,Input.GetAxis("Horizontal"); if (Input.GetAxis("Horizontal") == 0) { rb.velocity = Vector3(rb.velocity.x,rb.velocity.y,0); } }
Actually, it seems that for some reason the Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") simply don't work, or they work too well. I've inputted a Debug.Log() into all of the if statements, and they're constantly getting triggered no matter what I do. My console is just getting spammed. However, clickig W, A, S and D may or may not trigger the messages, but my player does not move in the slightest. I'm not quite sure why this is, but I definitely would not like to have to implement specific if statements for all the possible keys.
Go to Edit/Project Settings/Input and there change the Horizontal one to positive button: w negative :s, because it's defaulted to Joystick, my bad
I've done that, but it still does not work. For me, Input.GetAxis rarely works, I only use it for Mouse X and Mouse Y
Then the other code that I posted should work, I'm tried, and it works, the only thing, that you need to make YAxis(w,s) and XAxis(a,d)
The other code you posted doesn't really work, the player still doesn't move. To be honest, I'm looking for tutorials here, not really code snippets. That's more a job for Unity Answers. So if you know of any good tutorials that actually work (*cough* docs.unity3d.com *cough*), I'd appreciate that.
This is a great tutorial series, it explain's everything from the basics, there is tutorial for inventory, shops terrain, and what's good for you Movement (I think there's multiple Episodes of that) It even explaines how codes work.
Hello. I don't know any tutorials but you can use this if you want. Add this to your player Code (CSharp): using UnityEngine; using System.Collections; public class PlayerMovement : MonoBehaviour { private Rigidbody rb; private float runSpeed = 5f; private float jumpForce = 650f; private bool jumping = false; private bool ground = false; private Vector3 speed; private void Start() { rb = GetComponent<Rigidbody>(); } private void Update() { GetInput (); } private void FixedUpdate() { MovePlayer(); } private void GetInput() { speed.x = Input.GetAxis("Horizontal") * runSpeed; speed.y = rb.velocity.y; speed.z = Input.GetAxis ("Vertical") * runSpeed; speed = transform.rotation * speed; if (Input.GetKeyDown(KeyCode.Space) && !jumping && ground) jumping = true; } private void MovePlayer() { rb.velocity = speed; if (jumping) { rb.AddForce(new Vector3(0, jumpForce, 0)); jumping = false; } rb.AddForce(new Vector3(0, -25, 0)); // schneller fallen } private void OnCollisionStay(Collision col) { foreach (ContactPoint contact in col.contacts) { if (Vector3.Angle(contact.normal, Vector3.up) < 60) { ground = true; } } } private void OnCollisionExit() { ground = false; } } Add this to your camera. Code (CSharp): using UnityEngine; using System.Collections; public class PlayerCamera : MonoBehaviour { public GameObject player; private float sensitivity = 1f; private float xRot; private float yRot; public CursorLockMode cursorLock; private void Update() { GetInput(); } private void FixedUpdate() { Move(); } private void GetInput() { if (xRot > 360) xRot -= 360; else if (xRot < 0) xRot += 360; xRot += Input.GetAxis("Mouse X") * sensitivity; yRot -= Input.GetAxis("Mouse Y") * sensitivity; yRot = Mathf.Clamp(yRot, -90, 90); Cursor.lockState = cursorLock; } private void Move() { transform.localRotation = Quaternion.Euler(new Vector3(yRot, 0, 0)); player.transform.rotation = Quaternion.Euler(new Vector3(0, xRot, 0)); } }
My axes are working perfectly, but the thing just doesn't want to move. Somehow, printing the Input.GetAxis() returns an actual number, but printing speed.x immediately after setting it to the Input.GetAxis() returns 0.
Ummm... I'll send you a project without sensitive art and code... How's about that. Also, question: how?
OK sorry that took so long, but I've discovered now that it DOES work, but for some reason all my axes are completely screwed up???? Here's the link to the project: http://www.mediafire.com/download/8avu1i0aa01q9vt/BrokenProject.unitypackage
For some reason your player is a "missing prefab". Maybe you should try to copy your whole project folder and put it in a .zip archive?
That's because I didn't provide the prefab, but for some reason unity decided to package the player component. The prefab exists and it works fine, but it doesn't make sense that it packaged the gameobjects that I deleted into the thing... EDIT Here's the updated .unitypackage without all the crazy missing prefab shenanigans: http://www.mediafire.com/download/seylr9ipxpb0zyx/BrokenProject.unitypackage
Sorry, but after further testing it turn out it's not working. Let me send you the new project: http://www.mediafire.com/download/wcfzy36rpbyosg1/BrokenProject.unitypackage (Remember to double-click the TestScene file).