Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to stop the camera from changing the axis?

Discussion in 'Scripting' started by Denisowator, Jun 24, 2015.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I have a ball that follows the mouse, and I've been having trouble with locking one of the axis so that the ball doesn't go up and down, only back and forth. And I've realized that the problem is the "main camera". As when I put the ball at a certain position, and I play the ball appears slightly higher. Then when I raise the camera, the ball also rises when I hit play.

    Any idea how to fix it? Also please explain to me like I'm 5.
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Wait. The ball moves when you move the camera? Does that happen while the player is paused, too? If so, is the camera a child of the ball object?
     
  3. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    No, what happens is let's say I put the ball on the surface of a box, and when I hit play, the ball is slightly above the box. So when I exit play, and move the camera a bit more up, and hit play, the ball is even higher. (it only changes height when I hit play)
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    That seems very odd. Post a screenshot of your entire scene, especially your scene hierarchy.
     
  5. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're missing the scene hierarchy, the most important part :D
     
  7. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    My bad, here it it. ;) Hierarchy for scene.png
     
  8. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Nothing weird there. Can you share the code for the ball following the mouse? It must be somewhere in there.
     
  9. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Code (JavaScript):
    1.     var depth = 10.0;
    2.    
    3.     function Start ()
    4.     {
    5.          Cursor.visible = false;
    6.     }
    7.    
    8.     function Update ()
    9.     {
    10.          var mousePos = Input.mousePosition;
    11.          var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
    12.          transform.position = wantedPos;
    13.     }
    14.  
     
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    If you're looking to move the ball along a plane, you're going to have to project the mouse onto a plane first. Unfortunately, I don't know the syntax for JS, but here it is in C#:
    Code (csharp):
    1.  
    2. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3. float distance = 0.0f;
    4.  
    5. Plane plane = new Plane(Vector3.up, new Vector3(0.0f, 0.0f, 0.0f)); // replace the Y with where you want it in the world
    6.  
    7. if (plane.Raycast(ray, out distance) == true)
    8. {
    9.      Vector3 point = ray.GetPoint(distance);
    10.  
    11.      transform.position = new Vector3(point.x, 0.0f, point.z);
    12. }
    13.  
     
  11. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I copied the whole thing, created a C# Script and pasted it (after deleting the default lines of code that appear every time you start a new script). And when I saved the script I got 4 errors.

    Here they are. errors.png
     
  12. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    The code GroZZleR posted has to go into a method.
     
  13. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Is this correct?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FollowMouse : MonoBehaviour {
    5.  
    6.     void Start () {
    7.      
    8.     }
    9.  
    10.     void Update () {
    11.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.         float distance = 0.0f;
    13.      
    14.         Plane plane = new Plane(Vector3.up, new Vector3(0.0f, 205.3f, 0.0f)); // replace the Y with where you want it in the world
    15.      
    16.         if (plane.Raycast(ray, out distance) == true)
    17.         {
    18.         Vector3 point = ray.GetPoint(distance);      
    19.         transform.position = new Vector3(point.x, 0.0f, point.z);
    20.         }
    21.     }
    22. }
    I'm not getting any errors, but when I try to attach it to the ball/sphere I get this: message.png
     
  14. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Ok, let's start over.




    I completely reset everything and put in the models.
    From the point of view the game is gonna be in (top-down view), the X axis is up and down, the Y axis is towards and away from the camera, and the Z axis is left and right. Here's an image of the axis. Axis box.PNG

    I want the sphere (character) to be at the position of the mouse cursor at all times. Please keep in mind that this doesn't count for when the level starts and restarts.

    For when it starts - The position of the cursor will be changed to the sphere's position. (This is so that the sphere doesn't teleport to wherever the cursor is and allow people to either cheat or immediately be in the wall and die)

    For when it restarts - The ball will be teleported back to the starting position, and just like when the level starts, the cursor will be teleported to the sphere's position.


    I also want the sphere to be able to only move in the X and Z axis and be locked to the starting position of the Y axis. (This is so that the sphere doesn't move towards and away from the camera, which would cause it to also go above or below the walls of the levels. Again, which would allowing people to cheat)


    And keep in mind the very last phrase of my first post on this thread. ("Also please explain to me like I'm 5.")