Home c# How to rotate an object around its axis? / Unity3d

How to rotate an object around its axis? / Unity3d




There is a ball that I want to rotate around its axis in advance.

The question itself would be very easy if it were not for one thing.

The movement of the object:

rb.MovePosition (transform.position + (transform.forward * Time.deltaTime * speed));

In order to change the direction of his character, I turn that I need to use forward so that the character could not just go forward along the axis X, but also change direction and move it vpered.No you understand if I just start to spin it like so transform.Rotate (Vector3.right * 3) , then forward will not work correctly.

To rotate:

private Touch touch1;
    private Vector2 TouchPosition;
    private Quaternion rotationY;
    private float rotateSpeedModifier = 0.7f;
    Void Update ()
      if (Input.touchCount & gt; 0)
            touch1 = Input.GetTouch (0);
            if (touch1.phase == TouchPhase.Moved)
              rotationY = Quaternion.Euler (
                touch1.deltaPosition.x * rotateSpeedModifier,
              transform.rotation = rotationY * transform.rotation;

The only solution I see is to change the mechanics of movement, but how?

// Rotate animation is probably a bad idea.

Answer 1, Authority 100%

The best way would be to use physical movement rather than rotating + to move forward.

using unityengine;
// this line ensures that our script is not filled up
// got some on the player will be absent Rigidbody component
[RequireComponent (typeof (Rigidbody))]
public class Movement: MonoBehaviour
  public float Speed ​​= 10f;
  public float JumpForce = 300f;
  // this variable that would add a tag worked "Ground" on the surface of your land
  private bool _isGrounded;
  private Rigidbody _rb;
  Void Start ()
    _rb = GetComponent & lt; Rigidbody & gt; ();
  // note that all actions with physics
  // must be processed in FixedUpdate, rather than Update
  void FixedUpdate ()
    MovementLogic ();
    JumpLogic ();
  private void MovementLogic ()
    float moveHorizontal = Input.GetAxis ( "Horizontal");
    float moveVertical = Input.GetAxis ( "Vertical");
    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    _rb.AddForce (movement * Speed);
  private void JumpLogic ()
    if (Input.GetAxis ( "Jump") & gt; 0)
      if (_isGrounded)
        _rb.AddForce (Vector3.up * JumpForce);
        // Note that I do on the basis of Vector3.up
        // rather than on the basis of transform.up. If a character is dropped or
        // if the character - a ball that his personal "top" can
        // any direction. Left, right, down ...
        // But we need only race in the absolute top,
        // therefore Vector3.up
  void OnCollisionEnter (Collision collision)
    IsGroundedUpate (collision, true);
  void OnCollisionExit (Collision collision)
    IsGroundedUpate (collision, false);
  private void IsGroundedUpate (Collision collision, bool value)
    if (collision.gameObject.tag == ( "Ground"))
      _isGrounded = value;

The code is taken from the theme:
Proper implementation of the character movement

And there they are taken from the documentation of the Unity and finalized.

In your case, you need to cut the logic of the jump, and also clicch the object in height (y) when you touch the “land”, as far as I understand.

But if you answer the question itself: how to rotate the ball around your axis …

For this you need to use ROTATE. But the most important thing is that you need to consider that Pivot Point was in the center of the object. Because Rotate the object will be around it.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions