how do you constrain rotation of actor to moving actor???

nickbuakaewnickbuakaew Member Posts: 77
edited April 2012 in Working with GS (Mac)
i have an actor (gun) that need to rotate between 0-180 degree. The problem is this actor have to rotate when another object (target) moving all the time.

i need the gun to rotate, the rotation to follow target that are moving automatically

at this point i got the gun to follow target but only one time but not all the time.

thanks for help.

Best Answers

  • JohnPapiomitisJohnPapiomitis Posts: 6,256
    Accepted Answer
    Make 2 real attributes called targetX and targetY

    In the target actor constrain targetX to self.position.x and constrain targetY to self.position.y

    In your gun Constrain rotation to vectorToAngle(self.position.x-targetX,self.position.y-targetY)

    That will have it always rotate to face the target.

    If you want it to always rotate to face the target, but limit the rotation between 0-180 degrees then constrain the guns self rotation to

    max(0,min(180,vectorToAngle(self.position.x - targetX , self.position.y - targetY )))

    Also make sure that the gun image is facing right.

    Hope that helps
  • tatiangtatiang Posts: 11,949
    Accepted Answer
    vectorToAngle determines an angle based on two points' x and y positions. From the GameSalad Cookbook:

    vectorToAngle (Function)

    Finds the angle relative to the origin 0,0, given an X and Y coordinate. For instance, vectorToAngle(100,200)=63.435. You can also find the angle relative to an offset - for instance, vectorToAngle(x-x',y-y'). VectorToAngle(100-200,150-250) will find the angle between the points 100,150 and 200,250, or -135 degrees.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Posts: 11,949
    edited April 2012 Accepted Answer
    I made a video that might be helpful:

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Answers

  • nickbuakaewnickbuakaew Member Posts: 77
    work really well man. thanks a lot.
    i got simple question.....what is vectorToAngle do exactly?
Sign In or Register to comment.