playMaker

Author Topic: Roll-a-Ball Tilt based on dynamic camera forward?  (Read 4554 times)

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Roll-a-Ball Tilt based on dynamic camera forward?
« on: December 02, 2015, 01:42:22 PM »
Hello,

I am creating a roll a ball type of game and am stuck on a tilt concept. I have a camera that follows a ball and when my device is tilted the ball rolls as expected. Now the problem is I added a function to my camera that allows the player to press a GUI button and rotate the camera 90 degrees at a time left or right depending on the button pressed. That works but now I am left with the problem that the ball rolls based on the tilted direction of the device rather than the camera. I am not sure how to get around this.

TL;DR: I want my ball to roll in the camera's forward direction based on the device tilt and I can't figure it out.

Any suggestions?
Most Recent Published Games:
Juicy Theater - Android

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #1 on: December 03, 2015, 12:24:48 AM »
Hi,

 I think it's because your ball is acting based on the camera direction.

 what script or fsm are you using to control the ball?

 the trick is to decouple the camera and the ball control,

-- Create a GameObject call "Ball Controller", it will be what's used to apply the device accelerations to control the ball.

-- Attach the camera to the "Ball Controller". now if you rotate the camera around, you won't be affecting the Ball controller, yet you'll have another view of the world.

 Does that make sense?

 Bye,

 Jean

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #2 on: December 03, 2015, 10:19:15 AM »
Hi Jean,

I have the camera following a camera dummy(child of the ball) with the smooth follow action.

I have a Game Object that collects the device acceleration and stores them in global variables. Then I have the ball add torque in the specific axis to make it move.

What I think I need is when I turn the camera the add torque needs to apply in different axis base on where the camera is looking.

Im not sure what you mentioned will work in my situation, or am I not seeing it?
Most Recent Published Games:
Juicy Theater - Android

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #3 on: December 04, 2015, 12:42:17 AM »
Hi.

 What scripts are you using the control the ball? Is it a fsm you built or a script you took somewhere?

Bye,

 Jean

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #4 on: December 04, 2015, 12:04:26 PM »
Hey Jean,

I built two small FSM's for this.

The first one is on my "GameManager" Gameobject. This one captures the device acceleration input and stores it in two global variables.

The second is on my "PlayerSphere" Gameobject. This one adds torque based on the devices tilt/accelerometer that is stored in the global variables.

Images of my two FSM's are attached.

The way I have it set up now, when the player tilts the device, it works. Though, if i move the camera the controls are only meant for the default camera view so the player continues to roll as before but it looks weird and is disorienting.

What I am trying to do is if the camera is moved I am hoping to control the ball as it would if the camera is in a default view(looking down the positive z) only it would now be looking down the positive or negative "x" based on if I turned the camera. I am trying to get the player tilt controls to be relative to the camera.

I think I need to get the camera's forward and then somehow apply it to the Add torque variables on the ball combined with the acceleration variables.

Most Recent Published Games:
Juicy Theater - Android

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #5 on: December 08, 2015, 12:04:27 AM »
@jeanfabre

Just wanted to bump this :D
Most Recent Published Games:
Juicy Theater - Android

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #6 on: December 08, 2015, 12:31:50 AM »
Hi,

 ok, for this you'll need to get the axis of the camera and then apply a torque to the ball using this axis as the torque axis.

 so let's say we take the x Axiy of the camera as a rotation axis for the torque.

 use the action "Transform Direction" targeting the camera and use the local direction (1,0,0), the x axis.

Multiply this axis by the strength, you end up with a torque.

use "Add torque" on your ball, using this vector you computed.

 Bye,

 Jean

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #7 on: December 08, 2015, 02:00:46 PM »
I think we are getting closer Jean, but still not there yet. I found a "Unity Answers" question on exactly what I am trying to do.

Here is what I am trying to get:
http://answers.unity3d.com/questions/53685/link-inputacceleration-to-camera.html

The last suggestion to provided is still acting weird. I tried a few variations as well but it still didn't work.
Most Recent Published Games:
Juicy Theater - Android

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #8 on: December 09, 2015, 07:48:21 AM »
Hi,

 this answer from UA is what I explained earlier :)

Are you saying that when you use the script from this asnwer it still doesn't work?

 I am completly overloaded atm, but I'll try to do a sample next week ok?

 Bye,

 Jean

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #9 on: December 09, 2015, 10:51:31 AM »
Hi Jean,

I have not tried the script yet, the link was just to try to show what I was trying to do. I do have a question though to see if I did this right.

Here is what I did based on the solution you provided:

1. Get device accelerometer values and store them in a vector3 every frame.
2. Get the camera's X axis with "transform direction" every frame
3. Use a "vector3 operator" to multiply the accelerometer values by the transform direction values every frame.
4. Take the vector3 from that multiply operation and use it in the add torque value every frame.

I tried many different variations as well but this is the one you suggested I believe. All the ball does (even in the default camera view) is move in one axis, based on the camera view but is always a different axis when the camera turns. It's inconsistent.

If we can't get this going it's ok you don't have to do a sample, I already have a joystick working with the camera turn. I can just take the tilt function out. It would be nice to have a tilt but it is not necessary. I know you are very busy and I don't want to bother you with my stuff too.  :)
Most Recent Published Games:
Juicy Theater - Android

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #10 on: December 10, 2015, 05:37:07 PM »
I don't know if it helps but here is a screenshot (attached) of the camera view in it's default view. The ball just goes forward and backward in this view, then when the camera is turned the ball goes diagonally.
Most Recent Published Games:
Juicy Theater - Android

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #11 on: December 14, 2015, 04:31:37 PM »
Just want to bump this again...sorry ;D
Most Recent Published Games:
Juicy Theater - Android

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Roll-a-Ball Tilt based on dynamic camera forward?
« Reply #12 on: February 03, 2016, 05:47:02 AM »
Hi,

 Made a Fully working sample on the Ecosystem using camera relative controls for rolling a ball. it works with regular Inputs as well as Device Accelerometer.

https://twitter.com/JeanAtPlayMaker/status/694143001174654976

Bye,

 Jean