Question by
DeadCatLady · Aug 25, 2017 at 05:07 AM ·
c#camera followroll a ball
How to make the ball follow the cursor?
Hey all, I haven't been able to find a tutorial for this, but I'm trying to modify the Roll-a-Ball tutorial to making it so the all will follow the cursor.
I have the same player controller from the tutorial :
PlayerController.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win!";
}
}
}
FollowMouse.cs
using UnityEngine;
using System.Collections;
public class FollowMouse : MonoBehaviour
{
public float distance = 1.0f;
public bool useInitalCameraDistance = false;
private float actualDistance;
// Use this for initialization
void Start ()
{
if(useInitalCameraDistance)
{
Vector3 toObjectVector = transform.position - Camera.main.transform.position;
Vector3 linearDistanceVector = Vector3.Project(toObjectVector,Camera.main.transform.forward);
actualDistance = linearDistanceVector.magnitude;
}
else
{
actualDistance = distance;
}
}
// Update is called once per frame
void Update ()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = actualDistance;
transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
}
}
Comment
Your answer
Follow this Question
Related Questions
camera not following target after reload 0 Answers
smooth follow camera too slow when dash 0 Answers
Why is my camera rolling around like a ball? 3 Answers
The roll a ball script isn't working :(((( 2 Answers
Enemies disappear in test mode 0 Answers