• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple rotations make a mess! 0 Answers

trying to rotate a 2D sprite, wait 3 seconds, rotate it back 1 Answer

how do I rotate an object without affecting later rotation? 0 Answers

how to get object rotation? 1 Answer

How to smoothly rotate an object 180 degrees each time you press the spacebar in 3D 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges