- Ball.cs
- Scripts /
- RollerBall /
- Characters /
- Standard Assets /
- Assets /
- project /
1 using System;
2 using UnityEngine;
3
4 namespace UnityStandardAssets.Vehicles.Ball
5 {
6 public class Ball : MonoBehaviour
7 {
8 [SerializeField] private float m_MovePower = 5; // The force added to the ball to move it.
9 [SerializeField] private bool m_UseTorque = true; // Whether or not to use torque to move the ball.
10 [SerializeField] private float m_MaxAngularVelocity = 25; // The maximum velocity the ball can rotate at.
11 [SerializeField] private float m_JumpPower = 2; // The force added to the ball when it jumps.
12
13 private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
14 private Rigidbody m_Rigidbody;
15
16
17 private void Start()
18 {
19 m_Rigidbody = GetComponent<Rigidbody>();
20 // Set the maximum angular velocity.
21 GetComponent<Rigidbody>().maxAngularVelocity = m_MaxAngularVelocity;
22 }
23
24
25 public void Move(Vector3 moveDirection, bool jump)
26 {
27 // If using torque to rotate the ball...
28 if (m_UseTorque)
29 {
30 // ... add torque around the axis defined by the move direction.
31 m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x)*m_MovePower);
32 }
33 else
34 {
35 // Otherwise add force in the move direction.
36 m_Rigidbody.AddForce(moveDirection*m_MovePower);
37 }
38
39 // If on the ground and jump is pressed...
40 if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
41 {
42 // ... add force in upwards.
43 m_Rigidbody.AddForce(Vector3.up*m_JumpPower, ForceMode.Impulse);
44 }
45 }
46 }
47 }
2 using UnityEngine;
3
4 namespace UnityStandardAssets.Vehicles.Ball
5 {
6 public class Ball : MonoBehaviour
7 {
8 [SerializeField] private float m_MovePower = 5; // The force added to the ball to move it.
9 [SerializeField] private bool m_UseTorque = true; // Whether or not to use torque to move the ball.
10 [SerializeField] private float m_MaxAngularVelocity = 25; // The maximum velocity the ball can rotate at.
11 [SerializeField] private float m_JumpPower = 2; // The force added to the ball when it jumps.
12
13 private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
14 private Rigidbody m_Rigidbody;
15
16
17 private void Start()
18 {
19 m_Rigidbody = GetComponent<Rigidbody>();
20 // Set the maximum angular velocity.
21 GetComponent<Rigidbody>().maxAngularVelocity = m_MaxAngularVelocity;
22 }
23
24
25 public void Move(Vector3 moveDirection, bool jump)
26 {
27 // If using torque to rotate the ball...
28 if (m_UseTorque)
29 {
30 // ... add torque around the axis defined by the move direction.
31 m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x)*m_MovePower);
32 }
33 else
34 {
35 // Otherwise add force in the move direction.
36 m_Rigidbody.AddForce(moveDirection*m_MovePower);
37 }
38
39 // If on the ground and jump is pressed...
40 if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
41 {
42 // ... add force in upwards.
43 m_Rigidbody.AddForce(Vector3.up*m_JumpPower, ForceMode.Impulse);
44 }
45 }
46 }
47 }