Question by
thunderclaws · Sep 11, 2015 at 08:29 AM ·
getcomponentthird-personthirdpersoncontrollerthird-person-camera
need general help with camerascript
okay here is the script for my camera currently it has everything i need it to do, but the problem is that i want the vector 3 relpos (relative position) sent to my movescript, what i want it to do there is to determine the forward direction for the character. you know when you have a third person game you rotate your camera around your character and you press W he starts walking in the direction the camera is facing. so if anyone has any tips on: getting this script to make friends and communicate. make it function the way i want. cleaning up this nightmare of badly placed variables. thank you.
using UnityEngine;
using System.Collections;
public class Camerascript : MonoBehaviour {
float xrot = 1.5f;
float yrot = -1.0f;
float newxrot = 1.5f;
float newyrot = -1.0f;
float z;
float x1;
float x2;
float y;
float posy;
float posx;
float posz;
float camspeedx;
float camspeedy;
float camspeedw;
float forwards;
Vector3 campos;
public Transform player;
//settings
public float zoom = 3.0f;
public float zoom2 = 3.0f;
public float sensitivity = 0.5f;
public float smooth = 2f;
public string targetplayer = "Cube";
public string XInput = "Mouse X";
public string YInput = "Mouse Y";
public float Xmultiplier = 1.0f;
public float Ymultiplier = 1.0f;
public Vector3 relpos;
void Start () {
}
void Update () {
//player position
GameObject go = GameObject.Find (targetplayer);
player = go.GetComponent <Transform> ();
//zoom
int layerMask = 1 << 8;
layerMask = ~layerMask;
int layerMask2 = 1 << 9;
layerMask2 = ~layerMask2;
RaycastHit hit;
RaycastHit hit2;
zoom2 -= Input.GetAxis ("ScrollWheel") * 5.0f;
if (Physics.Raycast (player.transform.position, transform.position, out hit, zoom +1.5f, layerMask)) {
//if (Physics.SphereCast (player.transform.position, 0.2f, transform.position, out hit2, zoom, layerMask2)) {
// zoom = Mathf.Clamp (zoom2, 0.1f, hit2.distance);
//}else{
zoom = Mathf.Clamp (zoom2, 0.1f, hit.distance);
//}
} else {
zoom = Mathf.SmoothDamp(zoom, zoom2, ref camspeedw, 0.2f);
}
//input
float camx = -Input.GetAxis (XInput);
float camy = -Input.GetAxis (YInput);
//rotation
newxrot += sensitivity * Xmultiplier * camx;
newyrot += sensitivity * Ymultiplier * camy;
newyrot = Mathf.Clamp(newyrot, -3.0f, -0.2f);
xrot = Mathf.SmoothDamp(xrot, newxrot, ref camspeedx, Time.deltaTime * smooth);
yrot = Mathf.SmoothDamp(yrot, newyrot, ref camspeedy, Time.deltaTime * smooth);
//orientation
z = Mathf.Sin (xrot);
x1 = Mathf.Cos (xrot);
x2 = Mathf.Sin (yrot);
y = Mathf.Cos (yrot);
Relpos ();
//direction
transform.LookAt(player);
}
public class Test{
static float TopKek = 9001.0f;
}
public void Relpos()
{
Vector3 position;
relpos.y = player.position.y + y * zoom;
relpos.x = player.position.x + x1 * x2 * zoom;
relpos.z = player.position.z + z * x2 * zoom;
position.x = relpos.x;
position.y = relpos.y;
position.z = relpos.z;
transform.position = position;
}
}
Comment