Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 IceBlackSanctum · Feb 04, 2015 at 07:55 AM · camerarotationpositionforceview

How to apply a force forward depending of the camera

Hi,

Sorry I have not properly explained the problem.

I'm making a game with a ball. It is handled with the arrows of the keyboard and the mouse for the view around the ball. But there is a problem. I succeeded to do the movement of the ball and of the camera but no way to linked them.

I would like that when I turn the camera the "forward" of the ball turn itself. I so always go forward if I press the forward arrow (depending on the view of the camera), and if I push the back button I go back.

The mouvement is made thanks to forces (I have to keep the forces I need for the game).

Here the script for the mouvement of the ball :

 var vitesseDeplacement = 8.0;
 function Update () {
     if (Input) {
         rigidbody.AddForce(Input.GetAxis("Horizontal") * 25 * vitesseDeplacement * Time.deltaTime,0,Input.GetAxis("Vertical") * 25 * vitesseDeplacement * Time.deltaTime);
     }
 }

And here for the camera :

 var sensibilite : int = 3;
 
 function Update () {
     transform.Rotate(0, Input.GetAxis ("Mouse X") * sensibilite, 0);
 }


I surely checked the tutorial of the "Roll-a-Ball", look at the script of character in Unity but I lose the forces with it. And check at Unity Manual and I didn't found the correct command..

I hope you will help me,

Thanks for your answer,

IceBlackSanctum,

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
3
Best Answer

Answer by _MGB_ · Feb 04, 2015 at 11:29 AM

You can transform the force vector by the camera using something like:

 vec.x = Input.GetAxis("Horizontal");
 vec.z = Input.GetAxis("Vertical");
 vec = Camera.main.transform.TransformDirection(vec);  // Make relative to main camera
 vec.y = 0;  // optional for no y movement.
 Vector3 force = vec.normalized * Magnitude;

Comment
Add comment · Show 2 · 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 IceBlackSanctum · Feb 05, 2015 at 06:18 PM 0
Share

Hi,

Well, it's work, as long as we don't want to go back... If we press back button, we begin to fly... Very, very, very far away. It works when we go forward, but with a huge slowdown, it isn't the same made than my previous script. When I see at the vector of my ball (in the inspector), I see that there is forces at Y applied, approximatly 5 when we go back, and -5 we we go front.

It is the script, tell me if I did a mistake :)

 var speed = 8.0;
 var force : Vector3;
 var vec : Vector3;
 function Update () {
     if (Input) {
         vec.x = Input.GetAxis("Horizontal") * 25 * Time.deltaTime;
         vec.z = Input.GetAxis("Vertical") * 25 * Time.deltaTime;
         vec = Camera.main.transform.TransformDirection(vec.x, 0, vec.z);
         force = vec.normalized * speed;
         rigidbody.AddForce(force);
     }
 }

It is in JavaScript, I see that you gave me a C# script, is it better ?

Sorry for the time I take to answer I wasn't available ^^

Thanks for your answers you're a great help.

EDIT : I think I understand why there is a slowdown, it is because of the force on Y. And so the made will be the same as my previous.

EDIT 2 : A short screen : http://puu.sh/fvOlZ/4bb42782a9.jpg (Don't look at the map, it isn't ended)

avatar image IceBlackSanctum · Feb 10, 2015 at 07:32 PM 0
Share

Sorry I didn't copy down well.. It works, THAN$$anonymous$$S !

avatar image
1

Answer by AngryBurritoCoder · Feb 04, 2015 at 11:18 AM

I can't help by coding atm ( on a train on phone ) , but there is a few solutions

  1. You can apply the force to the ball based on camera position, so basically apply the force to the ball in the direction the camera faces, (ignoring the y axis so the ball doesn't start to fly xd ) , with this method the ball won't rotate itself to the direction of the camera though

  2. When input is being taken and camera is moved the ball rotated to always face forward in the direction of the camera. So basically you allign the ball with camera rotation, again ignoring all axis except y ( so your ball doesn't fly off again)

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 AngryBurritoCoder · Feb 04, 2015 at 12:20 PM 0
Share

$$anonymous$$GB wrote the code :) check his out

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RTS Camera movement wrong after rotation 1 Answer

Switch between Camera's 2 Answers

character view range 2 Answers

Camera Scripting Help (Sims Style) 0 Answers

Camera rotation and position problem,Camera rotation and camera position 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