- Home /
Camera Effect + Translating US to C#
Hello everyone, i have a camera script that makes the cam follow player and this was working great until we wanted to add an effect that would have the cam move further from the player and zoom in based on the player's speed, my artist tells me that they use this in movies and it should look great.
Now i figured out how to do this(untested and not sure if there's a better way) but the problem is that i can't access the speed because this script is in unityscript and the other one in c#, placing the c# script in standard assets isn't an option because other scripts depend on it aswell and changing the script execution time doesn't take away the thrown error so i can't press play and test.
I tried translating it to c# but that causes issues because this line does not work in c#: transform.position.y = currentHeight; Throws error: Cannot modify the return value of Transform.Position because it's not a variable
And i don't know how to cache some of the variables, you'll see which ones i'm talking about Full (US) script:
/*This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.
For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.*/
var target : Transform;
var playerC : PlayerControl;
var cam : Camera;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// Damping
var heightDamping = 2.0;
var rotationDamping = 3.0;
//Cache for performance
var wantedRotationAngle;
var wantedHeight;
var currentRotationAngle;
var currentHeight;
var currentRotation;
function Awake(){
playerC = GameObject.FindWithTag ("Player").GetComponent(PlayerControl);
cam = GetComponent(Camera);
}
function LateUpdate () {
// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
//Change distance and fov based on player speed
if(playerC.currThrust > 1050) distance += currThrust/200; cam.fieldOfView += currThrust/200;
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
Translated so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour {
Transform target;
PlayerControl playerC;
Camera cam;
// The distance in the x-z plane to the target
float distance = 10;
// the height we want the camera to be above the target
float height = 5;
// Damping
float heightDamping = 2;
float rotationDamping = 3;
//Cache for performance ****Not sure how to translate these****
/*var wantedRotationAngle;
var wantedHeight;
var currentRotationAngle;
var currentHeight;
var currentRotation;*/
void Awake()
{
playerC = GameObject.FindWithTag("Player").GetComponent<PlayerControl>();
cam = GetComponent<Camera>();
}
void LateUpdate()
{
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
if (playerC.currThrust > 1050) distance += playerC.currThrust / 200; cam.fieldOfView += playerC.currThrust / 200;
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera *****Here is the issue*****
transform.position.y = currentHeight;
transform.LookAt(target);
}
}
Thanks in advance
Answer by kritoa · Apr 28, 2017 at 06:02 AM
You can't adjust transform.position.y directly, but you can do something like:
Vector3 tempPosition = transform.position;
tempPosition.y = currentHeight;
transform.position = tempPosition;
Those variables you want to cache - you can just do what you did with all the other variables - it looks like they are all floats, so:
float wantedRotationAngle;
float wantedHeight;
//etc...
Thanks that works and i don't know why i got so confused about the variables, it is just currentRotation that needs to be a Quaternion.
Answer by BenderBRodriguez · Apr 28, 2017 at 06:01 AM
I tried translating it to c# but that causes issues because this line does not work in c#: transform.position.y = currentHeight; Throws error: Cannot modify the return value of Transform.Position because it's not a variable
The transform.position is a (Vector3) property that can be set or get. You just have to create a new Vector3 with the new position coords.
transform.position = new Vector3( new_x_value, new_y_value, new_z_value );
Heres a simple fix
// Set the height of the camera *****Here is the issue*****
transform.position = new Vector3( transform.position.x, currentHeight, transform.position.z );
Answer by AtGfx · Apr 28, 2017 at 07:36 AM
Creating a vector each time you want to move your camera may not be the optimal thing to do (variable creation + GB...)
Don't forget that you have the Transform.Translate method to modify position vectors which should be better !
local variables doesn't need to be "created". They are allocated automatically on the stack when you enter the method and removed when you leave it. There's no overhead. Also Vector3 is a struct and therefore a value type. So the GC is out of the question
Your answer
Follow this Question
Related Questions
Natural camera sway 1 Answer
Animated camera problems 0 Answers
WorldToScreenPoint 0 Answers
Translate C# into Js 1 Answer
Problem Javascript to C# 3 Answers