Currently I am working on my first game in unity, (I have experience in other languages mind you
) and I have stumbled upon a problem.
I am trying to move my game object (a simple cube) vertically in the y axis, and horizontally in the x axis, while moving forward in the z axis.
The movement works fine, however I need a better way to handle diagonal movement in the x y axes simultaneously.
Additionally, is there a way to make the movement less choppy?
I am using the “speed * Time.deltaTime”, however at higher speeds the movement is not fluid on a key down.
How should this be optimized for holding a key?
var xspeed : float = 1.0;
var yspeed : float = 1.0;
var zspeed : float = 1.0;
function Update () {
//Forward
transform.Translate(0, 0, zspeed * Time.deltaTime);
//Right
if (Input.GetAxis("Horizontal") > 0) {
transform.Translate( xspeed * Time.deltaTime, 0,0);
}
//Left
if (Input.GetAxis("Horizontal") < 0) {
transform.Translate( -xspeed * Time.deltaTime, 0,0);
}
//Up
if (Input.GetAxis("Vertical") > 0) {
transform.Translate(0, yspeed * Time.deltaTime, 0 );
}
//Down
if (Input.GetAxis("Vertical") < 0) {
transform.Translate(0, -yspeed * Time.deltaTime, 0 );
}
}
Also, how should I handle collisions between walls and my cube.
Both have a box collider, yet the cube passes right through the plane.
Thanks!
I don’t quite understand with this “choppy” term. You mean, stuttering? As far I could tell. When I move my character diagonally, the movement speed is higher than horizontal and vertical. (I move my character under Z and X axis because Y is up and down in my game)
For collision detection, add Rigidbody to your object.
EDIT:
The collisions don’t seem to work to well. With rigid bodies on the cube and wall it glitches out, and with one on the cube it still passes through…
For the other part, wheat I mean is that basically if you press the W A S or D keys one time, it move it the total distance of speed * time.
How would I make it move just a small amount if you tap the key once, and keep moving at the set speed if you hold the key down?
EDIT:
Can you post the screenshoot of the glitch? Describe the glitch, and what material do you use for your wall? Terrain? or other game object?
Normally, you will have the desired result. Tap the key once it will move a little and holding the key will keep moving the object.
How about this:
if (Input.GetAxis("Horizontal") || (Input.GetAxis("Vertical")){
transform.translate(xspeed * Time.deltaTime * Input.GetAxis("Horizontal"), yspeed * Time.deltaTime * (Input.GetAxis("Vertical"), zspeed * Time.deltaTime);
} else {
transform.translate(0, 0, zspeed * Time.deltaTime);
}
Haven’t tried this code above since I’m not using my own computer (at cyber cafe right now), may be it will help you.
You will need to use forces for movement to avoid passing through colliders
With some minor tweaking, this code works great! Thank you!
For the collisions, I attached an image of what happens below. The game object I am using for a wall is a cube (Scale: 10,10,100), and so is my player object.
When they collide, the player will spin out of control and usually fly off of the map.