Question by
Cripstat · Oct 30, 2015 at 07:08 PM ·
rotaterotate objectanglestrigonometryangleaxis
Rotating object relative to player to orient it center of camera regardless of direction.
So as you see in the code below I am creating an instance of a game object, It appears in front of the players position upon button pressDown and is destroyed on button pressUP. Currently it appears "in front" of the player based on the position the player is standing in on testing the scene. It sticks there and does not rotate with the mouse as I want it to. I tried using AngleAxis with the players current direction angle as the angle and the players current position as the axis however this only allows the game object to rotate fixed in space in front of the player. I'm not very experienced with trigonometry and so I'm lost as to why it doesn't work the way I want it to, in my head it makes sense!
using UnityEngine;
using System.Collections;
public class playerControl : MonoBehaviour {
//Movement
public float moveSpeed;
public Camera myCamera;
private float rotationY = 0.0f;
public float mouseXspeed = 1.0f;
public float mouseYspeed = 1.0f;
//Boost
public float boostSpeed = 0.10f;
//Jumping
private bool onFloor = false;
public float jumpForce;
public Rigidbody rb;
//Raycasting
//Reference to the camera already exists at myCamera
Transform camTran; //Referance to the camera Transform
RaycastHit hit = new RaycastHit();
//Casting state
private bool castingMode = false;
public string castState = "off";
//Casting interface control
//public Transform intPos;
public GameObject castINT;
Vector3 localOffset = new Vector3(0.6f,0,0);
Vector3 playerLoc = new Vector3(0,0,0);
private float playerCurretnEuler;
// Use this for initialization
void Start () {
Cursor.visible = false;
//Settging camTran
camTran = myCamera.transform;
}
// Update is called once per frame
void Update () {
playerLoc = transform.position;
playerCurretnEuler = this.transform.localEulerAngles.y;
Debug.Log (playerCurretnEuler);
// Player Movement
if (Input.GetKey (KeyCode.W)) {
this.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
}
if (Input.GetKey (KeyCode.A) && onFloor == true) {
this.transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
}
if (Input.GetKey (KeyCode.S) && onFloor == true) {
this.transform.Translate(Vector3.back * Time.deltaTime * moveSpeed);
}
if (Input.GetKey (KeyCode.D) && onFloor == true) {
this.transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
}
if(Input.GetKeyDown(KeyCode.C)){
castingMode = true;
Cursor.visible = true;
castState = "on";
GameObject mycastINT = Instantiate(castINT);
GameObject intParent = GameObject.Find("mainCamera");
mycastINT.transform.parent = intParent.transform;
mycastINT.transform.rotation = Quaternion.AngleAxis(playerCurretnEuler, playerLoc);
mycastINT.transform.position = intParent.transform.position + localOffset;
}
if(Input.GetKeyUp(KeyCode.C)){
castingMode = false;
Cursor.visible = false;
castState = "off";
//Accessing casting Script to remove it from play
GameObject castInt = GameObject.Find("castInterface(Clone)");
castInterface castScript = castInt.GetComponent("castInterface") as castInterface;
castScript.removeSelf();
}
//Boosting Control
//Boost On
if (Input.GetKeyDown(KeyCode.LeftShift)) {
moveSpeed = moveSpeed +boostSpeed;
}
//Boost off
if (Input.GetKeyUp(KeyCode.LeftShift)) {
moveSpeed = moveSpeed -boostSpeed;
}
if (castingMode == false) {
//Player Looking
float rotationX = Input.GetAxis ("Mouse X");
this.transform.Rotate (0.0f, rotationX * mouseXspeed, 0.0f);
if (rotationY > -85.0f && rotationY < 85.0f) {
rotationY += Input.GetAxis ("Mouse Y") * mouseYspeed;
myCamera.transform.localEulerAngles = new Vector3 (Mathf.Clamp (-rotationY, -85.0f, 85.0f), myCamera.transform.localEulerAngles.y, 0.0f);
}
if (rotationY < -84.0f) {
rotationY = -83.5f;
}
if (rotationY > 84.0f) {
rotationY = 83.5f;
}
}
//Jumping
if (Input.GetKeyDown(KeyCode.Space) && onFloor == true) {
rb.AddForce(transform.up * jumpForce);
}
//Raycasting
Ray ray = new Ray (camTran.position, camTran.forward);
if (Physics.Raycast (ray, out hit, 3)) {
if(hit.collider.name == "Door"){
if(Input.GetMouseButtonDown(0)){
GameObject theDoor = hit.collider.gameObject;
doorControl doorScript = theDoor.GetComponent("doorControl") as doorControl;
doorScript.clickOnDoor();
}
}
}
}
void OnCollisionEnter(Collision col){
if (col.transform.tag == "floor") {
onFloor = true;
}
}
void OnCollisionExit(Collision col){
if (col.transform.tag == "floor") {
onFloor = false;
}
}
void OnTriggerEnter(Collider col){
if (col.transform.name == "collect") {
Destroy(col.gameObject);
}
}
}
Thanks for any help you can offer!
Comment