Happy weekend, all!
I’m trying to use a Vector3 to copy the x and z euler angles, then change the euler y angle to -180, for the 2D object attached to the script and all of its children. By doing so I hope to flip the objects across the y axis, making them face the opposite direction— left.
I got the error in the title, however. Does anyone know how to make this work as intended without errors? I appreciate any insight, even if you’re not too sure.
The issue has only occurred after attempting to apply “transform.eulerAngles = FaceLeft” on line 72.
The issue is on line 19, character 44, and character 80— Unity doesn’t like the transform.eulerAngles.x & z.
Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
private void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
//This is the movement speed
private static float MoveForce = 10;
private static float JumpForce = 8;
public Vector2 HorizontalVelocity = new Vector2(MoveForce,0);
public Vector2 VerticalVelocity = new Vector2(0, JumpForce);
//This is the rotation for facing a sprite left.
private Vector3 FaceLeft = new Vector3(transform.localeulerAngles.x, -180, transform.localeulerAngles.z);
// </>
//This is for the TpToSpawn method
public Vector3 SpawnPosition = new Vector3(-2, -3, 0);
public Vector4 Upright = new Vector4(0, 0, 0);
// </>
private Rigidbody2D _rigidbody;
private void Update()
{
//Move Player to spawn position, reset their velocity, and make them upright
//If they fall off the map
if(transform.position.y < -5)
{
Debug.Log("The Player should be respawning!");
_rigidbody.velocity = new Vector2(0,0);
transform.position = SpawnPosition;
transform.eulerAngles = Upright;
}
//This allows horizontal movement
if(Input.GetButton("Right"))
{
_rigidbody.AddForce(HorizontalVelocity * Time.deltaTime, ForceMode2D.Impulse);
}
if(Input.GetButton("Left"))
{
_rigidbody.AddForce(-HorizontalVelocity * Time.deltaTime, ForceMode2D.Impulse);
}
//This allows for jumping if the character presses the jump button
//and their vertical velocity is less than 0.001 units/s
if(Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
//Add an Impulse force called JumpForce
_rigidbody.AddForce(VerticalVelocity, ForceMode2D.Impulse);
}
//Makes the character face left when they move left
//changing the euler angles to the "FaceLeft" Vector3
if(Input.GetButtonDown("Left"))
{
transform.eulerAngles = FaceLeft;
}
}
}