Trying to make controls to move a cube

Harder than it looks! I tried using rotation. (transform.rotate) but I want it to have a force when it moves. If I use transform.rotate, it looks unrealistic and instantly rotates to the designated vector. I want it to move to that rotation, not instantly. So I thought using angularVelocity of Rigidbody component would work for this. This code isn’t working too well. When I play the game and press the AD keys, I barely see it turn at all. And if it does turn, it sure isn’t smooth. :confused:

public class Player_Movement : MonoBehaviour {

int Rotation = 0;
int Velocity = 0;

void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Rotation = -150;
}
else if(Input.GetKeyDown(KeyCode.D))
{
Rotation = 150;
}
else
{
Rotation = 0;
}

if (Input.GetKeyDown(KeyCode.W))
{
Velocity = 2;
}
else if(Input.GetKeyDown(KeyCode.S))
{
Velocity = -2;
}
else
{
Velocity = 0;
}
if(Rotation != 0)
{

}
if(Velocity != 0)
{
gameObject.GetComponent().AddForce(new Vector3(Velocity,0,0));
}
if(Rotation != 0)
{
gameObject.GetComponent().angularVelocity = new Vector3(0, Rotation, 0);
}

}
}

Oh I have a useless if statement in there but nevermind that.

It is rotating when the camera gets closer. But it moves too quickly. I want it transition to the new rotation vector.

There’s an icon next to the disk while you’re writing forum posts, it’s labeled insert, put your code inside the insert code bit so it’s a bit more legible/familiar to read online.

Ok. But this code is so simple, it shouldn’t really matter. :frowning:

You’ve got your variable velocity in the x axis of add force and rotation in your Y, is that what you meant to do?

If you take off gravity on the rigidbody and set your velocity to 200 it definitely does something, I’m just not entirely sure what your intention is.

I chucked velocity into the x and y of the addforce vector. Are you making a 3D or 2D game?

I’m making a cube game in 3D space. What this code is supposed to do is if you hold down the A or D keys it will constantly rotate the cube in the according direction. To my surprise, the Y coordinate actually acts like the X, or maybe the Z. It’s hard to know in 3D space! Must have it positioned in a weird way. I’ll uncheck gravity, set it to 200, and try again.

Looks great! Thanks :smile:

It’s nicer on the eyes reading it, I had to copy and paste into mono, you can’t visually debug something that fast without proper formatting however simple it may be. It’s usually the simple stuff that gets you like only chucking one variable into control a three dimensional point in space.

gameObject.GetComponent<Rigidbody>().AddForce(new Vector3(Velocity,0,0));
}
if(Rotation != 0)
{
gameObject.GetComponent<Rigidbody>().angularVelocity = new Vector3(0, Rotation, 0);

Look into the Vector3’s of both angular velocity and rotation, at moment you’re rotation round the Y and adding force only in the X, experiment chucking Velocity into all of this bit (new Vector3(Velocity,Velocity,Velocity) for instance and same for rotation maybe.

That’s an insanely hard game to play with controls like that though, I reckon people will love it!! Good luck!

Ok, thanks. And sorry about the unformatted code. I didn’t know that was an option.

Haha thanks! :smile:

I’m back. So everything works, but why can I not change the speed of the velocity? … Also I don’t know how you format code. I’m looking for it, I clicked on use BB code editor, but nothing is happening.

float Rotation = 0;
int Velocity = 0;
public float speed = .0001f;
bool ADown = false;
bool DDown = false;

void Update()
{
if (Input.GetKey(KeyCode.A))
{
Rotation = -speed;
ADown = true;
DDown = false;
}
else if(Input.GetKey(KeyCode.D))
{
Rotation = speed;
ADown = false;
DDown = true;
}
else
{
ADown = false;
DDown = false;
Rotation = 0;
}

if (Rotation != 0)
{

gameObject.GetComponent().angularVelocity = new Vector3(0,Rotation,0);
}
else
{
gameObject.GetComponent().angularVelocity = Vector3.zero;
}
if (Velocity != 0)
{

}
}

To format the code while your writing the message click the button called insert, box with a little I in it, when you click it there’s an option for code, and just pop it in there.

I’m just trying to write a better version of your code, hang on two secs

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public float moveSpeed = 20f;

    private Vector3 moveDir;

    private Vector3 moveRot;

    void Update()
    {
        moveDir = new Vector3 (0, 0, Input.GetAxisRaw ("Vertical")).normalized;   

        moveRot = new Vector3 (0, 0, Input.GetAxisRaw ("Horizontal")).normalized;   

    }

    void FixedUpdate()
    {
        GetComponent<Rigidbody> ().MovePosition (GetComponent<Rigidbody>().position + transform.TransformDirection (moveDir) * moveSpeed * Time.deltaTime);
        GetComponent<Rigidbody> ().angularVelocity = moveRot * moveSpeed;
    }
}

I’ll leave it up to you to tweak for a sec now!

I’m not entirely sure but I think that Input.GetAxis is a bit more versatile in certain situations as it allows you to control with your arrow keys as well as ASDW

Thanks Richop. But could you tell me what the ‘Input.GetAxis’ lines do? I never understood them.

I guess one way to look at the Axis part is instead of specifying every direction individually ie up, down,left,right, front, back, you can simplify the process by giving each pair an axis, look at the gimbal the basic 3D pointer is three axis, simplifies into x,y,z and you can have negative and positive numbers to describe the difference between front and back on just a line.

Reach your hands out that’s your horizontal axis, -1 would be either left and +1 would be right, you can do the same thing with vertical, up and down your waste is 0, your toes are -1 and your head is +1 for example

Oh okay, cool!

Also EDIT > Project Settings > Input, that’s what you’re talking to the Inputs that are assigned tags “Horizontal”

I feel I might have gone overboard with the last explanation!

Inside “Horizontal” you can see there has a definition for left and right, so makes it easier for you.

Also these things map well to other devices like phones and crap

Oh thanks! :smile: