- Home /
reset camera position mouselook
hey chaps,
loving unity. such an awesome intro into programming and some of the best refs out there for budding developers. quite a few hours into my own game and i'm quite getting into it, never thought i'd even try programming but with unity it's rather fun. for the first time ever it's all making sense :) admittedly i'm poaching code to gain a basic understanding but from this weekend i'll be endeavouring to code an original game from scratch. so happy i could pee.
currently, with my practise game, i've run into a problem with the mouselook script. it works great with on a button press but when i switch it off the camera stays locked rather than returning to its original position.
Also (probably in relation to..) as per the instructions with the script i parented the cam to the player and reset the transforms. when i do this the cam snaps to the players transform, is this right? i can add some variables for distance and height to solve this but i expected the camera position to freeze and reset transforms in position behind the player where i'd placed it. anyways, here's the script chaps and many thanks in advance.
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 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 (Input.GetButton("Fire1")) { if (axes == RotationAxes.MouseXAndY) { float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
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.localEulerAngles.y, 0);
}
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
nearly a day gone by and nothing from anyone...come on chaps, don't make me lose faith. this community is supposedly legendary in the helpfulness area.
Answer by flabbers · Apr 07, 2011 at 11:08 PM
left the mouselook as it wasn't going to give me what i wanted. rather handily mouseorbit does. added a var to adjust the cams height from player transform and bingo. could have tried locking the cursor with the button press on mouselook but figured it was a bit pointless. still would like to know about reseting transforms as stated in my question above if anyone would care to enlighten me.
Your answer
Follow this Question
Related Questions
Restrict camera rotation 1 Answer
Override MouseLook/FirstPersonController 0 Answers
How can I create a grid of transforms? 3 Answers
Gaps and overlaps between platforms in the infinite runner 0 Answers
Resetting a Scene 2 Answers