- Home /
Turn character the way I am moving
Hi I’ve been trying to be able to get my character to turn the way it’s moving all day and couldn’t do it could someone please just give me something that I could copy and paste into my script thank you
EDIT: I have problems like this a lot and everything is the exact way the person had it even when I copy and paste could something be wrong with my unity or something because it can’t be code when I copy and paste when it’s not working it’s very discouraging and happens a lot
EDIT: I noticed that the rotation in the inspector changes to 90.00001 or -90.00001 depending on the way I move the arrows on what ever axis I don't know if that's normal but yeah and one time the y position not rotation moved to 900000 when I try to move but that stopped happening. Sorry for editing so much.
EDIT: Here is my code right now without the changes also im a beginner so how do I do that I just want the character to turn so if I press left arrow he moves left and I want to make it so he rotates left too I saw a bunch of things about quarteron angle and stuff and it confused me and didn't work with my code probably because I had to delete something. using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { [SerializeField] float moveSpeed = 10f; [SerializeField] float padding = 1f; float xMin; float xMax; float yMin; float yMax;
// Start is called before the first frame update void Start() { SetMoveBoundaries(); }
private void SetMoveBoundaries() { Camera gameCamera = Camera.main; xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding; xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding; yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding; yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
}
// Update is called once per frame void Update() { Move();
}
private void Move() { var deltaX = Input.GetAxis("Horizontal") Time.deltaTime moveSpeed; var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax); var deltaY = Input.GetAxis("Vertical") Time.deltaTime moveSpeed; var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax); transform.position = new Vector2(newXPos, newYPos);
} }
Would be useful to see some code, how you move the character at the moment in your game. Then we can help you with the turning problem for your specific situation, because there are different approaches, how you could achieve this.
Here is my code right now without the changes also im a beginner so how do I do that I just want the character to turn so if I press left arrow he moves left and I want to make it so he rotates left too I saw a bunch of things about quarteron angle and stuff and it confused me and didn't work with my code probably because I had to delete something. using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : $$anonymous$$onoBehaviour { [SerializeField] float moveSpeed = 10f; [SerializeField] float padding = 1f; float x$$anonymous$$in; float x$$anonymous$$ax; float y$$anonymous$$in; float y$$anonymous$$ax;
// Start is called before the first frame update void Start() { Set$$anonymous$$oveBoundaries(); }
private void Set$$anonymous$$oveBoundaries() { Camera gameCamera = Camera.main; x$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding; x$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding; y$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding; y$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
}
// Update is called once per frame void Update() { $$anonymous$$ove();
}
private void $$anonymous$$ove() { var deltaX = Input.GetAxis("Horizontal") Time.deltaTime moveSpeed; var newXPos = $$anonymous$$athf.Clamp(transform.position.x + deltaX, x$$anonymous$$in, x$$anonymous$$ax); var deltaY = Input.GetAxis("Vertical") Time.deltaTime moveSpeed; var newYPos = $$anonymous$$athf.Clamp(transform.position.y + deltaY, y$$anonymous$$in, y$$anonymous$$ax); transform.position = new Vector2(newXPos, newYPos);
} }
Answer by Vibertex · Apr 07, 2020 at 04:03 AM
@LukeNewsome1 Try setting the Euler angles to the mouse pointer or whatever way you want it to change direction and *IMPORTANT*make sure that you select local at the top left when you select the character
Here is my code right now without the changes also im a beginner so how do I do that I just want the character to turn so if I press left arrow he moves left and I want to make it so he rotates left too I saw a bunch of things about quarteron angle and stuff and it confused me and didn't work with my code probably because I had to delete something.
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : $$anonymous$$onoBehaviour {
[SerializeField] float moveSpeed = 10f;
[SerializeField] float padding = 1f;
float x$$anonymous$$in;
float x$$anonymous$$ax;
float y$$anonymous$$in;
float y$$anonymous$$ax;
// Start is called before the first frame update
void Start()
{
Set$$anonymous$$oveBoundaries();
}
private void Set$$anonymous$$oveBoundaries()
{
Camera gameCamera = Camera.main;
x$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
x$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
y$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
y$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
}
// Update is called once per frame
void Update()
{
$$anonymous$$ove();
}
private void $$anonymous$$ove()
{
var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
var newXPos = $$anonymous$$athf.Clamp(transform.position.x + deltaX, x$$anonymous$$in, x$$anonymous$$ax);
var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
var newYPos = $$anonymous$$athf.Clamp(transform.position.y + deltaY, y$$anonymous$$in, y$$anonymous$$ax);
transform.position = new Vector2(newXPos, newYPos);
}
}
Answer by BBIT-SOLUTIONS · Apr 07, 2020 at 03:26 PM
Thanks for your code, that was helpful. I guess all you need is this line as first line of your Move()-function
Vector3 currPos = transform.position;
And as last line in the same function:
transform.forward = transform.position - currPos;
Or if it is maybe rotated, then try transform.up or transform.right instead of transform.forward
Hi thanks for your reply I did what you said and my character disappears and a warning appeared saying viewing vector is zero Its not appearing anymore tho so I don't know if that was a problem
never $$anonymous$$d it still says look rotation viewing vector is zero and it only disappears when I move my character then it reappears
Ok strange. I tested your code with my additional lines and everything worked actually.
Could you maybe make a screenshot of your error message in the console and from your scene, hierarchy and inspector? $$anonymous$$aybe something is wrong there...
Oh no, sorry. Have to correct me. You are right, if they are both the same value, forward would show to Zero vector, what is not defined, so we have to check, if it has really changed during this frame. (Just not sure, why i didn't get the error as i tried before) Try the following:
private void $$anonymous$$ove()
{
Vector3 currPos = transform.position;
var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
var newXPos = $$anonymous$$athf.Clamp(transform.position.x + deltaX, x$$anonymous$$in, x$$anonymous$$ax);
var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
var newYPos = $$anonymous$$athf.Clamp(transform.position.y + deltaY, y$$anonymous$$in, y$$anonymous$$ax);
transform.position = new Vector2(newXPos, newYPos);
if(currPos != transform.position){
transform.forward = transform.position - currPos;
}
}
Your answer
Follow this Question
Related Questions
2D Zig-zag movement and rotation 1 Answer
how to rotate a gameobject in a given direction and stop when it's done 1 Answer
Sprite will not roate when arrow keys are pressed 2 Answers
Change the z rotation of my player?,how to adjust the z rotation of my player? 1 Answer
Is it possible to constrain an object's rotation in worldspace? 2 Answers