#vector stuff idk please help I wanna die
1 messages ยท Page 1 of 1 (latest)
everythings broken
Anyone? Please?
I've literally spent a whole day working on this shitty thing that should've been done in an hour
float angle2 = Vector3.Angle(forward, -cam.transform.rotation.eulerAngles);
looks like a strange line to me
It's the angle between the ohhh wait
might help posting some visual screenshots of what you're trying to do and what isn't working with it
It's hard to screenshot
I want to record it though apparently my record thing is broken
ok it fixed itself
When I look past a certain angle, the vectors just stop
they just give up
why?!
Secondly, my camera, when going up, is at a negative rotation. When looking down it's at a positive rotation. That's why I added -
Here I get the angle between the purple line (forward vector) and camera
I guess I'm just going to completely redo this
gtg now for ~30 min
I'm a bit busy to look through this in detail at the moment but I'd suggest splitting up the code for the rotation of the camera from the height of the object - they are separate axis
I want to use different maths for this to make it easier
Blue is the distance from the player to object. Red is the camera view. Light Blue is an upwards vector.
the x-z position is basically a freebie from the camera to begin with, just use the y rotation of the camera and forward direction of the camera
then, separately, the height can be calculated from the rotation about a vector pointing left/right
I can then use a bit of trigonometry to calculate every side, and then I have what I need
gtg now brb
//Assuming your reusing a plane and ray
var direction = transform.forward;
direction.y = 0f;
plane.SetNormalAndPosition(direction, item.position);
ray.origin = transform.position;
ray.direction = transform.forward;
if(plane.Raycast(ray, out float distance))
{
item.position = ray.GetPoint(distance);
}
Ok Im back
(Mobile discord coding should work but possible typos)
Yeah someone told me about plane raycasting before but it made me wanna jump of a building
How do I calculate angle alpha though?
I have the camera angle, though that is 0 degrees on the horizontal axis here
starting at the viewpoint
Well, you've got two vectors. (0, 1, 0) and your forward.
(0, 1, 0)?
if I understand this right you only want to know the light blue line? because that one is really, really simple
You'd be able to use this to get the commentary angle https://docs.unity3d.com/ScriptReference/Vector3.Angle.html
No
I know the blue line
I need the light blue line
thats what I asked, read again
distance is the length of the blue line..
What're you attempting to make me solve...?
Ok now it's green
ok yeah Im blind
so the green line now, yeah really simple
So how?
You can solve any of those lines if you apply the plane algorithm above.. where the line is your transform.forward
Yes but I don't understand how the plane algorithm works and how to implement it
Which is why Im using trigonometry instead, because at least I know how that works
I gave you the exact code...
Just because you give me a piece of code doesn't mean I know how it works
thats what you want?
Almost. It should only change on the y-axis. The player should be able to move around it
Basically, the object is locked on the y-axis
Yes. exactly that
That's the thing I've wasted the past 10 hours on and still haven't fixed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlacementTest : MonoBehaviour {
public GameObject testObject;
private void Update() {
if (Input.GetMouseButton(0)) {
float d = Vector3.Distance(testObject.transform.position, this.transform.position);
Vector3 t = this.transform.forward * d;
Vector3 c = testObject.transform.position;
c.y = (this.transform.position + t).y;
testObject.transform.position = c;
return;
}
if (Physics.Raycast(this.transform.position, this.transform.forward, out RaycastHit hit, 100f)) {
testObject.transform.position = hit.point;
}
}
}
the script is attached to the camera
Let me try it
Issue would be that's a spherical carrying motion rather than planar.
He didn't want it to come forward as the object rose to the ceiling.
ie distance will change
uhh when I try this the object gets closer towards me, and it doesn't move up and down
hold the mouse button
also the object I tested had no collider
yours have one and is raycasting against itself
Oh man it almost works. How do I prevent it from going towards myself though?
remove the collider or modify the raycast in the script to ignore it
I'm implementing this with a grid placement system, so it glitches out a bit. @next harness ```
public void ShowPreview(RaycastHit hit2)
{
if (!Input.GetKey(KeyCode.LeftShift))
{
pos = hit2.point;
pos -= Vector3.one * offset;
pos.x /= gridSize;
pos.z /= gridSize;
pos.y /= gridSize / 3;
pos = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
pos.x *= gridSize;
pos.z *= gridSize;
pos.y *= gridSize / 3;
pos += Vector3.one * offset;
currentpreview.position = pos;
}
else
{
float d = Vector3.Distance(currentpreview.transform.position, cam.transform.position);
Vector3 t = cam.transform.forward * d;
Vector3 c = currentpreview.transform.position;
c.y = (cam.transform.position + t).y;
currentpreview.transform.position = c;
return;
}
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit, 100f))
{
if(!hit.collider.CompareTag("building"))
currentpreview.transform.position = hit.point;
}
}```
Can I just put the raycast inside the else?
I don't think you need the raycast after all
I only used it for testing to put the object into position
๐ oh alright awesome
And why does it limit how far it can go?
uhhh the closer I am to the object, the higher I can move it. If I'm far away I can't move it up and down at all. @next harness
But questions aside, where were you all day? I really needed you 10 hours ago lol. Thanks so much for your help I seriously appreciate it a lot! If you hadn't given me this answer I would've been crying for the coming hours

It's not limited in height for me... I'm not a math expert but that was the fastest solution that came to my mind, so I wrote that dirty test script within 2 minutes and tested it lol
lol
Hmmm it's weird
I don't know why either, maybe it doesn't have to do with your script
quickly tested 5 more minutes... here is the plane cast version:
public class PlacementTest : MonoBehaviour {
public GameObject testObject;
private Plane plane;
private Ray ray;
private void Update() {
if (Input.GetMouseButton(0)) {
//Create a plane facing the player on Y axis (X/Z rotation stays zero)
Vector3 planeForward = (this.transform.position - testObject.transform.position);
planeForward.y = 0f;
plane.SetNormalAndPosition(planeForward.normalized, testObject.transform.position);
//Create a ray facing forward from camera
ray.origin = this.transform.position;
ray.direction = this.transform.forward;
//See if the ray hits the created plane
if (plane.Raycast(ray, out float enter)) {
//Keep X/Z position but modify Y (the height)
Vector3 curentPosition = testObject.transform.position;
curentPosition.y = ray.GetPoint(enter).y;
testObject.transform.position = curentPosition;
}
return;
}
if (Physics.Raycast(this.transform.position, this.transform.forward, out RaycastHit hit, 100f)) {
if (hit.transform.gameObject == testObject) {
return;
}
testObject.transform.position = hit.point;
}
}
}
that one should be more reliable, I've added some comments to it to make it easier to follow and implement
good luck
Thanks
I'll look up some tutorials to see if there's anything helpful but what I've seen so far didn't help
On which object did you put this script?
The camera?
yes
like this
I've checked the code but I couldn't find anything that does this
You know what, I don't think it's going to be a big problem anyways. It's weird that it happens, though I don't think it's too frustrating