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 /
avatar image
1
Question by laurens10 · Jun 08, 2011 at 12:54 PM · forwardrelativethird-person-camera

how to move player forward relative to camera forward with "mouse-y" input

hi! First of all im new to scripting. I have done intense searching in the unity answers, the forum and the script reference but i can't get a solution.. I use mouse-x and mouse-y input to move a 3rdperson player. (the x & y movement of the mouse should be translated to moving the character forward, backwards, left and right) Mouse-x also give a slight rotation so you can steer the player. But rotating doesn't influence the direction of the x and z axis. So when i rotate 180 degrees and I Make a forward input, the player just moves backwards along the original and unchanged z-axis

heres my script:

function Update () {

transform.position.x -= (Input.GetAxis("Mouse X") * 0.04);

transform.position.z += (Input.GetAxis("Mouse Y") * 0.04);

transform.rotation.y -= (Input.GetAxis("Mouse X") * 0.01);

}

I know it has something to do with transform.direction but from all the examples I stumbled upon, they all work with key input * speed as input, while i have raw numbers from the x and y movement of the mouse. My scripting knowledge is to narrow to make these things compatible. Can anyone push me in the right direction? Thanks in advance, any advice will be greatly appreciated! Laurens.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by tnetennba · Jun 08, 2011 at 02:41 PM

You can get the Camera's forwrad direction by using:

Camera.mainCamera.transform.forward

Am I correct in assuming that you want to move the player along the camera's direction. So you would want to do something like this:

player.transform.position += Camera.mainCamera.transform.forward * mouseYDelta;

player.transform.position += Camera.mainCamera.transform.right * mouseXDelta;

This exact code will not work but it should give you an idea of what to do.

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 laurens10 · Jun 08, 2011 at 10:17 PM 0
Share

Yup, thank you for pointing it out! Script works like a charm now. I also fixed the rotation part. transform.rotation.y -= (Input.GetAxis("$$anonymous$$ouse X") * 0.01); wouldn't let me steer more then 180° So ins$$anonymous$$d I used eulerAngles which allowed me to go over 180° without running into issues.

Heres my working script:

function Update () {

transform.position -= Camera.mainCamera.transform.right *(Input.GetAxis("$$anonymous$$ouse X")*0.03);

transform.position += Camera.mainCamera.transform.forward *(Input.GetAxis("$$anonymous$$ouse Y")*0.03);

transform.eulerAngles.y -= (Input.GetAxis("$$anonymous$$ouse X"));

}

Thanks again!

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

2 People are following this question.

avatar image avatar image

Related Questions

Relative Rotation 1 Answer

Rigid Body relative speed 1 Answer

3rd person camera 1 Answer

Third Person Camera on a speherical world 2 Answers

Switching between cameras 4 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