Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by vahid62 · Nov 19, 2014 at 05:40 PM · rotatefirst person

Rotate First Person Controller via script

Hi,

When I click on a GameObject, I want the position of First Person Controller sets to this GameObject.

 function OnMouseDown () 
     {
         GameObject.Find("First Person Controller").transform.position = transform.position;
         GameObject.Find("First Person Controller").transform.rotation = Quaternion.Euler (0,90,0);
     }


In this code the position changes well but the rotation doesn't work.

I want First Person Controller rotates to X axis direction. How can I do it?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by bossmuffin8877 · Jul 12, 2015 at 01:58 PM

Find the part in the Update void that says RotateView(); and then put and if statement in there or something like if (noRotateView == false) { RotateView(); }

Comment
Add comment · Show 1 · 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 vt5491 · Mar 03, 2016 at 08:09 AM 1
Share

The answer provided by bossmuffin8877 worked for me, although given how much effort it took to finally figure this out, I'm inclined to agree with the others in that it's probably just easier to roll your own FPS controller.

I replaced the mouselook altogether with keyboard commands. Here are some code snippets:

1) Remove call to "RotateView" from "FirstPersonController.cs":

 // Update is called once per frame
 private void Update()
 {
     //RotateView();  // comment out

2) attach a custom script to "FPSController" with the following:

 public class myFPSBehavior : $$anonymous$$onoBehaviour {
     public float rotationSpeed = 10.0F;
     
     void FixedUpdate()
     {
         if (Input.Get$$anonymous$$ey("escape"))
             Application.Quit();
 
         float rotation = Input.GetAxis("Rotate") * rotationSpeed;
   
         rotation *= Time.deltaTime;
 
         transform.Rotate(0, rotation, 0);
     }
 }

i.e just do a normal keyboard handler.

The key is to get rid of, or control the access to, RotateView, which seems to override any custom rotations you make.

avatar image
3

Answer by FeenikxFire · Sep 30, 2016 at 01:45 PM

@vahid62,

I have a script on my camera (MyCamera.cs) attached to the standard unity first person character controller. In that script I have just figured out how to set it.

My script (MyCamera.cs) needs reference to the Unity first person character controller script. So I added:

 using UnityEngine;
 using System.Collections;
 using UnityStandartAssets.Characters.FirstPerson; //This

Then make a reference to the First person Controller Script.

 public FirstPersonController fpc;

Actually open the FirstPersonController script attached to your playable character. Find the script reference:

 private MouseLook m_MouseLook; 

Hover over the MouseLook and press F12 (to find) and set it to public from private.

Inside of the MouseLook.cs search for two private Quaternions:

 private Quaternion m_CharacterTargetRot
 private Quaternion m_CameraTargetRot;

In my script (MyCamera.cs) when I wanted the rotation to be zero i wrote in C#: fpc.m_MouseLook.m_CharacterTargetRot = Quaternion.Euler(0f, 0f, 0f); fpc.m_MouseLook.m_CameraTargetRot = Quaternion.Euler(0f, 0f, 0f);

This worked for me. It made the character and the cameras rotation back to zero.

Comment
Add comment · Show 5 · 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 Whiteleaf · Sep 30, 2016 at 05:36 PM 0
Share

Please don't revive dead threads.

avatar image FeenikxFire Whiteleaf · Sep 30, 2016 at 11:54 PM 2
Share

How should I be aware if a thread is dead? If I found it, is it dead?

And this response is for others trying for the same issue.

avatar image JimmyCushnie FeenikxFire · Jul 08, 2017 at 09:20 PM 2
Share

For what it's worth, I'm here 8 months in the future googling around and I'm grateful that you made the comment you did.

Show more comments
avatar image Excit0n · Jul 31, 2017 at 08:20 PM 0
Share

Thanks so much for your answer! Totally worked for me. I will add one thing and that's to make both the m_CharacterTargetRot and m_CameraTargerRot public variables. Thanks a bunch!!!!

avatar image
0

Answer by Jeff-Kesselman · Nov 19, 2014 at 05:43 PM

the FPC has its own logic for movement. Trying to take it over is likely to lead to nothing but grief.

I recommend at this point you create your own using an object with a camera component, a capsule collider, rigidbody, and scripts to read controls and move it.

Comment
Add comment · Show 1 · 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 vahid62 · Nov 19, 2014 at 06:05 PM 0
Share

Thanks Jeff. Creating my own FPC and scripting it, is difficult at least for me. Isn't there a simpler way?

avatar image
0

Answer by Faithful_M · Nov 19, 2014 at 07:48 PM

Hello dear

You can do it by using RotateAroundLocal

if you want rotate 90 degress around x-axis you can do like this

transform.RotateAroundLocal(Vector3.Right,wantedAngle);

Comment
Add comment · Show 3 · 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 vahid62 · Nov 19, 2014 at 08:24 PM 0
Share

I tested it and FPC doesn't rotate.

avatar image Faithful_M · Nov 20, 2014 at 01:29 AM 0
Share

You should modify wantedAngle by radians. So if you want rotate 90 degrees around x-axis you can do like this; transform.RotateAroundLocal(Vector3.right,90f * $$anonymous$$athf.Deg2Rad);

avatar image vahid62 · Nov 20, 2014 at 07:57 AM 0
Share

As the Jeff answered above, FPC has its own logic and the problem is FPC has its own $$anonymous$$ouse lock script and transform.rotation doesn't work properly with it

avatar image
0

Answer by buttmatrix · Apr 12, 2016 at 05:48 AM

Put the FPScontroller in an empty gameobject. Such a hack, but it works. At this time, I'm not actually clear on why FPScontroller ignores transform.rotation (?)

Comment
Add comment · Show 1 · 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 rotundo21 · Oct 01, 2019 at 03:48 PM 0
Share

This works better. Genius!

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

37 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

Related Questions

Lock look rotation first person. 0 Answers

Camera rotation around player while following. 6 Answers

Unity 3D Orienting on Tagged Surfaces / Wall Walking 1 Answer

How to simulate a first person environment in 2D 1 Answer

Space Game Camera 2 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