Hi, I’m new to Unity and I have a 3D block moving maze, I have a game area with walls, a floor, several blocks positioned throughout, a skybox, and a camera with FPSWalker + Character Controller and Mouse Look scripts.
What I want to do is to move the blocks in only the X axis or Z axis depending on which of the 4 sides of the cube is touched.
So if the player hits the front X side I want it to move the cube backwards at a steady rate in only the -X direction and visa versa from the other side of the cube, and again for the Z sides.
I’ve tried to apply this to the blocks but they aren’t being affected.
Here is the code of the MoveBlock class done in CS.
using UnityEngine;
using System.Collections;
public class moveBlock : MonoBehaviour {
public int speed = 3;
public float x;
public float z;
void Update(){
x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
}
void OnCollisionEnter(Collision hit)
{
if (hit.gameObject.CompareTag("MainCamera") (Input.GetAxis("Vertical") == 1))
{
transform.Translate(0, 0, z, Space.World);
}
if (hit.gameObject.CompareTag("MainCamera") (Input.GetAxis("Vertical") == 1))
{
transform.Translate(x, 0, 0, Space.World);
}
}
}
I’ve tried to upload a .zip file containing my level which is only 10mb but i’m getting a “No Post Mode Specified” when I click the Add Attachment button after selecting the file. So until I get that resolved I can’t upload the entire project.
I’ve tried to modify the example you showed me to CS but I’m having a little trouble in the translation on the Vector3 declaration of pushDir on line 21
using UnityEngine;
using System.Collections;
public class moveBlock : MonoBehaviour {
float pushPower = 2.0f;
Vector3 pushDir;
void OnControllerColliderHit (ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
return;
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
return;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
body.velocity = pushDir * pushPower;
}
}
I’m getting this error:
Assets/moveBlock.cs(21,27): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected
Okay that’s working, I’m such a n00b I forgot about doing that from using XNA, duh!
Anyway the script was placed on the cube but its not doing anything still, is that the correct thing to do? Since the camera (character collider) doesn’t seem to be connected in any way, how do I go about doing that?
The cube has Rigidbody on with IsKinematic selected.