Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 mmonis2014 · Oct 09, 2019 at 07:24 AM · rtsplacementcitybuilder

Bug with trying to place objects in a manner similar to Cities: Skylines in game - spawned object keeps moving towards camera before placing.

I am aware that version conflicting may have something to do with this, but I'm not entirely sure. I am using code from this website:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class GroundPlacementController : MonoBehaviour
 {
     [SerializeField]
     private GameObject placeableObjectPrefab;

     [SerializeField]
     private KeyCode newObjectHotkey = KeyCode.A;

     private GameObject currentPlaceableObject;

     private float mouseWheelRotation;

     private void Update()
     {
         HandleNewObjectHotkey();

         if (currentPlaceableObject != null)
         {
             MoveCurrentObjectToMouse();
             RotateFromMouseWheel();
             ReleaseIfClicked();
         }
     }

     private void HandleNewObjectHotkey()
     {
         if (Input.GetKeyDown(newObjectHotkey))
         {
             if (currentPlaceableObject != null)
             {
                 Destroy(currentPlaceableObject);
             }
             else
             {
                 currentPlaceableObject = Instantiate(placeableObjectPrefab);
             }
         }
     }

     private void MoveCurrentObjectToMouse()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition /* + (Vector3.up * 10)*/);

         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
             currentPlaceableObject.transform.position = hitInfo.point;
             currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal); // stop rotation
         }
     }

     private void RotateFromMouseWheel()
     {
         Debug.Log(Input.mouseScrollDelta);
         mouseWheelRotation += Input.mouseScrollDelta.y;
         currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 10f);
     }

     private void ReleaseIfClicked()
     {
         if (Input.GetMouseButtonDown(0))
         {
             currentPlaceableObject = null;
         }
     }
 }

I presume the website is using an older version of Unity3D than mine (I'm working with 2019.2.5f1) and that may factor into it, but I'm not sure.

When I use this script, the behaviour in the attached image occurs. The accompanying YouTube video on the website I got the code from does not have that issue, and that is something I wish to replicate.

So far, my workaround is to modify the MoveCurrentObjectToMouse() function, so that currentPlaceableObject.transform.position = hitInfo.point + (Vector3.forward * 1);. This helps prevent the sphere from moving closer to the camera, but this does mean that object placement is less intuitive. I wonder what solutions you are able to provide.

alt text

unity3d-bug.gif (458.1 kB)
Comment
Add comment · Show 1
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 tormentoarmagedoom · Oct 09, 2019 at 07:32 AM 0
Share

HEllo.

Please remake the title of your post. A title must give a direction of what is your problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FatalBobo · Oct 09, 2019 at 10:36 AM

Hi, From what I see on your GIF, each frames, it moves the center of your object to the contact point on the surface of your object (the mouse input position). This is why it moves.

Your raycast needs to ignore the object you are trying to place.

Comment
Add comment · Show 1 · 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
avatar image mmonis2014 · Oct 09, 2019 at 11:24 AM 0
Share

Thank you for pointing this out, but I'm not sure how to do this. I want to be able to place the object where my mouse cursor is, but without it moving closer and closer to the camera. As I've mentioned before, the workaround I've used is with messing around with the ray cast values so they position somewhere that's not so aligned with the cursor. However, I'm not sure if I want to do that, because it can be a little bit confusing for players. Edit: I've tried using a raycast offset but the issue can still happen depending on where my mouse cursor is.

avatar image
0

Answer by mmonis2014 · Oct 09, 2019 at 11:44 AM

Never mind, I was able to do it with this. Sorry to inconvenience everyone, I haven't been using Unity3D in a long time:

 currentPlaceableObject.transform.position = new Vector3(hitInfo.point.x, 10.0f, hitInfo.point.z);
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

114 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

How to make a grid and able to place buildings on? 0 Answers

Help with city building game? 1 Answer

How to stop placing a building on an obstacle 1 Answer

RTS building placement collision problem C# 1 Answer

RTS Building Structures - How to have building follow my cursor until I click and place it on the grounds? 2 Answers


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