I`m trying to come with an Angry Birds clone in top down perspective, 3D.
So, I have a sling and a bird.
- To move the bird with a mouse I use ScreenPointToRay. Cast a ray from the camera and see if it hit a plane where my sling is placed.
- To limit the drag distance between the bird and a sling I use a ray again. This time it
s cast from the sling in the direction of the bird. In case my draggin exceeds maximum stretch distance (let
s say it is 4), I place my bird on this ray, at point 4.
Here is my code:
using UnityEngine;
using System.Collections;
public class super : MonoBehaviour {
int floorMask; // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
float camRayLength = 100f; // The length of the ray from the camera into the scene.
public Transform ass;
public Ray rayToMouse;
private Vector3 playerToMouse;
// Use this for initialization
void Start () {
floorMask = LayerMask.GetMask ("Floor");
rayToMouse = new Ray (ass.position, Vector3.zero);
}
// Update is called once per frame
void Update () {
float Distance = Vector3.Distance (ass.position, transform.position);
// Create a ray from the mouse cursor on screen in the direction of the camera.
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
// Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask) && Distance <= 4)
{
Vector3 playerToMouse = floorHit.point;
playerToMouse.y = 0f;
transform.position = playerToMouse;
}
else if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask) && Distance >= 4)
{
Vector3 playerToMouse = floorHit.point;
playerToMouse = floorHit.point;
rayToMouse.direction = playerToMouse;
playerToMouse = rayToMouse.GetPoint (4);
playerToMouse.y = 0f;
transform.position = playerToMouse;
}
}
}
Instead of doing what I intended, when I drag the bird to the maximum stretch it begins to blink. One frame it`s away from max distance, another — within in.
How should I fix it?
Thanks.