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);
}
}