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
0
Question by unity_Z6HWt8F-B72D-Q · Sep 29, 2019 at 10:51 PM · transformaddforceaxisworldspacelocalspace

Add force forward to a ball using local coordinates doesn't work as I wanted

I'm trying to control a ball for a simple game. The goal is to rotate the ball with the horizontal input on the Y axis (in world space), and then, pressing vertical input, add force to the ball to move forward or backward with the forward vector aligned to the rotation. I've been searching here, and I've found the add relative force, and also the addforce(transform.forward) to use the local axis of the ball, but, when the z axis is pointing to the floor, the ball doesn't move anymore, since I'm applying the force against the floor. I understand that I need to use some king of combination of local and global axis, but, I have no idea on how to do it. Here's the code I'm using: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class playerMovement : MonoBehaviour
  {
      public float force = 10000f;
      float forwardInput;
      float sideInput;
      public float turnSpeed;
      Rigidbody rb;
  
      void Start()
      {
          rb = GetComponent<Rigidbody>();
         
      }
  
      private void FixedUpdate()
      {
          GetInputs();
          Vector3 movement = new Vector3(0.0f, 0.0f, forwardInput * force * Time.deltaTime);
          transform.Rotate(Vector3.up * sideInput * turnSpeed * Time.deltaTime);
          rb.AddForce(transform.forward * forwardInput * Time.deltaTime * force);
      }
  
      void GetInputs()
      {
          forwardInput = Input.GetAxis("Vertical");
          sideInput = Input.GetAxis("Horizontal");
      }
  
  }

Also. I'm beginner so I would, if possible, really appreciate a quick explanation, Thank you so much in advance for your time.

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 WarmedxMints · Sep 29, 2019 at 11:06 PM 1
Share

$$anonymous$$ight be worth running through the Unity Roll A Ball tutorial to get your started - https://learn.unity.com/project/roll-a-ball-tutorial

avatar image unity_Z6HWt8F-B72D-Q WarmedxMints · Sep 30, 2019 at 12:36 AM 0
Share

Please, read the question. I'm trying to rotate the ball on the Y axis so it can rotate, not the same as in the tutorial. Thank you so much for your comment.

avatar image WarmedxMints unity_Z6HWt8F-B72D-Q · Sep 30, 2019 at 01:01 AM 0
Share

Please follow the tutorial. The movement is the same. You just need to adapt it to where you are looking. As I said, the tutorial will get you started, it isn't the answer. What you want to do is easy enough, the tutorial will get your part way there.

Show more comments

1 Reply

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

Answer by thomh_unity · Sep 30, 2019 at 10:34 AM

Hello,

If you want to understand what's happening here it's a good idea to be able to see where you're applying forces to your rigid body. The following code will show you where you're about to apply the 'forward force'.

 private void FixedUpdate()
 {
     GetInputs();
     Vector3 movement = new Vector3(0.0f, 0.0f, forwardInput * force * Time.deltaTime);
     transform.Rotate(Vector3.up * sideInput * turnSpeed * Time.deltaTime);
     rb.AddForce(transform.forward * forwardInput * Time.deltaTime * force);

     //lets see where we're actually going to apply force by debugging transform.forward direction...
     Debug.DrawRay(transform.position, transform.forward);
 }



This will show you a debug ray (a white line) in the scene view. It will point in the direction the force is going to be applied in. It will look something like this:

alt text



Run your game and watch what happens to the debug ray when you try and move forwards. You'll see the sphere rotates as the sphere moves forwards. As it rolls forward eventually you will be applying force into the ground and your sphere will stick there.

To make this approach work as you intend you'll need to prevent the sphere from rotating. You can do this by using the rigid body constraints.

alt text

This will prevent the sphere from rolling around in a realistic way. If you want more realism you'll probably want to not constrain the rotation of the sphere and not use transform.forward as the direction in which you apply the force.

Perhaps try not rotating the ball and actually just updating an angle variable stored in your player. Use this to determine the direction you want to move the ball in. Something like:

 private void FixedUpdate()
 {
     GetInputs();

     //change our angle variable based on the sideInput
     angle += sideInput * sideInput * turnSpeed * Time.deltaTime;

     //calculate our direction based on the angle
     Vector3 direction = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle));
     direction.Normalize();

     //add the force based on the direction
     rb.AddForce(direction * forwardInput * Time.deltaTime * force);

     //debug direction
     Debug.DrawRay(transform.position, direction);
 }



This will prevent the rotation of the ball from influencing the direction the force is applied in.

I hope this helps!

Best regards,
Thom.


forward.png (165.0 kB)
rigid.png (18.8 kB)
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 unity_Z6HWt8F-B72D-Q · Sep 30, 2019 at 01:25 PM 0
Share

Hi $$anonymous$$, thank you so much for your answer, that was exactly what I was looking for. I guess I need to refresh a lot of trigonometry and vectors from school...

About the first part of the answer, thank you so much for taking the time to explain it to me, but I was already aware of what was happening by setting the viewer to local (the axis) and selecting the ball in runtime. Then I understood that I was applying the force against the floor. But again thank you so much.

And for the solution, that was what I was looking for, a way to rotate the vector that applies the force against the ball. Again, thank you so much for your answer, really appreciated. Best regards!! Francisco.

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

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

transform.position is giving me local space 1 Answer

What is the difference between TransformDirection, TransformVector and TransformPoint since they all accept and return a Vector3 value. 1 Answer

How do i move an object relative to another objects axis. 2 Answers

JS Move object over time 1 Answer

Instantiate prefab as child at a transform? (Javascript/Uniyscript) 2 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