Question by
Hendawg · Jul 27, 2020 at 05:06 AM ·
c#rotationtransform.rotate
How do invert the Y rotation of my player on collision with an object?
I'm a noob and I have been looking at help pages for hours and I still can't get it to work. No matter what code I try, my player will never rotate. The objects are tagged correctly and everything. I would appreciate any help that can be given greatly! I have a feeling it's really simple lol. Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Portal")
transform.Rotate(new Vector3(0, 180, 0));
Debug.Log("Rotate!");
}
}
Comment
Answer by Llama_w_2Ls · Jul 27, 2020 at 06:12 PM
Vector3 EulerRotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(EulerRotation.x, 180, EulerRotation.z);
@Llama_w_2Ls I put that in my code an I got this error from the first line you gave me:
"Error CS0236 A field initializer cannot reference the non-static field, method, or property 'Component.transform' " Thank you for trying to help though!