- Home /
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?
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(); }
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.
Answer by FeenikxFire · Sep 30, 2016 at 01:45 PM
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.
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.
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.
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!!!!
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.
Thanks Jeff. Creating my own FPC and scripting it, is difficult at least for me. Isn't there a simpler way?
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);
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);
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
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 (?)
Your answer
![](https://koobas.hobune.stream/wayback/20220613171934im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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