- Home /
Rotate Object while placing it in Gameworld
Hi guys. I have a problem, i tried to create a simple "place object from inventory into gameworld" script. placing works fine but i have some problems with the rotation. the object should rotate slowly around itself while pressing A. here is the script:
using UnityEngine;
using System.Collections;
public class BuildingPlacement : MonoBehaviour {
public float scrollSensitivity;
private PlaceableBuilding placeableBuilding;
private Transform currentBuilding;
private bool hasPlaced;
public LayerMask buildingsMask;
private PlaceableBuilding placeableBuildingOld;
// Update is called once per frame
void Update () {
Vector3 m = Input.mousePosition;
m = new Vector3(m.x,m.y,transform.position.y);
Vector3 p = camera.ScreenToWorldPoint(m);
if (currentBuilding != null && !hasPlaced){
//rotate object
if (Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.down * speed * Time.deltaTime);
currentBuilding.position = new Vector3(p.x,0,p.z);
if (Input.GetMouseButtonDown(0)) {
if (IsLegalPosition()) {
hasPlaced = true;
}
}
}
else {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(new Vector3(p.x,8,p.z), Vector3.down);
if (Physics.Raycast(ray, out hit,Mathf.Infinity,buildingsMask)) {
if (placeableBuildingOld != null) {
placeableBuildingOld.SetSelected(false);
}
hit.collider.gameObject.GetComponent<PlaceableBuilding>().SetSelected(true);
placeableBuildingOld = hit.collider.gameObject.GetComponent<PlaceableBuilding>();
}
else {
if (placeableBuildingOld !=null) {
placeableBuildingOld.SetSelected(false);
}
}
}
}
}
bool IsLegalPosition() {
if (placeableBuilding.colliders.Count > 0) {
return false;
}
return true;
}
public void SetItem(GameObject b) {
hasPlaced = false;
currentBuilding = ((GameObject)Instantiate(b)).transform;
placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
}
}
The wrong line is on line 25 and marked with //rotate object. Unity says: - The name "speed" does not exist in the current contex - The best overloaded method match for UnityEngine.Transform.Rotate(UnityEngine.Vector3)' has some invalid arguments - Argument #1' cannot convert object' expression to type UnityEngine.Vector3'
yeah. and to be honest.. i have no idea whats wrong. could you help me please? thanks!
On line 25, you use the variable "speed". However, nowhere in your program do you define this variable. That's why it doesn't exist.
As for your second error. That is also due to "speed" not existing. As the type (of the non existent) speed is not defined, It defaults to Object. And you can't multiply a Vector3 by an Object. Nor can you pass an Object into a function as a Vector3
right, i forgot to put var speed = 30; in it. thats so dumb -_- but thank you!
No, in fact you didn't for get to put in var speed:float = 30!!
You forgot to put in public float speed = 30 :P
but both works for me. put var speed = 30; on line 24 and put public float speed = 30; into the top (not both at the same time of course) and it seams to me that i have with both solutions the same result. could you maybe explain me the difference? iam just trying to learn :)
Only one of them should work. One is C# and one is Javascript
Answer by Benproductions1 · Jul 09, 2013 at 05:57 AM
Hello there,
You forgot to define speed.
Hope this helps,
Benproductions1
Your answer