Is there a way to get the end position to raycast to from an angle?

0 favourites
  • 4 posts
  • Im trying to cast a ray from the direction the player is looking but like slightly to the left or right. But I cant raycast from the player at an angle. So I have to tell it the position to raycast to. Is there a way to get that position based on an angle?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ok, so I ended up doing it a lazy way. I pinned a long sprite to the end of the player and did a raycast to the end of that sprite. While this does work, I dont want to have to do this for multiple angles. There is probably a better way to do this using fancy math though.

  • For a given angle θ and a circle of radius r and center (h,k), recall that we can determine the Cartesian coordinates (x,y) of the point on the circle determined by θ and r, where

    x=h+rcosθ , y=k+rsinθ

    Since you don't care about radius in this particular application, it would be

    x=self.x+cos(a)

    y=self.y+sin(a)

  • To expand on what oosyrag said and because it sounds like you want to ray cast to a range of angles around the player angle, you can use a loop. Something like this:

    On click

    - Repeat 10 times:

    Cast Ray from self.X,self.Y to self.x+cos(self.angle-5+loopindex), self.y+sin(self.angle-5+loopindex)

    This will cast 10 rays at 1° intervals from -5° of the player angle through to +5°

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)