- Home /
This post has been wikified, any user with enough reputation can edit it.
Keyboard / Mouse control for FPS
Hi! I m brand new to Unity. I have an issue regarding first person camera control.
I have found a useful scrip online from defjr.com which goes like this.
using UnityEngine;
using System.Collections;
public class SimpleControls : MonoBehaviour {
public Transform cameraPivot;
public CharacterController character;
private Vector3 initialPosition;
private Vector3 currentPosition = Vector3.zero;
private float speed = 2.5f;
private Vector2 rotationSpeed = new Vector2 (75, 50);
private float currentRotation = 75f;
private float rotationLimitX = 150f;
private bool changingView = false;
void Update ()
{
if (Mathf.Abs (Input.GetAxis ("Horizontal")) < 0.1f)
currentRotation = rotationSpeed.x;
else
currentRotation += 1f;
if (currentRotation > rotationLimitX) currentRotation = rotationLimitX;
character.SimpleMove(character.transform.forward * speed * Input.GetAxis("Vertical"));
transform.Rotate(0, currentRotation * Time.deltaTime * Input.GetAxis ("Horizontal"), 0, Space.World);
if (Input.GetMouseButtonDown(0)) {
changingView = true;
initialPosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
changingView = false;
if (changingView) {
currentPosition.x = Mathf.Clamp ((Input.mousePosition.x - initialPosition.x) / 100, -3, 3);
currentPosition.y = Mathf.Clamp ((Input.mousePosition.y - initialPosition.y) / 100, -2, 2);
currentPosition.x *= rotationSpeed.x;
currentPosition.y *= rotationSpeed.y;
currentPosition *= Time.deltaTime;
transform.Rotate (0, currentPosition.x, 0, Space.World);
cameraPivot.Rotate (-currentPosition.y, 0, 0);
}
if (Mathf.Abs( Input.GetAxis("Vertical")) > 0.25f )
cameraPivot.rotation = Quaternion.Slerp (cameraPivot.rotation, transform.rotation, Time.deltaTime);
}
}
This is close to what I want. But m looking for something like on this page http://demo.memcosoft.com/en/repository/9-Shams-ArRiyadh-[Made-by-iXiBiT].html Hope its alright to paste link.
Any help on getting first person to behave like the link above.
Thanks and Regards, Vinay
Comment
Your answer
Follow this Question
Related Questions
Fps Rigidbody controller with mouse cursor? 0 Answers
How can I produce the typical FPS mouse steering effect? 2 Answers
Best Practice Multiple Control Methods 1 Answer
trespasser arm 0 Answers
2 players. Controlling just 1. 3 Answers