#vector stuff idk please help I wanna die

1 messages ยท Page 1 of 1 (latest)

cinder bolt
#

everythings broken

#

Anyone? Please?

#

I've literally spent a whole day working on this shitty thing that should've been done in an hour

ebon sphinx
#

float angle2 = Vector3.Angle(forward, -cam.transform.rotation.eulerAngles);
looks like a strange line to me

cinder bolt
#

It's the angle between the ohhh wait

ebon sphinx
#

might help posting some visual screenshots of what you're trying to do and what isn't working with it

cinder bolt
#

It's hard to screenshot

#

I want to record it though apparently my record thing is broken

cinder bolt
#

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 -

cinder bolt
#

I guess I'm just going to completely redo this

#

gtg now for ~30 min

ebon sphinx
#

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

cinder bolt
#

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.

ebon sphinx
#

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

cinder bolt
#

gtg now brb

wind vault
#
//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);
}
cinder bolt
#

Ok Im back

wind vault
#

(Mobile discord coding should work but possible typos)

cinder bolt
#

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

wind vault
#

Well, you've got two vectors. (0, 1, 0) and your forward.

cinder bolt
#

(0, 1, 0)?

next harness
#

if I understand this right you only want to know the light blue line? because that one is really, really simple

wind vault
cinder bolt
#

I know the blue line

#

I need the light blue line

next harness
#

thats what I asked, read again

cinder bolt
#

You said blue line

#

Not light blue line

wind vault
#

What're you attempting to make me solve...?

cinder bolt
#

The light blue line

#

you know what

#

let me change the colours

cinder bolt
#

Ok now it's green

cinder bolt
next harness
#

so the green line now, yeah really simple

cinder bolt
#

So how?

wind vault
#

You can solve any of those lines if you apply the plane algorithm above.. where the line is your transform.forward

cinder bolt
#

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

next harness
cinder bolt
next harness
#

thats what you want?

cinder bolt
# next harness

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

next harness
cinder bolt
#

Yes. exactly that

#

That's the thing I've wasted the past 10 hours on and still haven't fixed

next harness
#
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

cinder bolt
#

Let me try it

wind vault
#

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

cinder bolt
#

uhh when I try this the object gets closer towards me, and it doesn't move up and down

next harness
#

hold the mouse button

next harness
#

also the object I tested had no collider

#

yours have one and is raycasting against itself

cinder bolt
#

Oh man it almost works. How do I prevent it from going towards myself though?

next harness
#

remove the collider or modify the raycast in the script to ignore it

cinder bolt
#

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?

next harness
#

I don't think you need the raycast after all

#

I only used it for testing to put the object into position

cinder bolt
#

๐Ÿ‘ oh alright awesome

cinder bolt
#

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

next harness
#

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

cinder bolt
#

lol

#

Hmmm it's weird

#

I don't know why either, maybe it doesn't have to do with your script

next harness
#

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

cinder bolt
#

Thanks

#

I'll look up some tutorials to see if there's anything helpful but what I've seen so far didn't help

cinder bolt
#

The camera?

next harness
#

yes

cinder bolt
#

๐Ÿ‘ testing now

#

wtf it still limits the height

cinder bolt
#

I've checked the code but I couldn't find anything that does this

cinder bolt
#

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