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 saeedproxima · Dec 06, 2014 at 10:36 PM · 3rd person camera

How to move the ball according to the camera direction?

Hi. I'm trying to make 3rd person ball that the user moves with arrows and with the mouse the user controls the camera(which orbits around the ball). This is my script for the ball:

 public class BallMovement : MonoBehaviour {
 
     // Use this for initialization
     public Camera MyCamera;
     public GUIText Ind;
     bool OnFloor = true;
     private float x = 0.0f;
     void Update()
     {
         RaycastHit RHit;
         Ray MyRay = new Ray(transform.position, Vector3.down);
         Debug.DrawRay(transform.position, Vector3.down*3);
         if (Physics.Raycast(MyRay, out RHit, 4))
         {
             if (RHit.collider.gameObject.tag == "Floor")
             {
                 OnFloor = true;
                 Ind.text = "On the Floor";
             }
         }
     }
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxisRaw("Horizontal");
         float moveVerticle = Input.GetAxisRaw("Vertical");
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVerticle);
         rigidbody.AddForce(movement * 30.0f);
         //MyCamera.transform.position = new Vector3(transform.position.x - 2, transform.position.y+27, transform.position.z - 75);
         if (Input.GetButtonUp("Jump") && OnFloor==true)
         {
             rigidbody.AddForce(new Vector3(0,50,0)*5.0f);
             OnFloor=false;
             Ind.text = "Jumping";
         }
         
     }
 }

and for my camera I'm using the existing script in the standard assets folder/mouse orbit script.but my problem is that when i turn the camera with mouse and press the up arrow i want the ball to move towards the direction of the camera is looking.but it doesn't do that now with my script. can anyone help?

===================================

The Answer : this is my code for moving the ball according to answers:

         float moveHorizontal = Input.GetAxisRaw("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         Vector3 movement = MyCamera.transform.forward * moveVertical*speed*Time.deltaTime ;
         rigidbody.AddForce(movement);
         movement = MyCamera.transform.right * moveHorizontal*speed*Time.deltaTime;
         rigidbody.AddForce(movement);
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
0
Best Answer

Answer by EvilTak · Dec 07, 2014 at 02:49 AM

You can do this:

 float moveVertical = Input.GetAxisRaw("Vertical");
 Vector3 movement = mainCamera.transform.forward * movementVertical * 30;
 rigidbody.AddForce(movement);
 

Before this, you will have to create public Camera mainCamera, and in the inspector assign it to your main camera which has the mouse orbit script. If you want to do the same for horizontal axes use mainCamera.transform.right if right arrow has the positive value in your axis else use negative of the horizontal axis value.

Comment
Add comment · Show 7 · 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 saeedproxima · Dec 07, 2014 at 09:25 AM 0
Share

Thanks.the Vertical movement now is ok(except that the ball doesn't move softly on the terrain.the ball mass is 0.7).about the X axis you mean like this? : float moveHorizontal = Input.GetAxisRaw("Horizontal"); if(moveHorizontal >0) { i don't know the rest} could you explain? thank you

avatar image EvilTak · Dec 07, 2014 at 02:57 PM 0
Share

In your horizontal input axis, mostly the right direction will be positive, so Vector3 movement = mainCamera.transform.right * movementHorizontal * 30. If on playing the ball moves in the opposite direction that is left when you press right, just use mainCamera.transform.left ins$$anonymous$$d of mainCamera.transform.right. If that helped, please accept this answer.

avatar image saeedproxima · Dec 07, 2014 at 03:00 PM 0
Share

thanks.but i don't still get it how can i have two vector3 movement?

avatar image EvilTak · Dec 07, 2014 at 03:02 PM 0
Share

Just reuse the vector movement. No need to define it again.

avatar image Shanty22 · Dec 08, 2014 at 10:19 PM 0
Share

i know this is a beginner question but how can i combine vertical and horizontal(right) without using another vector3 (which doesn't work). You wrote that i am supposed to reuse the vector movement but i cant figure it out. An example would be highly appreciated.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I extend my linecast? 1 Answer

How does TransformDirection(Vector) work? 1 Answer

How to make the character move and rotate where the camera is facing (3rd person) 0 Answers

Clamping Camera doesn't work 1 Answer

Smooth Camera follow script is causing framerate drops when moving 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