Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Transforming position of object with mouse.

Discussion in 'Scripting' started by mishappp, Jun 13, 2018.

  1. mishappp

    mishappp

    Joined:
    Aug 9, 2017
    Posts:
    8
    I have a character camera in a sitting position on a carriage. His movement is solely based on mouse movement and is clamped to prevent clipping with the cart behind him. What I'm looking to do is based on the rotation of the camera when the clamp maximum is reached to transform the camera position into a "standing" position and release the clamps and slowly transform the position of the camera based on how far your looking over to prevent the camera view to just looking at a wall. I tried using an animator of the camera to transform the position when the criteria is met. It works but it looks bad since its a fixed animation it just kinda jumps if it not spot on. So I'm trying to solve it via scripting.

    I'm using this basic cookie cutter clamp to get the vector coordinates for moving the cameras view.

    Code (CSharp):
    1.  xCor += Input.GetAxis("Mouse X") * xSensitivity * Time.deltaTime;
    2.  yCor += Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
    3.  yCor = Mathf.Clamp(yCor, yMinLimit, yMaxLimit);
    4.  xCor = Mathf.Clamp(xCor, xMinLimit, xMaxLimit);
    5.   Camera.main.transform.localEulerAngles = new Vector3(-yCor, xCor, 0);
    I have been using variations of this to move the camera but mostly I'm just getting jumps into the atmosphere, the math is a little past me.
    Code (CSharp):
    1.    if (xCor >= Xmax)        
    2.         transform.position = new Vector3();
    Any advice would be appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    2,414
    Have you looked into Cinemachine? I imagine it would be actually perfect for this sort of thing probably without writing a line of code.
     
  3. mishappp

    mishappp

    Joined:
    Aug 9, 2017
    Posts:
    8
    Ive been looking over Cinemachine for a while here playing with it. It is an extremely powerful tool but it seems to be main focused on following objects / rails for cutscenes rather then player controlled cameras. While I see this could work for what I'm doing I believe I would run into the same problem I'm having currently.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    2,414
    Could you use cinemachine and have it track an invisible gameobject, one that is controlled directly by the mouse?

    Then when that game object reaches the clamp limit tell cinemachine to go to the standing view? (and vice versa obviously). Cinemachine would be tracking the invisible object all the time, and you can decide what type of tracking to do based on where that mouse-controlled invisible object is. Plus you can temporarily add a sphere to the invisible object to make sure things are doing what you want when debugging the motion.
     
  5. mishappp

    mishappp

    Joined:
    Aug 9, 2017
    Posts:
    8
    Wouldn't the same motion I'm using to control the object make the camera behave the same way? I would run into the issue of making the invisible object move the same way.
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    81
    Can you post a video of what you're talking about? I'm having trouble picturing the situation.
     
  7. mishappp

    mishappp

    Joined:
    Aug 9, 2017
    Posts:
    8
    I'm having trouble coming up with an example video. I have a screenshot from PUBG that will hopefully be able to illustrate this a bit more. Think of sitting in a car aiming forward as you take your aim backwards you will collide with the car seat unless you hang outside of the window. Same concept I'm trying to achieve. I want to take the cameras rotation position while looking left and right and adjust the cameras transform position to prevent collision of the object your sitting on. I can post a screen shot of my actual problem when I get off work.
    https://imgur.com/a/KE6wmYi
     
  8. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    81
    Ah, I see what you mean. I would probably approach that by creating an animation path (either through the unity animator, or by creating a spline, or something along those lines) and controlling the progress via the current rotation.
     
  9. mishappp

    mishappp

    Joined:
    Aug 9, 2017
    Posts:
    8
    Can you continue to control the eulerangles of a camera while an animation is playing on it? In my attempts it seems the animation takes priority over any other transformation of it, is there a way around this?
     
  10. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    81
    If you ensure that your animation only modifies the position and not the rotation, then yes.
     
    mishappp likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    2,414
    Yes, either by the method @Madgvox mentioned above, or by jamming another GameObject into the hierarchy, either above or below the one controlled by the animation, and controlling that one yourself. How you lay it out kinda depends on your problem.
     
    mishappp likes this.
  12. mishappp

    mishappp

    Joined:
    Aug 9, 2017
    Posts:
    8
    Yeah that worked, thanks a ton everyone.
     
    Kurt-Dekker likes this.