0

i have simple unity project where i created simple cube. When i press spacebar this cube jumps.

My Problem is - i want to implement also 360 Rotation around Y-axis (only once and then stops) when i press spacebar. How can i do this?

Heres my current code:

void Start () {

        mSpeed = 3f;
        RB = GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    void Update () {
        transform.Translate(mSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f,mSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
        //transform.Rotate(Vector3.up, 100 * Time.deltaTime);

        if (Input.GetButtonDown ("Jump")) 
        {
            RB.velocity = new Vector3 (0,5,0);
        }

    }
  • you can rotate using Slerp and Lerp functions, if you want it to be controlled by inputs you should use a co-routine if you just want consistent motion put it in the update method. it might be better if you just researched how to make a simple game it will get all these little questions out of the way – johnny 5 Mar 20 '17 at 19:30
  • Do you want to rotate instantly or over time? – I.B Mar 20 '17 at 19:31
  • With the `RotateObject` function from the duplicated question, do this: `StartCoroutine(RotateObject(gameObject, new Vector3(0f,360f,0f), 1f));` The `1f` will make it rotate over one second. You can set it to `0` if you want it to rotate immediately. – Programmer Mar 20 '17 at 19:36
  • @cNuts when i press the spacebar i want to rotate this cube once 360d. around y-axis and then stop the rotation. Can u help me with this ? – mrneedyourhelp Mar 20 '17 at 19:41
  • Since the question was mark duplicate i can't do much but have you tried what @Programmer said i haven't tested the function but i'm pretty sure it works as intended and he even told you what values you need to give it. – I.B Mar 20 '17 at 19:44
  • nope, i didnt get any error but it doesnt work – mrneedyourhelp Mar 20 '17 at 19:56
  • Well try putting this `transform.RotateAround (transform.position, transform.up, 360f);` in your `if (Input.GetButtonDown ("Jump")) ` It should make it rotate instantly 360 degrees. – I.B Mar 20 '17 at 20:03
  • i copy-pasted the code but when i press the spacebar the cube only jumps but doesnt rotate itself – mrneedyourhelp Mar 20 '17 at 20:09
  • Well you do understand that if you make an object rotate 360 degrees instantly you won't really see anything. – I.B Mar 20 '17 at 20:11
  • @CNuts how can i add time for this 360 Rotation ? for example - i want to rotate this cube around y axis in 2 seconds (360degrees) – mrneedyourhelp Mar 20 '17 at 20:41
  • The `RotateObject` function [here](http://stackoverflow.com/questions/41651423/rotating-a-gameobject-to-a-specific-point) should work. You could edit your question with how you tried it maybe you didn't implement it right. – I.B Mar 20 '17 at 20:48
  • 1
    @CNuts I apologize to both of you. There seems to be a problem when rotation supplied is above 170 degrees. This has been fixed by removing `Quaternion`. The function now uses `Vector3` for every calculation. Please copy the new `RotateObject ` function and it should work like magic. Let me know if there is still a problem. – Programmer Mar 20 '17 at 21:13

0 Answers0