playMaker

Author Topic: [SOLVED] How do you make the camera move related to mouse actions?  (Read 4445 times)

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
I'm making a 2D game and I want to manipulate the camera in 2 ways:

1. When clicking on a blank spot on the map, the camera can be dragged around similar to how google maps is navigated.
2. When the cursor is near the edge of the screen it moves in that direction, similar to classic RTS games like Warcraft.

I can't think of how this would be best achieved using the Unity API.  Any hints to get me started on this feature?
« Last Edit: May 26, 2012, 02:51:24 PM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you make the camera move related to mouse actions?
« Reply #1 on: May 25, 2012, 08:01:37 PM »
I've worked on the drag function for the map, and it kind of does what I want it to, but the camera is jittering terribly.  Here is my custom action that does most the work:
Code: [Select]
using System;
using System.Collections;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions{

[ActionCategory("Soul Saga")]
[Tooltip("...")]
public class CameraDragScript : FsmStateAction
{
//Public
[RequiredField]
public FsmGameObject goCamera;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVector3 mousePickOld;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVector3 mousePickNew;
public bool everyFrame;

//Private
float unitinfoValue;
UnitInfo varUnitInfo;
string strDebug;
Space space;

public override void Reset()
{
mousePickOld = null;
mousePickNew = null;
goCamera = null;
everyFrame = false;
space = Space.World;
}

public override void OnEnter()
{
DoDrag();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoDrag();
}

void DoDrag()
{
if (goCamera.Value == null) return;
if (mousePickOld == null) return;
if (mousePickNew == null) return;


var go = goCamera.Value;
Vector3 positionChange;
positionChange = space == Space.World ? go.transform.position : go.transform.localPosition;
float camX = go.transform.position.x;
float camY = go.transform.position.y;
float mouseOldX = mousePickOld.Value.x;
float mouseOldY = mousePickOld.Value.y;
float mouseNewX = mousePickNew.Value.x;
float mouseNewY = mousePickNew.Value.y;

if (mousePickNew.Value.x != mousePickOld.Value.x)
{
positionChange.x = camX + (mouseOldX - mouseNewX);
Debug.Log("Changing X");
}
if (mousePickNew.Value.y != mousePickOld.Value.y)
{
positionChange.y = camY + (mouseOldY - mouseNewY);
Debug.Log("Changing Y");
}

go.transform.position = positionChange;

mousePickOld.Value = mousePickNew.Value;


//Debug

}
}
}

The only other thing in the state is a mouse pick to store a value for mousePickNew.

Any idea why the camera is so jittery?

« Last Edit: May 25, 2012, 08:17:41 PM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4168
  • Official Playmaker Support
    • LinkedIn
Re: How do you make the camera move related to mouse actions?
« Reply #2 on: May 26, 2012, 01:28:43 AM »
Try putting DoDrag in OnLateUpdate instead of OnUpdate.

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you make the camera move related to mouse actions?
« Reply #3 on: May 26, 2012, 12:16:42 PM »
Same results on the late update. =(

I noticed that the numbers the debug is showing for the vector of the mouse pick will start to switch back and forth really quick, any idea why the mouse pick would be giving weird results?
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you make the camera move related to mouse actions?
« Reply #4 on: May 26, 2012, 12:50:20 PM »
After a lot of playing around I realized that I should be grabbing the mouse Y and X coordinates, not picking against an object on the screen.  Silly me. =P

However, you might want to fix the Get mouse X and Get Mouse Y actions in playmaker to have an every frame option.

I ended up just making a custom action that added this, and also consolidated the X and Y into one function rather than 2 separate functions.  Checking if one of the values is null just doesn't attempt to store in that one, giving me freedom and simplicity to do either or both in one action.

This might be a good idea for the base playmaker actions for getting mouse X and Y.

I've placed the action here:
http://hutonggames.com/playmakerforum/index.php?topic=1644.0
« Last Edit: May 26, 2012, 02:51:54 PM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com