- Home /
Noob: Question about standard c# mouselook script. And other probably noob questions others may ask.
I am a bit confused on mouselook in c# and unity. I used the standard asset mouselook script as a template and tried to change it to suit my needs. When i used it, it seemed to work perfectly, but my vertical look made my character's model roate. When i looked up it would rotate and look up, which causes it to move forward. When i look down, it rotates the whole model and looks down, which causes it to move backwards. I figured this was due to the controller script i attached to the player. I wnat to do a small tweak, and rotate the character on its x-axis normally, but instead of the normal y rotation, to find the character's attached camera, and apply the rotation to that object.
I am a bit confused as to what this line does:
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX; I understand it puts the values from the transform rotation y value, and adds the mouse input x value, and the variable to change sensativity, but i am unsure why it uses the localEulerAngles.y value and not the x value. I change it to y, and the camera seems to just twitch around the same location when i move the mouse. Why does it do this?
I'm also having a problem with unity where the default (or essentially default) mouselook script doesnt work for rotation on y, (up / down). Why would this be? Here is my attached script:
PS: I have no other script that modifies rotation based on mouselook that interfere
using UnityEngine; using System.Collections;
/// MouseLook rotates the transform based on the mouse delta. /// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character: /// - Create a capsule. /// - Add the MouseLook script to the capsule. /// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it) /// - Add FPSInputController script to the capsule /// -> A CharacterMotor and a CharacterController component will be automatically added.
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform. /// - Add a MouseLook script to the camera. /// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.) [AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour { public bool rotateChildCameraforUpDown; //used to specidy to use my modified camera look for up / down public Camera childCamera; public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXAndY; public float sensitivityX = 15F; public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
if(rotateChildCameraforUpDown==false)
{
rotationY = transform.localEulerAngles.x + Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else
{
rotationY = transform.localEulerAngles.x + Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(0, rotationX, 0);
//transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.rotation.y, 0);
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
What does localEulerAngles actually mean? I looked it up on the documentation, but am still a bit confused. I understand that it stores the objects rotation according to the parent object as a quaternion. I understand the quaternion part, but by parent, it means, that whatever parented object(the objects are parent / child)will tell its rotation as referenced form the parent object, rather than form 0,0,0.
How do i apply a rotation to a child camera object? I defined a variable as type "camera" and used the inspector to assign the child, but i try to assign rotation to only its y values, but how do i keep the other values the same. Would i use "localEulerAngles.x" and "localEulerAngles.z"?
I have the latest blender 3d program, and i cannot import the .blend project files directly. i have to export them as an .fbx. I have unity 3.3 pro.
Does unity support gimp? or do i have to always export to photoshop formats?
When i model, do i HAVE to apply the texture as a skin in unity, or can i have each part be a different texture and just reference them? Doesn't unity support model texture coordinates that tell locations of materials for meshes?
If i have to use a skin, would i be forced to use "uv unwrapper" or similar program to create the base skin and then add in my textures to create the model skin?
Do any of you know any updated fps tutorials, and tutorials that cover gui and menus?
I hope i'm not asking too many questions at once. I just love unity so far and need a little help aside from documentation.
I hope I've provided enough information to be able to let you all help me, if not just ask for other information.
Wow, that is a big question, but I think you may just have to reload it it all... not sure but
You should also edit your question, select the code blocks, and hit the 101/010 button to format it as code.
Answer by 1337GameDev · Jun 08, 2011 at 02:53 AM
ok, thanks for the response. That makes sense with the mouse, to model rotation / movement. I didnt think that part of the code referenced that part. it looked like it was grabby the model's y rotation, and applying it to it's x rotation. Which confused me.
I imported the model from google sketchup by exporting it as google earth and extracting the .dae file form the output file (which is a zip structure). I then imported into blender and saved, with no errors. The exported model imports fine, but i dont want to export each time i make a minor change.
Do you happen to know why the script doesn't work when i revert it to its former state where it did work in modifying the x and y of the model based on the mouses x/y.
Do i have to use the get child method? well i think its a function in c#... or can just just reference the object in the inpscetor to test my code?
I wondered if unity would recognize gimps unique file format for its projects, rather than using more time and exporting each time.
NOTE TO MYSELF: Next time I could really leave a comment instead of a answer. I am such a dummy! I could also really understand how this site works. The fact that is not a forum is unclear to me. - Orangelightning.
... Did you even read DaveA's comment below your last answer? Don't use the answer function to comment. You want to keep it clean? You've just messed the system up. Q&A is not a forum.
I would direct you to the FAQs but they aren't set up since we moved to qato ...
I left a note for you. I thought you might like it. :)
You can edit your questions and comments if you want to clean things up.
Sorry I don't have time to analyze that script right now.
You can use the inspector and a public var, sure. Or you can code it with GetComponentInChild. or you can use GameObject.Find.
You may be able to dump the dae file from sketchup directly into your assets. Good luck, Google's DAE are notoriously off-spec, but some work.
Is it that much work to save jpg or png from Gimp?
I am confused then as to how this site "works". Why would they put an answer box, and have a comment box as well? Why wouldn't they remove the answer box if they didn't want people using it?
It is not much work to save form gimp, but to make a $$anonymous$$or change and go through like 7 boxes just to epxort the file is a pain. And doing this EVERY time, for EVERY change, for EVERY material is worse. I just wanted to save a bunch of time i would waste.
The script is the standard assets script, but for some reason it refuses to work. I change it to modify the x rotation (up/down) and it didnt work and i changed it back and it wont work.
Answer by 1337GameDev · Jun 08, 2011 at 02:16 AM
Oh i just didnt want to spam and monitor like 20 different topics. if these got answered well this coukd be a nice topic to sticky if others have this same question, to reduce clutter.
Don't answer a comment with an Answer. Please use Comments to comment on a comment. If you click on your user name it will list all the questions you've asked so it's easy to find your answers when they come.
Answer by DaveA · Jun 08, 2011 at 02:25 AM
Generally you have a character (body) which has a child object containing the camera (head and eyes). You rotate the body around Y (vertical) and head around X (pitch/tilt up/down). So typically a character controller does the movement of the body, and mouselook handles the head/camera.
Mouse input has X going horizontally and Y vertically. But you spin the model on the Y axis when moving the mouse in X, and head around its X when the mouse moves on mouse Y.
localEulerAngles are the pitch (x), yaw (y), and roll (z) angles of the object the script is on. They are in that object's coordinate space (not world space). It's a convenient way of saying, for example, 'look left' (by rotating -90 around Y) no matter which way the parent (body) is oriented.
If you want to 'lean' left/right, you'd use localEulerAngles.z. Well, if it's on the head (camera), that would lean just your head. To lean the body, do that on the parent of the camera (body).
If you put a mouselook script on the same object as the (child) camera, it just works. If you wanted to put mouselook on the body for some reason, you'd have to get the child and apply it, maybe like GetComponentInChildren(Camera).transform.localEulerAngle.x = 0; or something.
Unity supports image formats which Gimp can save to. If Gimp is your default editor for those file types, double-clicking a texture should launch it.
Unity should be loading .blend files (at least 2.49), so something must be strange. Don't use fancy shaders in Blender, keep it simple. Also Collada 1.4 is a good choice.
You should skin your models in blender (UV mapping). Each mesh in Unity can have one material, but meshes can have sub-meshes. A material contains one or more colors, one or more textures, and a shader (to combine those). So each mesh which has unique properties will have its own material, but you can certainly reuse the same material on different meshes. Blender has a pretty good set of UV mapping (unwrapping) tools.
You can google the rest. All this stuff has been asked and answered somewhere.