Camera Modification Help
Hello Unity! I need some help modifying this script. when you click and hold the left mouse button, you can look around the character and move like normal. I'm trying to figure out how i can make the character not snap in a direction when i right click. what i need is the ability to change this on the fly also. my character is able to control a boat but boats don't snap in a single direction. i was wondering if anyone can help me figure out a way with either a bool or int, to make the camera have two controls. One where the character can left click and look around while right clicking to turn, and one where on the ship you can only look around while another script controls the movement. (P.S. I have the Movement Script) All my Code is at the Bottom.
Movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movment : MonoBehaviour {
public GameObject targetCamera;
public string PlayerControls;
public bool IsOnTheGround;
public bool IsInOcean;
public int MoveSpeed;
public int TurnSpeed;
public int JumpForce;
public int SwimForce;
public string BoatControls;
public bool BoatActive;
public int BoatSpeed;
public int BoatTurnSpeed;
public GameObject Boat;
private Rigidbody RB;
void Start(){
RB = GetComponent<Rigidbody> ();
Boat.SetActive (false);
}
void Update(){
CallShip ();
if (!BoatActive) {
if (Input.GetKey (KeyCode.W)) {
this.gameObject.transform.Translate (new Vector3 (0, 0, 1) * Time.deltaTime * MoveSpeed);
}
if (Input.GetKey (KeyCode.S)) {
this.gameObject.transform.Translate (new Vector3 (0, 0, -1) * Time.deltaTime * MoveSpeed);
}
if (Input.GetKey (KeyCode.A)) {
transform.Rotate (Vector3.down * Time.deltaTime * TurnSpeed);
}
if (Input.GetKey (KeyCode.D)) {
transform.Rotate (Vector3.up * Time.deltaTime * TurnSpeed);
}
}
if (BoatActive) {
if (Input.GetKey (KeyCode.W)) {
this.gameObject.transform.Translate (new Vector3 (0, 0, 1) * Time.deltaTime * BoatSpeed);
}
if (Input.GetKey (KeyCode.S)) {
this.gameObject.transform.Translate (new Vector3 (0, 0, -1) * Time.deltaTime * BoatSpeed);
}
if (Input.GetKey (KeyCode.A)) {
transform.Rotate (Vector3.down * Time.deltaTime * BoatTurnSpeed);
}
if (Input.GetKey (KeyCode.D)) {
transform.Rotate (Vector3.up * Time.deltaTime * BoatTurnSpeed);
}
}
if (Input.GetMouseButton (1)) {
transform.rotation = Quaternion.Euler (transform.eulerAngles.x, targetCamera.transform.eulerAngles.y, transform.eulerAngles.z);
}
//Jump Mechanic
if (Input.GetKeyDown (KeyCode.Space) && IsOnTheGround) {
RB.AddForce (new Vector3 (0, JumpForce, 0), ForceMode.Impulse);
}
if (IsInOcean) {
RB.drag = 5;
MoveSpeed = 3;
if(Input.GetKey(KeyCode.Space)){
RB.AddForce (new Vector3 (0, SwimForce, 0), ForceMode.Force);
}
} else {
RB.drag = 1;
MoveSpeed = 5;
}
}
void OnTriggerEnter(Collider Col){
if (Col.tag == "Ocean") {
IsInOcean = true;
}
if (Col.tag == "Land") {
RB.constraints = ~RigidbodyConstraints.FreezePositionY;
RB.constraints = RigidbodyConstraints.FreezeRotation;
BoatActive = false;
Boat.SetActive (false);
}
}
void OnTriggerExit(Collider Col){
if (Col.tag == "Ocean") {
IsInOcean = false;
}
}
//Boat Mechanics
void CallShip(){
if (IsInOcean) {
if (Input.GetKeyDown (KeyCode.F)) {
this.transform.position = new Vector3 (this.transform.position.x, 5, this.transform.position.z);
RB.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotation;
BoatActive = true;
Boat.SetActive (true);
}
}
}
}
And the Camera:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraControllerTest : MonoBehaviour {
public Transform cameraTarget;
public int RIGHT_M = 1;
public int LEFT_M = 0;
private float x = 0.0f;
private float y = 0.0f;
private int mouseXSpeedMod = 3;
private int mouseySpeedMod = 3;
public float maxViewDistance = 25.0f;
public float minViewDistance = 1.0f;
public int zoomRate = 30;
public float lerpRate = 1.0f;
private float distance = 3.0f;
private float desiredDistance;
private float correctedDistance;
private float currentDistance;
public float cameraTargetHeight = 1.0f;
// Use this for initialization
void Start () {
Vector3 angles = transform.eulerAngles;
x += angles.x;
y -= angles.y;
currentDistance = distance;
desiredDistance = distance;
correctedDistance = distance;
}
void LateUpdate () {
if (Input.GetMouseButton (0)) {
x += Input.GetAxis ("Mouse X") * mouseXSpeedMod;
y -= Input.GetAxis ("Mouse Y") * mouseySpeedMod;
}
else if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0){
float targetRotationAngle = cameraTarget.eulerAngles.y;
float cameraRotaionAngle = transform.eulerAngles.y;
x = Mathf.LerpAngle (cameraRotaionAngle, targetRotationAngle, lerpRate * Time.deltaTime);
}
y = ClampAngle (y, -50, 50);
Quaternion rotation = Quaternion.Euler (y, x, 0);
desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
desiredDistance = Mathf.Clamp (desiredDistance, minViewDistance, maxViewDistance);
correctedDistance = desiredDistance;
Vector3 position = cameraTarget.position - (rotation * Vector3.forward * desiredDistance);
RaycastHit collisionHit;
Vector3 cameraTargetPosition = new Vector3 (cameraTarget.position.x, cameraTarget.position.y + cameraTargetHeight, cameraTarget.position.z);
bool isCorrected = false;
if(Physics.Linecast(cameraTargetPosition, position, out collisionHit)){
position = collisionHit.point;
correctedDistance = Vector3.Distance (cameraTargetPosition, position);
isCorrected = true;
}
currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf .Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomRate) : correctedDistance;
position = cameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -cameraTargetHeight, 0));
transform.rotation = rotation;
transform.position = position;
}
private static float ClampAngle(float angle, float min, float max){
if (angle < -360) {
angle += 360;
}
if (angle > 360) {
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}
}
Your answer
Follow this Question
Related Questions
How to make an object go the direction it is facing? (Im new) 0 Answers
How to make an object go the direction it is facing? 0 Answers
How to have your player controls change while your camera rotates? 0 Answers
Camera Jitters When Displacing and Rotating Smoothly 0 Answers
how to make forward relative to the view of the camera 0 Answers