Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ChompIV · Aug 04, 2017 at 06:37 AM · camerarotationgameobjectmouserotate object

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);
 
 
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image x4637x · Aug 04, 2017 at 07:34 AM 0
Share

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.

avatar image ChompIV · Aug 04, 2017 at 07:43 AM 0
Share

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));
     }
 }
avatar image x4637x ChompIV · Aug 04, 2017 at 07:55 AM 0
Share

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]); 
avatar image x4637x ChompIV · Aug 04, 2017 at 08:00 AM 0
Share

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.

avatar image x4637x x4637x · Aug 04, 2017 at 08:04 AM 0
Share

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.

avatar image x4637x ChompIV · Aug 04, 2017 at 08:22 AM 0
Share

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.

avatar image ChompIV x4637x · Aug 04, 2017 at 10:12 PM 0
Share

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);
         }
     }
 
 }
Show more comments
avatar image
0

Answer by a161803398874 · Aug 04, 2017 at 09:12 AM

 you.LocalEulerAngles = new Vector3(x,y,z,);   
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating GameObject Around Player With "Mouse Y" Position 2 Answers

Get input strength from mouse movement? 1 Answer

Rotating gameobject after mouselook 0 Answers

Rotating a GameObject based on another GameObject's y-axis 1 Answer

Mouse Orbit snapping issues 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges