Goal: Rotate paddle for pinball game when you press the A key.
Error:
CS1061:
Quaternion
does not contain a definition foreularAngles
and no accessible extension methodeularAngles
accepting a first argument of typeQuaternion
could be found (are you missing a using directive or an assembly reference?)
After googling the issue and finding a 'loosely' related forum as well as looking up on Unity forums how eularAngles
and Quaternion is supposed to be handled, I think my issue is that I haven't properly stated a class to 'normalise' the Quaternion. I have almost no understanding of these concepts.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paddleLeft : MonoBehaviour{
private Transform myTransform;
void Start() {
myTransform = gameObject.transform;
}
void Update() {
if (Input.GetKey(KeyCode.A)) {
var ourRotation = myTransform.rotation.eularAngles;
var newRotation = new Vector3(ourRotation.x, ourRotation.y, ourRotation.z (5 * Time.deltaTime));
myTransform.rotation = Quaternion.Euler(newRotation);
}
}
}
CodePudding user response:
I tested the above code, which worked correctly, but I suggest using a simpler rotate alternative, as the transform class is stored directly inside the mono Behavior
body and does not need to be re-saved.
void Update()
{
if (Input.GetKey(KeyCode.A)) transform.Rotate(Vector3.forward, 5 * Time.deltaTime);
}
CodePudding user response:
Thanks to KiynL I have a better alternative than my original approach. One that is much simpler to understand conceptually. I've implemented it as the below code. I need to make the number much higher for better result on the pinball and add maximum and minimum range but that's for later. Very happy with the result of this.
if (Input.GetKey(KeyCode.A)) {
transform.Rotate(Vector3.forward, 50 * Time.deltaTime);
} else {
transform.Rotate(Vector3.forward, -50 * Time.deltaTime);
}