Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Moving And Rotation 2D Object (Touch) 0 Answers

How to rotate an object according to the camera's view? 1 Answer

Rotating on other axis 1 Answer

Get angle of two Vector3s 1 Answer

how to rotate a mesh with GetMouseButton with specific rotation value 2 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