• 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 hamzachaudhry · Jul 24, 2016 at 01:19 PM · addforcedirectionrotate objectsphere

how can i rotate the ball in left direction deirection if it was in forward direction and vice versa , like a real ball?

     if (Input.GetMouseButtonDown(0) && !isDead)
     {
         isPlaying = true;

         score++;
         scoreText.text = score.ToString();
         if (dir==Vector3.forward)
         {
             dir = Vector3.left;
         }
         else
         {
             dir = Vector3.forward;
         }
     }
    
     float amountToMove = speed * Time.deltaTime;

     transform.Translate(dir * amountToMove);
 }
Comment
Add comment · Show 5
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 hamzachaudhry · Jul 24, 2016 at 01:04 PM 0
Share

please someone tell me i am new in unity

avatar image General-Troll · Jul 24, 2016 at 09:30 PM 0
Share

Question: Do you want your ball to essentially look like it is "rolling" ins$$anonymous$$d of just moving? I didn't quite understand form the title.

avatar image hamzachaudhry · Jul 26, 2016 at 09:35 AM 0
Share

@General-Troll yes i want it to look like rolling while moving

avatar image Charmind hamzachaudhry · Jul 26, 2016 at 10:16 AM 0
Share

transform.Rotate(new Vector3(dir[2], 0, dir[0]) Time.deltaTime speed);

avatar image hamzachaudhry · Jul 27, 2016 at 09:55 AM 0
Share

Thanks , I will try :-)

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Rajeesh_AR · Jul 26, 2016 at 11:18 AM

Hi @hamzachaudhry

You could use this.

 void Update ()
  {
         transform.Rotate(Vector3.forward * Time.deltaTime*50);
 }

You can change Vector3.forward to Vector3.left, Vector3.down etc. Also to avoid calling this code all the time (since it is in update), you could use a boolean

 bool canRevolve = false;
  void Update ()
      {
                if(canRevolve)
             transform.Rotate(Vector3.forward * Time.deltaTime*50);
     }

Activate and Deactivate boolean necessarily. Hope this will help. Happy Coding..

Comment
Add comment · Show 10 · 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 hamzachaudhry · Jul 27, 2016 at 09:55 AM 0
Share

Thankyou , hope it helps :-)

avatar image hamzachaudhry · Jul 28, 2016 at 09:18 AM 0
Share

$$anonymous$$y ball is my player , and it is only attach with ball

avatar image Rajeesh_AR hamzachaudhry · Jul 28, 2016 at 09:22 AM 1
Share

So... With the code which I provided, it will rotates only the player(ball).

And if whole platform is rotating, means the code is in some other script or in the camera also.

avatar image hamzachaudhry Rajeesh_AR · Jul 28, 2016 at 06:01 PM 0
Share

actually camera is child of my player

Show more comments
avatar image hamzachaudhry · Jul 28, 2016 at 06:34 PM 0
Share

+General-Troll sorry i didn't understand

avatar image General-Troll hamzachaudhry · Jul 28, 2016 at 06:49 PM 0
Share

What didn't you understand? Please be more specific.

avatar image hamzachaudhry General-Troll · Jul 28, 2016 at 06:55 PM 0
Share

I have to make player a child of camera or camera to be child of player??

Show more comments
Show more comments
avatar image
0

Answer by General-Troll · Jul 27, 2016 at 05:07 PM

Rajeesh is right, but that will constantly make it move in just one direction while the boolean is true. If moving to the right makes the boolean true, than it will still rotate forward instead of right. The solution to this is binding rotation AND movement to an axis. For example:

 var speed : float = 5;
 
 function Update  () {
  
 var forwardMovement = Input.GetAxis ("Vertical") * speed;
 var horizontalMovement = Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
  
 transform.Rotate(forwardMovement, 0, horizontalMovement);
 transform.Translate(horizontalMovement, 0, forwardMovement);
 }

I'm not positive, but due to the nature of rigidbodies and AddRelativeForce/AddForce, you might be able to invoke both movement and rotation using either of those, since force affects both rotation and position instead of just position. But I'm not 100% sure it will work.

Example code for this might be:

 var speed:  float = 5;
 
 function Update  () {
  
 var forwardMovement = Input.GetAxis ("Vertical") * speed;
 var horizontalMovement = Input.GetAxis ("Horizontal") * speed;
 
 rigidbody.AddRelativeForce (horizontalMovement, 0, forwardMovement);
 }

I can't test (and won't be able to for a while) any of these, so if someone can test out both ideas for me, that would be great.

Hope this helps!

Comment
Add comment · Show 3 · 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 hamzachaudhry · Jul 27, 2016 at 06:03 PM 0
Share

it doesn't do anything

avatar image General-Troll hamzachaudhry · Jul 27, 2016 at 09:05 PM 0
Share

Which doesn't do anything. If it's the second one, make sure you have a rigidbody attached.

avatar image General-Troll hamzachaudhry · Jul 28, 2016 at 01:47 PM 0
Share

Create a new Javascript script and copy-paste the first line group of code into it, and attach it to the ball and see if it works. If it does, than I can convert it to c# to work with your current script.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody.AddTorque/AddForce Question. 1 Answer

Slerp object in direction of traveling 1 Answer

For some akward reason bullet is moving in the wrong direction 1 Answer

Local Direction of Vector3 1 Answer

Using direction and Rigidbody2D.AddForce() to move towards object. 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