Hello- complete newb here any help would be appreciated.
I am using the following script to move my camera between 2 (or more) viewing points
public class CameraPos : MonoBehaviour
{
public float smooth = 2;
private Vector3 newPosition;
void Awake()
{
newPosition = transform.position;
}
void Update ()
{
PositionChanging ();
}
public void PositionChanging ()
{
Vector3 positionA = new Vector3(-45,15, 0);
Vector3 positionB = new Vector3(-1, 4, 8);
if(Input.GetKeyDown(KeyCode.Q))
newPosition = positionA;
if(Input.GetKeyDown(KeyCode.E))
newPosition = positionB;
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
}
which works, but interferes with the scrip I am using to fly my camera around, as if it has a rubber band to the camera position, it accepts keyboard input to fly a direction and then snaps back to it’s start position (A,B etc) the FlyCam script is below
using UnityEngine;
using System.Collections;
public class FlyCamera : MonoBehaviour {
/*
wasd : basic movement
shift : Makes camera accelerate
space : Moves camera on X and Z axis only. So camera doesn't gain any height*/
[SerializeField] Transform map;
public Transform target;
public float mainSpeed = 15.0f; //regular speed
float shiftAdd = 250.0f; //multiplied by how long shift is held. Basically running
float maxShift = 1000.0f; //Maximum speed when holdin gshift
public float camSens = 0.0f; //How sensitive it with mouse
public float panSpeed = 4.0f; // Speed of the camera when being panned
private Vector3 lastMouse = new Vector3(0, 0, 0); //kind of in the middle of the screen, rather than at the top (play)
private float totalRun = 1.0f;
private bool isPanning;
private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts
void Update ()
{
//make camera sensitive on mouse click
if (Input.GetMouseButton(0)) {
camSens = Mathf.Abs (.2f);
}
if (!Input.GetMouseButton(0)){
camSens = Mathf.Abs (0);
}
lastMouse = Input.mousePosition - lastMouse ;
lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0 );
lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x , transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
//Mouse camera angle done.
//panning on and off
if(Input.GetMouseButtonDown(2))
{
// Get mouse origin
mouseOrigin = Input.mousePosition;
isPanning = true;
}
if (!Input.GetMouseButton(2)) isPanning=false;
if (isPanning)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0);
transform.Translate(move, Space.Self);
}
//Keyboard commands
//6float f = 0.0f;
Vector3 p = GetBaseInput();
if (Input.GetKey (KeyCode.LeftShift)){
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
}
else{
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
p = p * mainSpeed;
}
p = p * Time.deltaTime;
Vector3 newPosition = transform.position;
if (Input.GetKey(KeyCode.Space)){ //If player wants to move on X and Z axis only
transform.Translate(p);
newPosition.x = transform.position.x;
newPosition.z = transform.position.z;
transform.position = newPosition;
}
else{
transform.Translate(p);
}
}
private Vector3 GetBaseInput() { //returns the basic values, if it's 0 then it's not active.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey (KeyCode.W)){
p_Velocity += new Vector3(0, 0 , 1);
}
if (Input.GetKey (KeyCode.S)){
p_Velocity += new Vector3(0, 0, -1);
}
if (Input.GetKey (KeyCode.A)){
p_Velocity += new Vector3(-1, 0, 0);
}
if (Input.GetKey (KeyCode.D)){
p_Velocity += new Vector3(1, 0, 0);
}
return p_Velocity;
}
void GetInputForAxis(Vector3 dir,string axis,float response)
{
float move = 0.0f;
float speed = Input.GetAxis(axis);
move += speed * response;
if (speed != 0.0f)
{
transform.Translate(dir * move);
}
}
}