Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by skulldj1 · Apr 06, 2021 at 11:35 AM · quaternioncamera-movementcamera followfirst-person

How to control missile/knife (Quaternion.Euler)

Hi,

I am making a game where the player is able to throw a knife and then control the knife, from a first person view, by moving the mouse. When the player throws a knife, a new "knife-camera" is instatiated, and I can see that it is looking in the same direction as the player (which is what I want). But as soon as the first update (within the knife script) is called, the view shifts to some default position.

The update function can be seen here:

 void Update()
     {
         if (!hasHit )
         {
             if (isBeingControlled)
             {
 
                 //float mousePosX = Input.GetAxis("Mouse X") + screenCenter.x;
                 //float mousePosY = Input.GetAxis("Mouse Y") + screenCenter.y;
 
                 float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
                 float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
 
                 xRotation -= mouseY;
                 xRotation = Mathf.Clamp(xRotation, -90f, 90f);
                 yRotation += mouseX;
 
                 transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
 
                 transform.position += transform.forward * speed * Time.deltaTime;
 
                 if (Input.GetKeyDown(KeyCode.Q))
                 {
                     isBeingControlled = false;
                     speed = 20f;
                     ActivatePlayer();
                 }
             }
 
             else
             {
                 transform.position += transform.forward * speed * Time.deltaTime;
             }
 
         }
 
     }

If I remove, transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);, I can see that the knife is flying in the "correct" direction, but then I cannot control the knife.

Am I missing something, or can I do this in an easier way.

Thank you in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_ek98vnTRplGj8Q · Apr 06, 2021 at 04:34 PM

When you call transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f); you are overwriting whatever rotation it held before. Instead of setting the value absolutely, you want to make incremental, relative changes to the rotation.


Try instead something like transform.localRotation = transform.localRotation * Quaternion.Euler(xRotation, yRotation, 0);

Comment
Add comment · Show 1 · Share
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
avatar image skulldj1 · Apr 06, 2021 at 07:07 PM 0
Share

I agree that the transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f); is overwriting the initial rotation and that I have to find a way to make the rotation incremental, but unfortunately your suggestion did not work.

avatar image
0

Answer by skulldj1 · Apr 06, 2021 at 07:20 PM

I found the root of the problem. I initialize xRotation and yRotation as 0f but they should be set to the rotation of the player.

Comment
Add comment · Share
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

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

123 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 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 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 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

How to make a camera Follow an Object moving in zigzag path? 1 Answer

Camera Follow (Not Exact) 1 Answer

Camera move & rotate along vehicle 0 Answers

How can ı attach my camera to object for fps game ? 0 Answers

Why Child's Local z position is changing when I change z of my camera. 1 Answer


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