- Home /
Look to the side-Scripting Problem
I have looked everywhere and have yet to find a way to make it so that my 3rd person camera can rotate slightly left and right when I press a key...
I don't even have code that's failing because I don't know what code to use.
What I need is to have my camera's rotation offset (without moving my character) so that I can glance to the right and left of it. The important thing is to have the camera's rotation move back to its original position when I let go of the key(s).
Answer by DNP · Aug 04, 2012 at 08:53 PM
Make a block on both sides, and one facing where the camera was originally looking, then do a code like this.
var defaultPos : Transform;
var target : Transform;
var target2 : Transform;
function Update(){
if (Input.GetKeyDown(KeyCode.Q)){
transform.LookAt(target);
}
if (Input.GetKeyUp(KeyCode.Q)){
transform.LookAt(defaultPos);
}
if (Input.GetKeyDown(KeyCode.E)){
transform.LookAt(target2);
}
if (Input.GetKeyUp(KeyCode.E)){
transform.LookAt(defaultPos);
}
}
Answer by Tyoma · Aug 07, 2012 at 11:23 AM
I'm trying this with a flight simulator joystick and the only code that seems to have worked is
public var Twist = 0.00;
public var _twist = 0.00;
public var maxTwist = 45;
public var smooth = 2.0;
function LateUpdate () {
}
function Update () {
Twist =(Input.GetAxis("Twist")); //Joystick Axis 3Rd
twist();
var target = Quaternion.Euler (0, _twist, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target,
Time.deltaTime * smooth);
}
function twist() {
if (Twist > 0.9)
_twist = maxTwist;
if (Twist <= 0.9 && Twist > 0.8)
_twist = (maxTwist * 0.9);
if (Twist <= 0.8 && Twist > 0.7)
_twist = (maxTwist * 0.8);
if (Twist <= 0.7 && Twist > 0.6)
_twist = (maxTwist * 0.7);
if (Twist <= 0.6 && Twist > 0.5)
_twist = (maxTwist * 0.6);
if (Twist <= 0.5 && Twist > 0.4)
_twist = (maxTwist * 0.5);
if (Twist <= 0.4 && Twist > 0.3)
_twist = (maxTwist * 0.4);
if (Twist <= 0.3 && Twist > 0.2)
_twist = (maxTwist * 0.3);
if (Twist <= 0.2 && Twist > 0.1)
_twist = (maxTwist * 0.2);
if (Twist <= 0.1 && Twist >= -0.1)
_twist = 0;
if (Twist < -0.1 && Twist >= -0.2)
_twist = -(maxTwist * 0.2);
if (Twist < -0.2 && Twist >= -0.3)
_twist = -(maxTwist * 0.3);
if (Twist < -0.3 && Twist >= -0.4)
_twist = -(maxTwist * 0.4);
if (Twist < -0.4 && Twist >= -0.5)
_twist = -(maxTwist * 0.5);
if (Twist < -0.5 && Twist >= -0.6)
_twist = -(maxTwist * 0.6);
if (Twist < -0.6 && Twist >= -0.7)
_twist = -(maxTwist * 0.7);
if (Twist < -0.7 && Twist >= -0.8)
_twist = -(maxTwist * 0.8);
if (Twist < -0.8 && Twist >= -0.9)
_twist = -(maxTwist * 0.9);
if (Twist < -0.9)
_twist = -maxTwist;
}
It's great and all the only problem I have with this code is that the character moves along with the camera. It might be because I'm also using the default code that the 3rd person character/camera come with.
If anyone has any ideas as to how I can keep my character from rotating slightly with the camera I'd appreciate it.
Edit: It actually messes with the 3rd person camera script Soooo yeah.... ignore what I said about this code working.