Mikey Lee
Mikey Lee

Reputation: 95

Animation disrupting transform.position , move to mouse click C#

I currently have a script on a game I am working on where a drone is set to move towards the position where my Mouse Cursor has clicked.

This script which will be posted below works completely fine and has no problem in functioning. It was until I added a floating animation to the drone that the script stopped working, due to the transforming position of the animation.

Right now there are no compiler errors when I get into the game, it just does not seem to respond.

An important factor is that my animation is my drone moving up and down

Here is my code:

using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class lightDrone : MonoBehaviour
{
public float speed = 2.0f;

// Our destination needs to be remembered outside a single iteration of
// Update. So we put it outside of the method in order to remember it
// across multiple frames.
private Vector3 currentDestination;

// We need to check if we're at the destination yet so we know when to stop.
private bool notAtDestinationYet;

// When we're closer to the destination than this tolerance, we decide that
// we have arrived there.
private float tolerance = 0.1f;

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            var newPosition = hit.point;
            currentDestination = new Vector3(newPosition.x, 3.0f, newPosition.z);
            notAtDestinationYet = true;
        }
    }

    if (notAtDestinationYet)
    {
        // Use a bit of vector math to get the direction from our current
        // position to the destination. The direction is a normalized Vector
        // that we can just multiply with our speed to go in that direction
        // at that specific speed!

        var heading = currentDestination - transform.position;
        var distance = heading.magnitude;
        var direction = heading / distance;

        // Check if we've arrived at our destination.
        if (distance < tolerance)
        {
            notAtDestinationYet = false;
        }
        else
        {
            // If the remaining distance between us and the destination is
            // smaller than our speed, then we'll go further than necessary!
            // This is called overshoot. So we need to make our speed
            // smaller than the remaining distance.

            // We also multiply by deltaTime to account for the variable
            // amount of time that has passed since the last Update() call.
            // Without multiplying with the amount of time that has passed
            // our object's speed will increase or decrease when the
            // framerate changes! We really don't want that.

            float currentSpeed = Mathf.Clamp(speed * Time.deltaTime,
                Mathf.Epsilon, distance);
            transform.position += direction * currentSpeed;
        }
    }
}

}

Obviously, I want to get this code working, how do I adapt my constantly moving position to match with this code? I am very new so explain like I am 5. Thank you :)

Upvotes: 1

Views: 94

Answers (1)

Sandro Figo
Sandro Figo

Reputation: 183

If your drone has the animation running on the same gameobject as the script, you can create a parent object for the script and have the gameobject with the animation as a child of that.

Parent (has lightDrone  attached)
    Drone (has Animator attached)

Upvotes: 1

Related Questions