Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovementPC : MonoBehaviour { public float moveSpeed = 2f; //public float turnSpeed = 2f; public Rigidbody2D player; Vector2 movement; // Update is called once per frame void Update() { //input movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); } private void FixedUpdate() { //movement player.MovePosition(player.position + movement * moveSpeed * Time.fixedDeltaTime); //rotation //transform.Rotate(0, movement.x * turnSpeed, 0); //transform.eulerAngles = new Vector3(0, transform.eulerAngles.z, 0); } } So I can move my top down view character, but would like it to rotate to the direction that it is gonna be travelling in. The character is a snail. So naturally it won't move fast and won't turn fast. Any solution to that?
You can use this to rotate slowly towards your direction: Code (csharp): transform.rotation = Quaternion.RotateTowards(transform.rotation, targetedRotation, Time.deltaTime * someSpeedInDegrees)); In 3D, determining the "targetedRotation" is easy (just Quaternion.LookRotation), but in 2D it needs a little more math. Assuming your sprite's "up" is its "forward", you can use this: Code (csharp): Quaternion targetedRotation = Quaternion.AngleAxis(Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg,Vector3.forward); (note: you probably want to cache this value and only change it when movement != Vector2.zero) From here you basically have two possible approaches: 1) The snail always moves absolutely in the direction you press to move, and rotating towards it is purely visual. For this, you should be able to just insert the line above and leave it as is. 2) The snail always moves in its own forward vector, and just turns towards the player's desired movement vector. This will look more correct but players might find it annoying to control, and there's a possibility of getting stuck in corners. For this, you'll want to change your movement line to something more like: Code (csharp): float moveSpeed = movement.magnitude; player.MovePosition(player.position + player.transform.rotation * Vector3.up * moveSpeed * Time.fixedDeltaTime);[code]
So I kind of understand what you're saying, I've only ever dealt with 3D, 2D seems way more complicated haha. I'm not entirely sure where to put the lines of code you're suggesting?
Code (CSharp): public class PlayerMovementPC : MonoBehaviour { public float moveSpeed = 2f; //public float turnSpeed = 2f; public Rigidbody2D player; Vector2 movement; // Update is called once per frame void Update() { //input movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); } private void FixedUpdate() { //movement player.MovePosition(player.position + movement * moveSpeed * Time.fixedDeltaTime); //rotation } } Based on what I have where do I add rotation? to update? fixedupdate?
Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovementPC : MonoBehaviour { public float moveSpeed = 2f; //public float turnSpeed = 2f; public Rigidbody2D player; Vector2 movement; // Update is called once per frame void Update() { //input movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); } private void FixedUpdate() { float moveSpeed = movement.magnitude; //movement player.MovePosition(player.position + movement * moveSpeed * Time.fixedDeltaTime); //player.MovePosition(player.position + player.transform.rotation * Vector3.up * moveSpeed * Time.fixedDeltaTime); //rotation if (movement != Vector2.zero) { Quaternion targetedRotation = Quaternion.AngleAxis(Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg, Vector3.forward); } } } I did this just to test if it rotates at all? but it doesn't. it still moves.
You haven't done anything with "targetedRotation" - you still need to apply it to the transform. Use the code from the first code tag in my previous comment.
Code (CSharp): { public float moveSpeed = 2f; public float turnSpeed = 2f; public Quaternion targetedRotation; public Rigidbody2D player; Vector2 movement; // Update is called once per frame void Update() { //input movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); } private void FixedUpdate() { float moveSpeed = movement.magnitude; //transform.rotation = Quaternion.RotateTowards(transform.rotation, targetedRotation, Time.deltaTime * turnSpeed)); //movement //player.MovePosition(player.position + movement * moveSpeed * Time.fixedDeltaTime); player.MovePosition(player.position + player.transform.rotation * Vector3.up * moveSpeed * Time.fixedDeltaTime); //rotation if (movement != Vector2.zero) { Quaternion targetedRotation = Quaternion.AngleAxis(Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg, Vector3.forward); } } So I did this but now I'm getting and error on line: player.MovePosition(player.position + player.transform.rotation * Vector3.up * moveSpeed * Time.fixedDeltaTime); am I on the right tracks?
You should be doing that after you set the value of targetedRotation. Also, in line 34 you're creating a local "targetedRotation" and the class variable that you're using on line 29 is never getting changed - remove the "Quaternion" from the start of line 34 to make that use the class variable. Also, always paste the actual error message - it contains important information for knowing what's going wrong.
Thanks for your help, I've not got it working. Woo. I'll be sure to reference this thread and you. Thanks again.