RTS Building Rotation

hi everyone, this is my 1st post since im very new to unity and coding in general. im creating this simple rts game but i cant get the buildings to rotate before i place them…not sure what im doing wrong but im sure its something very simple. please help iv been searching endlessly…here is my building placement code…

using UnityEngine;
using System.Collections;

public class BuildingPlacement : MonoBehaviour {

private Transform currentBuilding;
private bool hasplaced;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (currentBuilding != null && !hasplaced) {
Vector3 n = Input.mousePosition;
n = new Vector3 (n.x, n.y, transform.position.y);
Vector3 p = GetComponent ().ScreenToWorldPoint (n);
currentBuilding.position = new Vector3 (p.x, 0, p.z);

if (Input.GetMouseButtonDown (0)) {
if (IsLegalPosition()){
hasplaced = true;
}

if (Input.GetMouseButtonUp(1)){
Destroy(this.gameObject);
}

}

}

}
bool IsLegalPosition(){
return true;
}
public void SetItem(GameObject b){
hasplaced = false;
currentBuilding = ((GameObject)Instantiate(b)).transform;
}

}

and this is the rotation code im trying to attach to the buildings…

using UnityEngine;
using System.Collections;

public class Rotate : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

ApplyRotation ();

}

void ApplyRotation()
{
float deadZone = 0.01f;
float ease = 3500f;

float scrollWheelValue = Input.GetAxis (“Mouse ScrollWheel”) * ease * Time.deltaTime;

if ((scrollWheelValue > -deadZone && scrollWheelValue < deadZone) || scrollWheelValue == 0f)
return;

this.transform.eulerAngles = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y + scrollWheelValue, transform.eulerAngles.z);
}

}

The Rotate class seems to not even try to apply any rotation before object is placed. Only thing you have set it do is rotate it afterwards with mousewheel.

please help …what do i do?? im loosing hair over this…

1 Like

Oops… Sorry. It’s my first post I didn’t know…

public class BuildingPlacement : MonoBehaviour {

private Transform currentBuilding;
private bool hasplaced;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if (currentBuilding != null && !hasplaced) {
Vector3 n = Input.mousePosition;
n = new Vector3 (n.x, n.y, transform.position.y);
Vector3 p = GetComponent<Camera> ().ScreenToWorldPoint (n);
currentBuilding.position = new Vector3 (p.x, 0, p.z);

if (Input.GetMouseButtonDown (0)) {
if (IsLegalPosition()){
hasplaced = true;
}

if (Input.GetMouseButtonUp(1)){
Destroy(this.gameObject);
}


}

}


}
bool IsLegalPosition(){
return true;
}
public void SetItem(GameObject b){
hasplaced = false;
currentBuilding = ((GameObject)Instantiate(b)).transform;
}

}

and this is the rotation code im trying to attach to the buildings..

using UnityEngine;
using System.Collections;

public class Rotate : MonoBehaviour {



// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

ApplyRotation ();

}

void ApplyRotation()
{
float deadZone = 0.01f;
float ease = 3500f;

float scrollWheelValue = Input.GetAxis ("Mouse ScrollWheel") * ease * Time.deltaTime;


if ((scrollWheelValue > -deadZone && scrollWheelValue < deadZone) || scrollWheelValue == 0f)
return;

this.transform.eulerAngles = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y + scrollWheelValue, transform.eulerAngles.z);
}

Please, press insert/code on your comment toolbar, and paste your code in that way instead of copying it from your original post
(We really nees an update to that post asap)
Btw I forgot to tell yiu, that you shouldnt put caps in your title, because it makes others psychologically not wanting to help

Have you attached the Rotate script to building, prefab maybe?

Try Debug.Log(scrollWheelValue); to see what numbers it’s getting, or if it’s entering the function at all. Debug.Log() more and more things to be sure everything works as you expect. Pause game when placing building, and check every object in scene that they are right. In top corner of Inspector window you can temporarily enable debug mode too, to see a little more information.

And as they say, code should have indents to be better readable http://www.python-course.eu/images/blocks.png You can still edit even your first post, it would be much preferred over posting the same thing for 3rd time.

here you go…

using UnityEngine;
using System.Collections;

public class BuildingPlacement : MonoBehaviour {

    private Transform currentBuilding;
    private bool hasplaced;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
        if (currentBuilding != null && !hasplaced) {
            Vector3 n = Input.mousePosition;
            n = new Vector3 (n.x, n.y, transform.position.y);
            Vector3 p = GetComponent<Camera> ().ScreenToWorldPoint (n);
            currentBuilding.position = new Vector3 (p.x, 0, p.z);
       
            if (Input.GetMouseButtonDown (0)) {
                if (IsLegalPosition()){
                hasplaced = true;
                }

            if (Input.GetMouseButtonUp(1)){
            Destroy(this.gameObject);
            }


        }

    }

     
}
   bool IsLegalPosition(){
    return true;
}
public void SetItem(GameObject b){
    hasplaced = false;
    currentBuilding = ((GameObject)Instantiate(b)).transform;
}

}
using UnityEngine;
using System.Collections;

public class Rotate : MonoBehaviour {



    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {

        ApplyRotation ();
   
    }

    void ApplyRotation()
    {
        float deadZone = 0.01f;
        float ease = 3500f;
       
        float scrollWheelValue = Input.GetAxis ("Mouse ScrollWheel") * ease * Time.deltaTime;
       
       
        if ((scrollWheelValue > -deadZone && scrollWheelValue < deadZone) || scrollWheelValue == 0f)
            return;
       
        this.transform.eulerAngles = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y + scrollWheelValue, transform.eulerAngles.z);
    }


   
    }

sorry about the copy pasting was using my phone earlier…

yes i attached the script to all the prefabs but once i place a building it wont stop rotating even when i select another one…

Using unrelated variable names is not a smart thing to do, because if someone looks into your code, they need to constantly look for n, and p, because even if youre from a different language, n says nothing at all about tge use of the variable

1 Like

Once you place the building destroy the Rotate script on that placed building.

Destroy(currentBuilding.GetComponent<Rotate>());
1 Like

Thank you guys let me try it out…

Thank you so much KelsoMRK it worked!! I can grow my hair back now… Much appreciated…