Here is FPS script im using, i have it fully functioning.
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 a rigid body to the 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 FPSWalker script to the capsule
/// - 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 rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (-rotationY, Vector3.right);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}
I have another camera in the scene that drops in shapes (simple cubes) when you press spacebar and I want to be able to pick them up and set them down somewhere else. Heres a script that Im thinking about using, just wondering how to bring the two together and add additional functionality to the fps script. How can I add the pick script to the fps script and have them work together?
var pickupDistance = 3.0;
static var heldObject : GameObject;
static var itemHeld = 0;
var objectDist = -.1;
private var hand :GameObject;
function Start() {
hand = gameObject.FindWithTag("Player");
}
function Update () {
if (Input.GetMouseButtonDown(0) && itemHeld == 2) {
hand.animation.Play("drop");
heldObject.transform.parent = null;
heldObject.GetComponent(Rigidbody).isKinematic = false;
heldObject = null;
itemHeld = 0;
}
}
function OnMouseDown() {
if(itemHeld == 0) {
heldObject = gameObject;
hand.animation.Play("grab");
heldObject.transform.parent = gameObject.FindWithTag("Player").transform;
heldObject.GetComponent(Rigidbody).isKinematic = true;
var handLocation = Vector3(0,objectDist,0);
heldObject.transform.localPosition = handLocation;
itemHeld = 1;
}
}
function OnMouseUp(){
if(itemHeld == 1)
{
itemHeld = 2;
}
}