Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Make a rolling ball in 2D

Discussion in 'Scripting' started by hamsteri, Aug 17, 2018.

  1. hamsteri

    hamsteri

    Joined:
    Jan 3, 2018
    Posts:
    4
    I need to make a ball rotate in Z-axis
    This is the code i am trying to use,
    but it wants me to use Vector2 instad of Vector3 And Vector2 cannot rotate in Z-axis.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 5;
    9.  
    10.     void Update()
    11.     {
    12.  
    13.         if (Input.GetKey(KeyCode.A))
    14.         {
    15.             Rigidbody2D.AddForce(Vector3.back * speed);
    16.         }
    17.         if (Input.GetKey(KeyCode.D))
    18.         {
    19.             Rigidbody2D.AddForce(Vector3.forward * speed);
    20.         }
    21.  
    22.     }
    23.  
    24. }
    25.  
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    I think you want to roll ball in its local x axis, as x axis is Right axis, while z axis is Forward axis.
    Assuming you want to roll toward moving direction?
     
  3. hamsteri

    hamsteri

    Joined:
    Jan 3, 2018
    Posts:
    4
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    you got on your rigid body myRigidBody2D.AddForce
    Why not use myRigidBody2D.AddTorqe ?
    Alternatively, you can apply myRigidBody2D.angularVelocity

    There is nothing wrong with having Vector3.
    Why your concerns?
     
    hamsteri likes this.
  5. hamsteri

    hamsteri

    Joined:
    Jan 3, 2018
    Posts:
    4
    Ok, Thanks for helping
    I use Microsoft Visual studio what suggested Vector2.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    I f you stay with Vector3, you will have always more flexibility, which doesn't hurt at all. But you can potentially reuse bits in future, if you want to go for full 3D, rather 2D. Also good learning case.

    I wonder, where MS VS suggested that. Have you link by any chance.
    Either way, I suggest stick with Unity guide lines. Unless they suggested Vector2 ;)
     
  7. hamsteri

    hamsteri

    Joined:
    Jan 3, 2018
    Posts:
    4
    I dont have links...
    But i seem to get " cannot convert from 'unityengine.vector3' to 'float' "
    from (Vector3.back * speed) and (Vector3.forward * speed)
    Btw i changed Addforce to Addtorque.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    Thats fine.

    multiplying vector by value gives you vector.
    If you want axis, then use vector.x for example.