I am attempting to move my player forward, backward, left, and right based on the EDSF keys but not on an axis; rather, based on where the mouse is looking. I am having a lot of trouble figuring it out....either my player stays on the axis no matter where the mouse is looking or my player falls over or flips upside down because of the mouse look...please help!!
I've tried tutorials on unity and udemy as well as youtube (I've been stuck on this for days --literally). I would say I am a beginner at C#..so I've tried transform/translate and Vector3. I currently have the following code...which 1. stays on the axes and 2. rotates the player to fall on its side if the mouse looks in a certain direction.. Code (csharp): public class PlayerMovement : MonoBehaviour { public float moveSpeed = 4.0f; public float rotateSpeed = 100.0f; public bool canMoveSideways = true; public float gravity; public float jumpForce = 500f; public float runSpeed = 8.0f; void Update () { Movement(); } void Movement() { //move front to back if (Input.GetKey(KeyCode.E)) { Debug.Log("Key E Pressed."); transform.position += Vector3.forward * Time.deltaTime * moveSpeed; } else if (Input.GetKey(KeyCode.D)) { Debug.Log("Key D Presses."); transform.position += Vector3.back * Time.deltaTime * moveSpeed; } //move side to side if (Input.GetKey(KeyCode.S)) { Debug.Log("Key S Pressed."); transform.position += Vector3.left * Time.deltaTime * moveSpeed; } else if (Input.GetKey(KeyCode.F)) { Debug.Log("Key F Pressed"); transform.position += Vector3.right * Time.deltaTime * moveSpeed; } } void jump() { Vector3 Motion; Motion = Vector3.zero; CharacterController controller; controller = gameObject.GetComponent<CharacterController>(); //jump if (Input.GetKey(KeyCode.Space) || controller.isGrounded) { Motion.y = jumpForce; } Motion.y -= gravity * Time.deltaTime; controller.Move(Motion * Time.deltaTime); } It could be something simply wrong that I am doing, but I am open to any help. As I said, I am a beginner.
Because you are using directly Vector3.someDirecton, like Code (CSharp): transform.position += Vector3.right * Time.deltaTime * moveSpeed; you simply move along axis, x (left/right). So you need some form of direction detection. Does your character rotates toward mouse? If so, you can use instead Vector3.right, use transform.right. There is multiple ways, to achieve the same mind. Also, please use Using code tags properly You can edit main post.
The following code that I have is: Code (csharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class LookAtMouse : MonoBehaviour { private Vector3 worldpos; private float mouseX; private float mouseY; private float cameraDif; public GameObject fpc; void Start() { cameraDif = GetComponent<Camera>().transform.position.y - fpc.transform.position.y; } void Update() { mouseX = Input.mousePosition.x; mouseY = Input.mousePosition.y; worldpos = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif)); Vector3 turretLookDirection = new Vector3(worldpos.x, fpc.transform.position.y, worldpos.z); fpc.transform.LookAt(turretLookDirection); } } AND Code (csharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public float moveSpeed = 4.0f; public float rotateSpeed = 100.0f; public bool canMoveSideways = true; public float gravity; public float jumpForce = 500f; public float runSpeed = 8.0f; Rigidbody rigidBody; void Start() { //Fetch the Rigidbody component you attach from your GameObject rigidBody = GetComponent<Rigidbody>(); } void Update() { if (Input.GetKey(KeyCode.E)) { //Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view) rigidBody.velocity = transform.forward * moveSpeed; } if (Input.GetKey(KeyCode.D)) { //Move the Rigidbody backwards constantly at the speed you define (the blue arrow axis in Scene view) rigidBody.velocity = -transform.forward * moveSpeed; } if (Input.GetKey(KeyCode.F)) { //Rotate the sprite about the Y axis in the positive direction transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * moveSpeed, Space.World); } if (Input.GetKey(KeyCode.S)) { //Rotate the sprite about the Y axis in the negative direction transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * moveSpeed, Space.World); } } } I figured this out based on your last suggestion. So thank you! However, the "player" moves unnaturally...for instance, when looking down and to the side, it is a slight sideways view instead of the "player" rotating to accommodate. Is there a way to fix that? (image attached)
Looks much better. However, I can not picture in my mind, what exactly you mean by I got some thoughts, but would be better, if you describe, draw, or record short vid, to show what is going on.
Just to confirm: 1. When looking round (no movement), is all ok? 2. When moving without looking round (look forward), is ok? 3. When look round horizontally, is moving ok? Is moving sideways / forward ok? 4. When look up down and moving, is the issue? Is moving sideways / forward ok / problem?
I determined the issue ( I still had a different script attached, so both were trying to be used at the same time). However, now I have another issue; When I look down and use D for backward, I jump. Is there a way to fix that? (p.s. sorry for the many questions--I am greatly appreciative for your assistance!). Should I be adding isGrounded?
There is multiple ways to solve the problem. Probably the easiest, is to have camera / head as child of character, and only allow to rotate it vertically. While body (parent) can rotate horizontally, with camera together. Clamping may cause, that you lose movement speed, higher you look. So if you look totally up, you wont be moving at all. If that makes sense?