How to stop the camera from changing the axis?

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.

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?

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)

That seems very odd. Post a screenshot of your entire scene, especially your scene hierarchy.

You’re missing the scene hierarchy, the most important part :smile:

My bad, here it it. :wink:2176089--144118--Hierarchy for scene.png

Nothing weird there. Can you share the code for the ball following the mouse? It must be somewhere in there.

    var depth = 10.0;
    
    function Start ()
    {
         Cursor.visible = false;
    }
    
    function Update ()
    {
         var mousePos = Input.mousePosition;
         var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
         transform.position = wantedPos;
    }

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#:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance = 0.0f;

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

if (plane.Raycast(ray, out distance) == true)
{
     Vector3 point = ray.GetPoint(distance);

     transform.position = new Vector3(point.x, 0.0f, point.z);
}

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.

The code GroZZleR posted has to go into a method.

Is this correct?

using UnityEngine;
using System.Collections;

public class FollowMouse : MonoBehaviour {

    void Start () {
      
    }

    void Update () {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        float distance = 0.0f;
      
        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
      
        if (plane.Raycast(ray, out distance) == true)
        {
        Vector3 point = ray.GetPoint(distance);      
        transform.position = new Vector3(point.x, 0.0f, point.z);
        }
    }
}

I’m not getting any errors, but when I try to attach it to the ball/sphere I get this:2178189--144286--message.png

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. 2183405--144707--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.”)