- Home /
Cube doesn't follow camera
using UnityEngine;
using System.Collections;
public class Build : MonoBehaviour {
public GameObject cube1;
public GameObject cube2;
public GameObject cube3;
public GameObject cube4;
public bool Cube1;
public bool Cube2;
public bool Cube3;
public bool Cube4;
public bool block;
private GameObject newCube;
public bool Active;
public bool warning;
public bool clear;
public Inventory check;
public string warningText;
void Start ()
{
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.B) && !Active && !block)
{
if(check.woodCount > 0)
{
if(Cube1)
{
newCube = Instantiate(cube1, new Vector3(transform.position.x,10,transform.position.z) + transform.forward * 4.0f, transform.rotation) as GameObject;
Active = true;
newCube.transform.parent = Camera.main.transform;
//check.woodCount -= 10;
//cube.rigidbody.isKinematic = true;
block = true;
}
if(Cube2)
{
newCube = Instantiate(cube2, new Vector3(transform.position.x,10,transform.position.z) + transform.forward * 4.0f, transform.rotation) as GameObject;
Active = true;
newCube.transform.parent = Camera.main.transform;
//check.woodCount -= 10;
//cube.rigidbody.isKinematic = true;
block = true;
}
if(Cube3)
{
newCube = Instantiate(cube3, new Vector3(transform.position.x,10,transform.position.z) + transform.forward * 4.0f, transform.rotation) as GameObject;
Active = true;
newCube.transform.parent = Camera.main.transform;
//check.woodCount -= 10;
//cube.rigidbody.isKinematic = true;
block = true;
}
if(Cube4)
{
newCube = Instantiate(cube4, new Vector3(transform.position.x,10,transform.position.z) + transform.forward * 4.0f, transform.rotation) as GameObject;
Active = true;
newCube.transform.parent = Camera.main.transform;
//check.woodCount -= 10;
//cube.rigidbody.isKinematic = true;
block = true;
}
}
else
{
warningText = "You don't have wood !";
warning = true;
}
}
if(Input.GetKeyDown(KeyCode.V) && Active && block)
{
newCube.transform.parent = null;
//cube.rigidbody.isKinematic = false;
Active = false;
clear = true;
block = false;
}
if(clear)
{
Cube1 = false;
Cube2 = false;
Cube3 = false;
Cube4 = false;
clear = false;
}
}
}
My problem: I can spawn cube, it spawn in fron of me, 10 forward like in script, but after that when i move my character, cube doesn't follow ;/ it just rotate with me, nothnig else. I have First Person Character from Sample Asstes .
Answer by perchik · Apr 07, 2014 at 09:25 PM
Right, that's what the code you have does. It instantiates a cube at a position, but doesn't do anything to update it's position.
After checking all the inputs in Update, you need to add something like this:
if(Cube1)
{
cube1.transform.position = transform.position + transform.forward * 4.0f;
}
which will set the cube's position to the current position plus 4 units forward.
Thanks, that's strange, because when i had character from standard assets (basic import) everything worked fine :<
But thanks, it work :)
Your answer
Follow this Question
Related Questions
Character controls seen in Amnesia 4 Answers
Sprinting camera movement? (C#) 1 Answer
Click on collider to rotate camera 1 Answer
One wall cube in the Scene is not shown by Camera in Game view 0 Answers
RTS type of camera 1 Answer