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.
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)
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.
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:
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.
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.”)