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 /
  • Help Room /
avatar image
0
Question by RudyPyro · May 25, 2016 at 01:43 AM · ballforwardautomaticrollx-axis

How can i move my player automatically?

I am trying to make a rolling ball game, The game is almost done but i do have 1 problem. The problem is that i'd like to make my ball roll automatically forward on the X axis without pressing any buttons. I tried a few things but nothing worked so far... Can someone maybe tell me how i can make this work? There are 2 codes attached to the rolling ball, 1 of them is called "Ball" and the other is called "Ball User Control" I think that the second script is the one where i need to change someting. This is the script "Ball User Control":

using System; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Vehicles.Ball { public class BallUserControl : MonoBehaviour { private Ball ball; // Reference to the ball controller.

     private Vector3 move;
     // the world-relative desired move direction, calculated from the camForward and user input.

     private Transform cam; // A reference to the main camera in the scenes transform
     private Vector3 camForward; // The current forward direction of the camera
     private bool jump; // whether the jump button is currently pressed


     private void Awake()
     {
         // Set up the reference.
         ball = GetComponent<Ball>();


         // get the transform of the main camera
         if (Camera.main != null)
         {
             cam = Camera.main.transform;
         }
         else
         {
             Debug.LogWarning(
                 "Warning: no main camera found. Ball needs a Camera tagged \"MainCamera\", for camera-relative controls.");
             // we use world-relative controls in this case, which may not be what the user wants, but hey, we warned them!
         }
     }


     private void Update()
     {
         // Get the axis and jump input.

         float h = CrossPlatformInputManager.GetAxis("Horizontal");
         float v = CrossPlatformInputManager.GetAxis("Vertical");
         jump = CrossPlatformInputManager.GetButton("Jump");

         // calculate move direction
         if (cam != null)
         {
             // calculate camera relative direction to move:
             camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
             move = (v*camForward + h*cam.right).normalized;
         }
         else
         {
             // we use world-relative directions in the case of no main camera
             move = (v*Vector3.forward + h*Vector3.right).normalized;
         }
     }


     private void FixedUpdate()
     {
         // Call the Move function of the ball controller
         ball.Move(move, jump);
         jump = false;
     }
 }

} AND the Ball Script :

using System; using UnityEngine;

namespace UnityStandardAssets.Vehicles.Ball { public class Ball : MonoBehaviour { [SerializeField] private float m_MovePower = 5; // The force added to the ball to move it. [SerializeField] private bool m_UseTorque = true; // Whether or not to use torque to move the ball. [SerializeField] private float m_MaxAngularVelocity = 25; // The maximum velocity the ball can rotate at. [SerializeField] private float m_JumpPower = 2; // The force added to the ball when it jumps.

     private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
     private Rigidbody m_Rigidbody;


     private void Start()
     {
         m_Rigidbody = GetComponent<Rigidbody>();
         // Set the maximum angular velocity.
         GetComponent<Rigidbody>().maxAngularVelocity = m_MaxAngularVelocity;
     }


     public void Move(Vector3 moveDirection, bool jump)
     {
         // If using torque to rotate the ball...
         if (m_UseTorque)
         {
             // ... add torque around the axis defined by the move direction.
             m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x)*m_MovePower);
         }
         else
         {
             // Otherwise add force in the move direction.
             m_Rigidbody.AddForce(moveDirection*m_MovePower);
         }

         // If on the ground and jump is pressed...
         if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
         {
             // ... add force in upwards.
             m_Rigidbody.AddForce(Vector3.up*m_JumpPower, ForceMode.Impulse);
         }
     }
 }

}

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 courageousmonkey · May 25, 2016 at 05:25 AM

Could you share your ball.Move code?

If your ball GameObject doesn't have a rigidbody component already then you should add one.

You can then mess with the ball's velocity either by directly changing it or by adding forces. By changing the velocity or adding forces, you can have your ball automatically keep on moving even if no input is applied.

For example, my code example mimics being on a conveyor belt. The user wants to move at inputVelocity, but there's always a velocity of 5 units/second (conveyorBeltVelocity) being applied in the positive x-direction. If inputVelocity is a zero vector then the ball will automatically keep on moving at the conveyor belt's velocity.

     public void Move(Vector3 inputVelocity)
     {
 
         Vector3 conveyorBeltVelocity = new Vector3(5f, 0f, 0f);
         
 
         GetComponent<Rigidbody>().velocity = conveyorBeltVelocity + inputVelocity;
 
     }
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 RudyPyro · May 26, 2016 at 04:23 PM 0
Share

@courageousmonkey

It has allready an rigidbody on it, This is the Ball Script:

using System; using UnityEngine;

namespace UnityStandardAssets.Vehicles.Ball { public class Ball : $$anonymous$$onoBehaviour { [SerializeField] private float m_$$anonymous$$ovePower = 5; // The force added to the ball to move it. [SerializeField] private bool m_UseTorque = true; // Whether or not to use torque to move the ball. [SerializeField] private float m_$$anonymous$$axAngularVelocity = 25; // The maximum velocity the ball can rotate at. [SerializeField] private float m_JumpPower = 2; // The force added to the ball when it jumps.

     private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
     private Rigidbody m_Rigidbody;


     private void Start()
     {
         m_Rigidbody = GetComponent<Rigidbody>();
         // Set the maximum angular velocity.
         GetComponent<Rigidbody>().maxAngularVelocity = m_$$anonymous$$axAngularVelocity;
     }


     public void $$anonymous$$ove(Vector3 moveDirection, bool jump)
     {
         // If using torque to rotate the ball...
         if (m_UseTorque)
         {
             // ... add torque around the axis defined by the move direction.
             m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x)*m_$$anonymous$$ovePower);
         }
         else
         {
             // Otherwise add force in the move direction.
             m_Rigidbody.AddForce(moveDirection*m_$$anonymous$$ovePower);
         }

         // If on the ground and jump is pressed...
         if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
         {
             // ... add force in upwards.
             m_Rigidbody.AddForce(Vector3.up*m_JumpPower, Force$$anonymous$$ode.Impulse);
         }
     }
 }

}

avatar image courageousmonkey RudyPyro · May 26, 2016 at 05:46 PM 1
Share

Here's one of the easiest things you can do.

First define public Vector3 exteriorForce = new Vector3(1f,0f,0f); in your Ball script. Then add the following line at the end of your $$anonymous$$ove method, make sure it's outside of all of your if-else statements: m_Rigidbody.AddForce(exteriorForce).

This will always apply a force in the x-positive direction. Controlling the ball should feel like trying to drive on a hill with this code.

avatar image RudyPyro courageousmonkey · May 27, 2016 at 01:17 PM 0
Share

Thank you a lot man! It works great! :D

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

Please help with a ball rolling 1 Answer

How to make 2D ball roll 0 Answers

Cube a roll .what can I do for integer output? 0 Answers

I need help in my zombie script or animation 0 Answers

2D Rolling Ball 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