Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Der_Kevin · Jul 09, 2013 at 05:43 AM · raycastrotateplacement

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!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jul 09, 2013 at 05:46 AM 0
Share

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

avatar image Der_Kevin · Jul 09, 2013 at 05:55 AM 0
Share

right, i forgot to put var speed = 30; in it. thats so dumb -_- but thank you!

avatar image Benproductions1 · Jul 09, 2013 at 05:57 AM 0
Share

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

avatar image Der_Kevin · Jul 09, 2013 at 06:08 AM 0
Share

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 :)

avatar image Benproductions1 · Jul 09, 2013 at 08:09 AM 0
Share

Only one of them should work. One is C# and one is Javascript

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Benproductions1 · Jul 09, 2013 at 05:57 AM

Hello there,

You forgot to define speed.

Hope this helps,
Benproductions1

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Third Person Cross Hair Question 1 Answer

Activating a single Prefab with the same tag using Raycast. 0 Answers

Raycast not rotating with object / always have raycast ray a certain way no matter what? 2 Answers

Raycast rotate with player 2 Answers

RayCasting shows wrong point 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges