- Home /
3rd pserson shooter camera issue
I am developing a third person shooter game for mobiles. Here I am using the following script to control the camera movement.
using UnityEngine; using System.Collections;
public class mouse_controller : MonoBehaviour {
[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
Vector2 _mouseAbsolute;
Vector2 _smoothMouse;
public Vector2 clampInDegrees = new Vector2(80, 20);
public bool lockCursor;
public Vector2 sensitivity = new Vector2(0.9, 0.9);
public Vector2 smoothing = new Vector2(2, 2);
public Vector2 targetDirection;
public Transform target;
public float range = 1000;
public float force = 1000;
// Use this for initialization
void Start ()
{
targetDirection = transform.localRotation.eulerAngles;
}
// Update is called once per frame
void Update ()
{
transform.position = new Vector3 (target.position.x,target.position.y,target.position.z);
if(Input.mousePosition.x > Screen.width/1.5f && Input.mousePosition.y < Screen.height/3)
{
}
else
{
if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Moved)
{
Screen.lockCursor = lockCursor;
var targetOrientation = Quaternion.Euler(targetDirection);
var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
_smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
_smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
_mouseAbsolute += _smoothMouse;
//--- Horizontal Movement ---
if (clampInDegrees.x < 85)
{
_mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
}
var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
transform.localRotation = xRotation;
//--- Vertical movement ---
if (clampInDegrees.y < 25)
{
_mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
}
transform.localRotation *= targetOrientation;
var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
transform.localRotation *= yRotation;
}
}
}
}
When i drag on the screen camera smoothly rotate. But the issue is when click after the fire button(for shoot)with right hand,same time user also has the provision to change the camera position with his left hand. After pressing the fire button camera suddenly switch to the left touched position without considering the Lerp values. This will happen rarely. please help me to findout the issue
I expect an output like Frontline Commando2
Thanks in advance..
At a glance I'm guessing you need to take another look at how Lerp works.
Answer by gamertest · Jun 20, 2014 at 10:34 AM
Have 1 mouse look script attached to the overall parent of your character, and make the mouse-look script only active on the x-axis. then on the head of your character, add the mouse-look script only active on the y-axis. now that's your 3rd person view. attach another camera to your head and set is to only move on the x-axis, and disable it when you turn mouse look mode to off, disable the mouse-look script on the overall parent and enable the x-axis mouse-look script on the head of your character.
Please stop guerrilla-posting spam links. Edited out.
Last chance.
Your answer
Follow this Question
Related Questions
MouseLook movement diagonal stutters... 0 Answers
MouseLook.cs:case of JavaScript 1 Answer
How to lock camera roll in C# 1 Answer
How do I disable mouse to turning right 0 Answers