Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411426 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 10:49:56 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Trying to Write Script to Move Camera Over Shoulder
Pages: [1]
Print
Author Topic: Trying to Write Script to Move Camera Over Shoulder  (Read 594 times)
Henry_Oswald
Level 1
*



View Profile WWW
« on: September 19, 2014, 09:26:08 PM »

Hey there. I've been trying to get my camera to smoothly move over the shoulder of my character when the right mouse button is pressed down (Kind of like in the new Tomb Raider. Like an aim or a look function). I've got some stuff written, but I'm really just swinging around blindly. If any of you guys could help me out, that would be much appreciated. Thanks!

Code:
using UnityEngine;
using System.Collections;

public class ShoulderCamera : MonoBehaviour {

public GameObject character;
public Camera camera;
public float smoothing = 1;
private Vector3 cIntPos;
private Vector3 cZoomPos;

// Update is called once per frame
void Update () {
if(Input.GetKeyDown ("mouse2")){
camera.transform.position = Vector3.MoveTowards(camera.transform.position,
                                               cIntPos,
                                               Time.deltaTime * smoothing);
}
}
}

Another note, I'm a little scared that this script will conflict with another one that allows for the camera to orbit around the character. I think this can be avoided with a simple if statement, but I'm still a little iffy about it.
« Last Edit: September 24, 2014, 09:41:47 PM by Henry_Oswald » Logged

"Do you think love can bloom even on the battlefield?"
- Otacon
Quill Devlog: http://forums.tigsource.com/index.php?topic=38129.0
Home Website: http://psychospectacle.weebly.com
Watch us on IndieDB: http://www.indiedb.com/games/quill
Follow us on Twitter: https://twitter.com/PsychoSpecs
Henry_Oswald
Level 1
*



View Profile WWW
« Reply #1 on: September 24, 2014, 09:44:26 PM »

Ok, so I've redone some things. This code is a modification to a script that orbits the camera around the player. When rmb is pressed the camera is intended to zoom into a shoulder/aim view. Right now I'm still getting some errors, for instance when I press rmb the camera resets to directly above the character pointing straight down. Any suggestions on how to fix this?

Code:
var target : Transform;
var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

/*Point that the camera should zoom in to when right
mouse button is clicked.*/
public var sOrigin : Transform;
 
@script AddComponentMenu("Camera-Control/Mouse Orbit")

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

// Make the rigid body not change rotation
    if (rigidbody)
rigidbody.freezeRotation = true;
}

function LateUpdate () {

/*Zoom camera to fixed position over shoulder when
right mouse button is clicked*/
if(Input.GetMouseButtonDown(1)){
x = sOrigin.position.x;
y = sOrigin.position.y;

var sRotation = Quaternion.Euler(y,x,0);
var sPosition = srotation * Vector3(x,y,-distance);

transform.rotation = sRotation;
transform.position = sPosition;
}
else{

/*Orbit camera around player character*/
if (target) {
    x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

y = ClampAngle(y, yMinLimit, yMaxLimit);
       
    var rotation = Quaternion.Euler(y, x, 0);
    var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
       
    transform.rotation = rotation;
    transform.position = position;
}
}
}

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Logged

"Do you think love can bloom even on the battlefield?"
- Otacon
Quill Devlog: http://forums.tigsource.com/index.php?topic=38129.0
Home Website: http://psychospectacle.weebly.com
Watch us on IndieDB: http://www.indiedb.com/games/quill
Follow us on Twitter: https://twitter.com/PsychoSpecs
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic