- Home /
How to rotate a gameobject (Y Axis) via c#?
So I want a system like the ones that are in many racing games, where when you press middle mouse (and while it is held down), the camera shows whats behind you (rotates 180 on the Y axis). My game is a being chased FPS, so I want to be able to look behind me, while keep running forward, this is possible, I've tried it by rotating the object the camera is on in the inspector and it works, so all I have to do is roate that object. In my script I have a gameobject called "you". That is the object with the camera on it, all I need to do is figure out a way to rotate it 180 on the Y, and then on unclick make it go backto 0. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookBehind : MonoBehaviour
{
public GameObject m9;
public bool On;
public bool onoff;
public GameObject curs;
public GameObject you;
public void Update()
{
if (Input.GetMouseButton(2))
{
onoff = !onoff;
if (onoff)
{
m9.SetActive(false);
curs.SetActive(false);
}
}
else
{
m9.SetActive(true);
curs.SetActive(true);
}
}
}
Answer by x4637x · Aug 04, 2017 at 07:06 AM
Add this function in your class :
public void ObjectRotation(GameObject _you){
_you.transform.roation = Quaternion.Euler(new Vector3([Degree in X axis],[Degree in Y axis],[Degree in Z axis]));
}
Call it where you want Game Object to rotate (replace [] to your rotation angle in degree). ie:
ObjectRotation(you);
You might want to store you.transform.roation
to somewhere before you do the rotation, and when you want to go back to that you could assign current value to that again.
It's starting me off looking the wrong way, and my gun and stuff is only true when it's facing the wrong way too? Here's what I'm using and thanks for the fast reply!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookBehind : $$anonymous$$onoBehaviour
{
public GameObject m9;
public bool On;
public bool onoff;
public GameObject curs;
public GameObject you;
public void Update()
{
if (Input.Get$$anonymous$$ouseButton(2))
{
onoff = !onoff;
if (onoff)
{
m9.SetActive(false);
curs.SetActive(false);
ObjectRotation(you);
}
}
else
{
m9.SetActive(true);
curs.SetActive(true);
ObjectRotation2(you);
}
}
public void ObjectRotation(GameObject you)
{
you.transform.rotation = Quaternion.Euler(new Vector3(0,180,0));
}
public void ObjectRotation2(GameObject you)
{
you.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
}
So I assume this "you" gameobject is not at where your player is? Is it at the location where your camera is ?
If that is the case, the quickest solution is to add an empty game object to where your player is at, then make that object as parent to your "you" object, and call
ObjectRotation([new empty game object]);
Another solution to this will be change your "you" game object's position according to where player is at. Imaging drawing a circle using "you" game object and player is the center point, $$anonymous$$athf.Sin and $$anonymous$$athf.Cos should help you with that.
Oh yeah, in this case you could update your camera rotation by calling transform.rotation.SetLookRotation()
maybe. I am not sure if is the right function to do the job, you could look it up.
Also I notice you are using Input.Get$$anonymous$$ouseButton() inside your Update(), this will toggle onoff every frame when your mouse button being held down. Try Input.Get$$anonymous$$ouseButtonDown() and Input.Get$$anonymous$$ouseButtonUp() ins$$anonymous$$d.
This works exactly like I want it to, but its flickering, I think that has something to do with it being in the Update function, and my you object is a child of my controller/player (it's a fps). How would I stop the flickering?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookBehind : $$anonymous$$onoBehaviour
{
public GameObject m9;
public bool On;
public bool onoff;
public GameObject curs;
public GameObject you;
public void Update()
{
if (Input.Get$$anonymous$$ouseButton(2))
{
onoff = !onoff;
if (onoff)
{
m9.SetActive(false);
curs.SetActive(false);
you.transform.localEulerAngles = new Vector3(0, 180, 0);
}
}
else
{
m9.SetActive(true);
curs.SetActive(true);
you.transform.localEulerAngles = new Vector3(0, 0, 0);
}
}
}