- Home /
Question by
GroundstompStudios · Feb 17, 2020 at 05:32 AM ·
c#camera-movementcamera rotate
How to make camera turn on right click?
Does anyone know how I would make the camera move right, left, up, and down when I hold right click? I've attempted it with this code and it doesn't work. Would appreciate if someone could help me out!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyCamera : MonoBehaviour
{
float mainSpeed = 25.0f;
float shiftAdd = 40.0f;
float maxShift = 1000.0f;
float camSens = 0.05f;
private Vector3 lastMouse = new Vector3(255, 255, 255);
private float totalRun = 1.0f;
void Update()
{
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;
//float 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))
{
transform.Translate(p);
newPosition.x = transform.position.x;
newPosition.z = transform.position.z;
transform.position = newPosition;
}
else
transform.Translate(p);
}
private Vector3 GetBaseInput()
{
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;
}
}
Comment
Your answer
Follow this Question
Related Questions
Confining Mouse Look on X axis 0 Answers
Adjust camera orientation through code 0 Answers
I only rotated X, but Y and Z are shifting as well when I play the game. 1 Answer
Lean/Peak system 1 Answer