Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Ray from camera that has an angle and calculating right position

Discussion in 'Scripting' started by cankirici34, Jul 24, 2022.

  1. cankirici34

    cankirici34

    Joined:
    Mar 27, 2020
    Posts:
    14
    I'm trying to do a character controller like shooty skies game and I'm sending a ray from a camera and camera has some angle and equating the hit point to the transform of an object, but if the Y of the object is not 0, of course, it looks like it's not folowingthe Mouse position (Ray hit point ), because of the angle of the camera. How can I calculate this and make it look right?
    my code:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetMouseButton(0))
    4.         {
    5.             ray = cam.ScreenPointToRay(Input.mousePosition);
    6.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
    7.             {
    8.                 Vector3 pos = new Vector3(hit.point.x, transform.position.y, hit.point.z - transform.position.y / 2);
    9.              
    10. transform.position = Vector3.Lerp(gameObject.transform.position, pos, Time.deltaTime * followSpeed);
    11.             }
    12.         }
    13.  
    14.     }
    and this is an example image


    https://imgur.com/a/fwidLBH
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    You can raycast to other things than a collider. If you make a mathematical Plane object (invisible) at the altitude you want your plane to fly at, then you just raycast to that.

    That's how I would do the "where is the player" problem in Shooty Skies. (EDIT: I did NOT write shooty skies; I'm simply observing the video)

    Raycasting, colliders, planes, Plane, etc:

    https://forum.unity.com/threads/hel...s-are-hitting-an-object.1058393/#post-6840296

    https://forum.unity.com/threads/kee...within-the-view-frustrum.936209/#post-6117173

    BE SURE to get the order of constructors correct for a
    new Plane()
    object!!
     
    Last edited: Jul 25, 2022
    cankirici34 likes this.
  3. cankirici34

    cankirici34

    Joined:
    Mar 27, 2020
    Posts:
    14
    I hadn't even heard of a mathematical plane until you said it and I created a plane on an object with an angle and used the plane.Ray function. Actually i think i might have even used it wrong logically. I don't know if it was necessary to create a cube with an angle. And i played shooty skies and that's what i wanted to do actually i haven't seen this game before. it doesn't have a distance effect exactly like in that game, maybe I need to decrease the scale of the object as the distance increases. Did you make the shooty skies game? I had the opportunity to play while studying the mechanics and it's a great game, congratulations. Thank you for reply back and keeping me informed i learned something new and important!

    EDIT: I guess I was in a bit of a hurry and forgot what I really wanted to do. Actually, it works very well, logically, but since the Y of the object I move has changed, they will not have the same y value as an object coming from the opposite direction, for example, as in the shooty skies game, I guess the enemies or the bullets that moving forward always move with the angle of the plane.
     
    Last edited: Jul 25, 2022