- Home /
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.
HEllo.
Please remake the title of your post. A title must give a direction of what is your problem.
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.
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.
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);