Hey all, novice C# coder here. I am trying to make it so that the player can turn when a key is pressed, towards their facing direction. I already have it so the player can move, now I just want the character to turn in the direction they are moving. Here is the code for movement I have so far:
Well, to turn, just insert code that sets transform.rotation, e.g. transform.localEulerAngles = new Vector3(0, 90, 0).
(Obviously you will need to specify a different value for that rotation for each of the four directions.)
Also note that you never need to say âgameObject.transformâ like that⌠just âtransformâ will do. Finally, when you paste code, please use the âInsertâ button (between the picture icon and the floppy disk icon) to get it formatted correctly. See here for more info.
Welcome to the community, and good luck with your game!
This really should be in the code section of the forums.
When posting code make sure to put it into brackets:
like this!
(Quote me so you can see what I wrote to make that)
Thereâs tonâs of documentation out there on this already and you need to work on looking it up.
I would transform rotate towards a rotation vector3 in world space when pressing the button.
Edit: So something like:
void Update() {
if (Input.GetButtonDown("Left")){
transform.eulerAngles = new Vector3(0, 270, 0);
}
if (Input.GetButtonDown("Right")){
transform.eulerAngles = new Vector3(0, 90, 0);
}
if (Input.GetButtonDown("Up")){
transform.eulerAngles = new Vector3(0, 0, 0);
}
if (Input.GetButtonDown("Down")){
transform.eulerAngles = new Vector3(0, 180, 0);
}
}
Also, youâre going to want to cache the objectâs transform so that Unity isnât going back and forth to the location in memory every frame. What Iâve given you SHOULD work, but itâs not the ârightâ way.
To cache a transform declare it with your variables then assign it in the start or awake function:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
private Transform myTransform;
void Start(){
myTransform = transform;
}
void Update() {
//Now use
myTransform.eulerAngles = new Vector3(0, 0, 0);
//Instead of
transform.eulerAngles = new Vector3(0, 0, 0);
}
}
Really? Does that actually work? I always assumed that .forward, .left, etc. were essentially read-only. To make assigning to them work, Unity would have to make some hefty assumptions about which axis you want to rotate around. Thereâs certainly nothing in the docs about it that I can see⌠but maybe itâs an undocumented feature?
Well, Unity is an enhanced, tamed version of HAL. In 2001, a young man (letâs call him David âŚ) found the remainings of HAL in an abandoned shed somewhere in Denmark. After 3 years of âŚexperimentation he renamed it to Unity3D - a friendly, lovely piece of software - mostly. it makes no assumptions - at least not in this case:
transform.forward = -Vector3.left; //for instant. turn right etc.
This does Not rotate something aroundâŚslowly. This line immediately (this frame!) re-orientates your transform, so that an imaginary point one unit in front of it in local space (space of this transform) is = Vector3(imagine position in world space) 1(x), 0(y), 0(z)
Vector3.right is the same as -Vector3.left (sorry for being complicated)
you could use something like transform.forward = Vector3.Lerp(transform.forward, Vector3.right, Time.deltaTime * howFastValueInsertSomeFloatHere);
But this would Not rotate the transform in a constant (or lerp-ish) speed, but it would move the imaginary point (see above) to itâs new position - aand okay⌠resulting in some kind of rotation for the transform. but be aware
and sorry for being complicated again. super tiredâŚ
for beginners reading this:
I recommend spending a few hours, learning and understanding the basics of Vectors, what they are, what they do and to get a better understanding of world and local space etc.
Yeah, but there are an infinite number of orientations that do that. Point your forward vector in that direction. Now rotate around your forward vector. Orientation is 4-dimensional; this only specifies three dimensions, leaving one unspecified. So how is that one set?
I frankly didnât believe that they would actually do this, so I tried it. Youâre right; it works. I donât know how they pick the free rotation, and I still claim itâs a skanky undocumented feature, but yes, it does work. Thanks for teaching me something new today!
I see, what you mean is What about all the other axes, when just Y-rot. points forward?? what about ârollâ etc.
I THINK all other axes get reset?
When using LookAt you can define some worldUp variable, so this would solve the problem you mentioned yea? perhaps here some standart-up vector is used?
Right, I always use LookAt when I want to make an object look at something, because itâs explicit about the fourth axis (via the up vector).
Presumably Unity is just choosing some fourth axis when you just assign to transform.forward (or .right or .up, which also work). I think I know the conversation that must have happened at Unity, because Iâve been in a similar situation many times when I was working at REAL Software⌠âWell, users really shouldnât be doing this, but if they do, we ought to make it do something sensible. So, letâs just pick a reasonable behavior and quietly code it up, but not document it or draw attention to it.â
Itâs still cool though, and especially in 2D (where there isnât this extra rotation ambiguity) it could be a really handy way to point a sprite!