Jump to content

Unity animation duplication

Go to solution Solved by Guest,
1 hour ago, stefanmz said:

So I found a YouTube video from unity explaining it using a box and a parachute but it seems too complicated and I can't really think how my example would fit in that script. So is there some generic script that is used for raycasting? Like a template? So I can use that and learn from it? Can you send me one? I will try to find one but if you can find it pls send it to me.

Here is the official text documentation.
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
 

 

I found the video you were talking about. I miss this era of unity tutorials because they were amazing.

Spoiler

 


In the diagram from 0:25 until he goes to the engine explains the arguments.
Before that he explains in english  what is going on.

Origin is a location in 3d space. I assume you know what a position is.

Direction is a bit confusing but it's a vector. (Yes so is position but vectors are often considered more in direction afaik)
So if you did direction like

(0, 0, 1)
This means it will go straight towards the positive Z direction.
So when you move things around, if the position in Z increases, that's the positive Z direction.
I'm hoping you can infer the other 5 directions.

 

hitInfo is a "structure" called RaycastHit.
This collects what collider was hit, where it was hit, and ...

Here's the complete documentation page on it:
https://docs.unity3d.com/ScriptReference/RaycastHit.html

 

Layer mask is a complex subject. I won't get into it but basically you can ignore certain things if you want to.


maxDistance is how far it checks.
Picture you paused your game, took a big stick, and put on end at the origin argument & aimed it in the direction argument direction.
The length of the stick is the maxDistance in unity units. (1 unit is supposed to be about 1 meter)

 

 


at 2:10 you see the script appear
Physics.Raycast returns true if it hits something. That's all it checks.
The hitInfo has the word "out" before it.
Knowing English ourselves, we can infer that this could be some kind of output...
I know C#, so I am telling you, yes, it is an output. This gives more power to programmers instead of just relying on functions returning things.
So the argument "hit" passed in was defined on line 14 in the video C# script.

 

Physics.Raycast has assigned the RaycastHit value from the raycast query to the variable named hit.

Finally, deploymentHeight is how far the raycast shoots.

 

This is executed in the update method, so every single frame of the game, this code executes.

 

The part that probably confused you is that on line 15, a variable "landingRay" of type Ray is defined. This was done to fill in the first 2 arguments of the Raycast with only 1 argument.

 

What can you tell me using this information in how it relates to your initial question about being able to click things with your mouse?
1. What do the units need attached to them?
2. Where would the raycast start?

3. When  you clicked on a unit, how would you consider getting the unit using the raycast?

4. Bonus question... How would you go about finding the direction argument? You can use Google. The answer to question 2 will help you.

On 12/8/2022 at 5:17 AM, fpo said:

Yes! 

 

That's a cool mathematical thingy. 

 

What can you learn about raycast with unity?

Is there any documentation? 

So I found a YouTube video from unity explaining it using a box and a parachute but it seems too complicated and I can't really think how my example would fit in that script. So is there some generic script that is used for raycasting? Like a template? So I can use that and learn from it? Can you send me one? I will try to find one but if you can find it pls send it to me.

Link to post
Share on other sites

1 hour ago, stefanmz said:

So I found a YouTube video from unity explaining it using a box and a parachute but it seems too complicated and I can't really think how my example would fit in that script. So is there some generic script that is used for raycasting? Like a template? So I can use that and learn from it? Can you send me one? I will try to find one but if you can find it pls send it to me.

Here is the official text documentation.
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
 

 

I found the video you were talking about. I miss this era of unity tutorials because they were amazing.

Spoiler

 


In the diagram from 0:25 until he goes to the engine explains the arguments.
Before that he explains in english  what is going on.

Origin is a location in 3d space. I assume you know what a position is.

Direction is a bit confusing but it's a vector. (Yes so is position but vectors are often considered more in direction afaik)
So if you did direction like

(0, 0, 1)
This means it will go straight towards the positive Z direction.
So when you move things around, if the position in Z increases, that's the positive Z direction.
I'm hoping you can infer the other 5 directions.

 

hitInfo is a "structure" called RaycastHit.
This collects what collider was hit, where it was hit, and ...

Here's the complete documentation page on it:
https://docs.unity3d.com/ScriptReference/RaycastHit.html

 

Layer mask is a complex subject. I won't get into it but basically you can ignore certain things if you want to.


maxDistance is how far it checks.
Picture you paused your game, took a big stick, and put on end at the origin argument & aimed it in the direction argument direction.
The length of the stick is the maxDistance in unity units. (1 unit is supposed to be about 1 meter)

 

 


at 2:10 you see the script appear
Physics.Raycast returns true if it hits something. That's all it checks.
The hitInfo has the word "out" before it.
Knowing English ourselves, we can infer that this could be some kind of output...
I know C#, so I am telling you, yes, it is an output. This gives more power to programmers instead of just relying on functions returning things.
So the argument "hit" passed in was defined on line 14 in the video C# script.

 

Physics.Raycast has assigned the RaycastHit value from the raycast query to the variable named hit.

Finally, deploymentHeight is how far the raycast shoots.

 

This is executed in the update method, so every single frame of the game, this code executes.

 

The part that probably confused you is that on line 15, a variable "landingRay" of type Ray is defined. This was done to fill in the first 2 arguments of the Raycast with only 1 argument.

 

What can you tell me using this information in how it relates to your initial question about being able to click things with your mouse?
1. What do the units need attached to them?
2. Where would the raycast start?

3. When  you clicked on a unit, how would you consider getting the unit using the raycast?

4. Bonus question... How would you go about finding the direction argument? You can use Google. The answer to question 2 will help you.

Link to post
Share on other sites

38 minutes ago, fpo said:

Here is the official text documentation.
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
 

 

I found the video you were talking about. I miss this era of unity tutorials because they were amazing.

  Hide contents

 


In the diagram from 0:25 until he goes to the engine explains the arguments.
Before that he explains in english  what is going on.

Origin is a location in 3d space. I assume you know what a position is.

Direction is a bit confusing but it's a vector. (Yes so is position but vectors are often considered more in direction afaik)
So if you did direction like

(0, 0, 1)
This means it will go straight towards the positive Z direction.
So when you move things around, if the position in Z increases, that's the positive Z direction.
I'm hoping you can infer the other 5 directions.

 

hitInfo is a "structure" called RaycastHit.
This collects what collider was hit, where it was hit, and ...

Here's the complete documentation page on it:
https://docs.unity3d.com/ScriptReference/RaycastHit.html

 

Layer mask is a complex subject. I won't get into it but basically you can ignore certain things if you want to.


maxDistance is how far it checks.
Picture you paused your game, took a big stick, and put on end at the origin argument & aimed it in the direction argument direction.
The length of the stick is the maxDistance in unity units. (1 unit is supposed to be about 1 meter)

 

 


at 2:10 you see the script appear
Physics.Raycast returns true if it hits something. That's all it checks.
The hitInfo has the word "out" before it.
Knowing English ourselves, we can infer that this could be some kind of output...
I know C#, so I am telling you, yes, it is an output. This gives more power to programmers instead of just relying on functions returning things.
So the argument "hit" passed in was defined on line 14 in the video C# script.

 

Physics.Raycast has assigned the RaycastHit value from the raycast query to the variable named hit.

Finally, deploymentHeight is how far the raycast shoots.

 

This is executed in the update method, so every single frame of the game, this code executes.

 

The part that probably confused you is that on line 15, a variable "landingRay" of type Ray is defined. This was done to fill in the first 2 arguments of the Raycast with only 1 argument.

 

What can you tell me using this information in how it relates to your initial question about being able to click things with your mouse?
1. What do the units need attached to them?
2. Where would the raycast start?

3. When  you clicked on a unit, how would you consider getting the unit using the raycast?

4. Bonus question... How would you go about finding the direction argument? You can use Google. The answer to question 2 will help you.

yeah so I learned a bit more watching videos and reading documentation and I think I kinda get it now, however to answer the questions about my implementation with the mouse, I first need to actually successfully cast a ray, which I haven't yet. So take a look at this code what do you think is wrong? I am guessing the start needs to be the mouse position and the direction well if I want to draw a straight line from the mouse, transform.forward? Is that right? Why does my ray not being drawn? 

    private Ray ray;
    RaycastHit hit;
    private Vector3 mousePos;


    private void OnMouseDown()
    {
        Debug.DrawRay(mousePos, Vector3.forward, Color.green);
    }

    void Start()
    {
        mousePos = Input.mousePosition;
    }

I tried transform.forward and Vector3.forward nothing works.

Link to post
Share on other sites

23 minutes ago, stefanmz said:

yeah so I learned a bit more watching videos and reading documentation and I think I kinda get it now, however to answer the questions about my implementation with the mouse, I first need to actually successfully cast a ray, which I haven't yet. So take a look at this code what do you think is wrong? I am guessing the start needs to be the mouse position and the direction well if I want to draw a straight line from the mouse, transform.forward? Is that right? Why does my ray not being drawn? 

    private Ray ray;
    RaycastHit hit;
    private Vector3 mousePos;


    private void OnMouseDown()
    {
        Debug.DrawRay(mousePos, Vector3.forward, Color.green);
    }

    void Start()
    {
        mousePos = Input.mousePosition;
    }

I tried transform.forward and Vector3.forward nothing works.

Vector3.Forward...

What direction is that? 

Is it global, or relative to the camera? 

 

 

What about the camera? 

Is this the start point of the raycast?

Does it have any helper methods? 

 

 

 

Also on mouse down, I'm not positive but I think that only executed on the frame in which the button is first pressed. 

Link to post
Share on other sites

10 minutes ago, fpo said:

Vector3.Forward...

What direction is that? 

Is it global, or relative to the camera? 

 

 

What about the camera? 

Is this the start point of the raycast?

Does it have any helper methods? 

 

 

 

Also on mouse down, I'm not positive but I think that only executed on the frame in which the button is first pressed. 

well yes, I need to execute that once since I only need to change a bool to true to play an animation clip. But I tried to do it relative to the camera, I am guessing if I don't specify it's global, but if I do it relative to the camera, I found this https://docs.unity3d.com/Manual/CameraRays.html

and I basically do that 

Ray ray = camera.ScreenPointToRay(Input.mousePosition);
but it doesn't like it like that because it's not an instance so it's not

Ray ray=new Ray(position, direction)

and I can't incorporate the camera example into the instance.

I don't have anything else with the camera just what you see in the code snippet.

And about Vector3.forward well whatever I said here, how do I make it relative to the camera?

Link to post
Share on other sites

Just now, stefanmz said:

well yes, I need to execute that once since I only need to change a bool to true to play an animation clip. But I tried to do it relative to the camera, I am guessing if I don't specify it's global, but if I do it relative to the camera, I found this https://docs.unity3d.com/Manual/CameraRays.html

and I basically do that 

Ray ray = camera.ScreenPointToRay(Input.mousePosition);

 

Do you have a reference to the camera? 

How did you reference the camera? 

Just now, stefanmz said:


but it doesn't like it like that because it's not an instance so it's not

Ray ray=new Ray(position, direction)

and I can't incorporate the camera example into the instance.

I don't have anything else with the camera just what you see in the code snippet.

Look up how to get a camera component and where the script is. 

 

Tip if you cannot find it after a full 10 minutes of googling and finding nothing:

Spoiler

Add the script as a component of a camera. 

 

Use "Get component<Camera>()"

 

Link to post
Share on other sites

6 minutes ago, fpo said:

Do you have a reference to the camera? 

How did you reference the camera? 

Look up how to get a camera component and where the script is. 

 

Tip if you cannot find it after a full 10 minutes of googling and finding nothing:

  Reveal hidden contents

Add the script as a component of a camera. 

 

Use "Get component<Camera>()"

 

oh yeah I forgot to attach the actual component with GetComponent just like the animator. Nevermind it's done now however now the ray actually works and is cast and it recognizes when it hits the object but since it's form the camera, when it hits the object since I have an if statement specifically targeting the object if the ray hits, I get an error that the camera is not attached to the object. So instead of using that specific camera ray that I don't quite get, how about a use a normal ray which I actually understand? But if I do a normal ray, it doesn't cast it, what am I doing wrong? The new Ray is cast like that

ray = new Ray(mousePos, Vector3.forward);

and the mousePos is

 

mousePos = Input.mousePosition;
Link to post
Share on other sites

6 minutes ago, stefanmz said:

oh yeah I forgot to attach the actual component with GetComponent just like the animator. Nevermind it's done now however now the ray actually works and is cast and it recognizes when it hits the object but since it's form the camera, when it hits the object since I have an if statement specifically targeting the object if the ray hits, I get an error that the camera is not attached to the object. So instead of using that specific camera ray that I don't quite get, how about a use a normal ray which I actually understand? But if I do a normal ray, it doesn't cast it, what am I doing wrong? The new Ray is cast like that

ray = new Ray(mousePos, Vector3.forward);

and the mousePos is

 

mousePos = Input.mousePosition;

I'm going to need to see your whole script at this point. 

I assume the script is only attached to the camera game object

Link to post
Share on other sites

8 minutes ago, fpo said:

I'm going to need to see your whole script at this point. 

I assume the script is only attached to the camera game object

no, the script is attached to the flask object( or any object I need to interact with the mouse)

this is it

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Flask_03_pick_up : MonoBehaviour
{
    // Start is called before the first frame update
    private Animator animator = new Animator();
    private Ray ray;
    RaycastHit hit;
    private Vector3 mousePos;
    private Camera camera;
    
    private void OnMouseDown()
    {
        //ray = new Ray(mousePos, Vector3.forward);
        ray = camera.ScreenPointToRay(Input.mousePosition);
    }

    void Start()
    {
        animator=GetComponent<Animator>();
        camera = GetComponent<Camera>();
        mousePos = Input.mousePosition;
    }

    // Update is called once per frame
    void Update()
    {
        if (Physics.Raycast(ray,out hit,Mathf.Infinity))
        {
            Debug.DrawRay(mousePos, Vector3.forward*Mathf.Infinity, Color.green);//(why doesn't this work?)
            
            if (hit.collider.gameObject.name=="Flask_03")
            {
                animator.SetBool("Flask_03_picked_up", true);
            }
        }
    }
}

 

Link to post
Share on other sites

24 minutes ago, stefanmz said:

no, the script is attached to the flask object( or any object I need to interact with the mouse)

this is it

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Flask_03_pick_up : MonoBehaviour
{
    // Start is called before the first frame update
    private Animator animator = new Animator();
    private Ray ray;
    RaycastHit hit;
    private Vector3 mousePos;
    private Camera camera;
    
    private void OnMouseDown()
    {
        //ray = new Ray(mousePos, Vector3.forward);
        ray = camera.ScreenPointToRay(Input.mousePosition);
    }

    void Start()
    {
        animator=GetComponent<Animator>();
        camera = GetComponent<Camera>();
        mousePos = Input.mousePosition;
    }

    // Update is called once per frame
    void Update()
    {
        if (Physics.Raycast(ray,out hit,Mathf.Infinity))
        {
            Debug.DrawRay(mousePos, Vector3.forward*Mathf.Infinity, Color.green);//(why doesn't this work?)
            
            if (hit.collider.gameObject.name=="Flask_03")
            {
                animator.SetBool("Flask_03_picked_up", true);
            }
        }
    }
}

 

few things wrong... read my comments in my adjustments:

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Flask_03_pick_up : MonoBehaviour
{
    private Animator animator = new Animator();
    private Ray ray;
    RaycastHit hit;
    private Vector3 mousePos;
  [SerializeField] //This allows you to edit in the inspector.
    private Camera camera;
    
    private void OnMouseDown()
    {
        //ray = new Ray(mousePos, Vector3.forward);
        ray = camera.ScreenPointToRay(Input.mousePosition);
      //I don't know whether or not this method will execute... in unity you can do the following to find out
      print("On Mouse down is being called!");
      //check the console to see if the string is present when mouse down. 
    }

    void Start()
    {
        animator=GetComponent<Animator>();
        //camera = GetComponent<Camera>(); //You can set the value in the inspector with the serialize field attribute I added.
        mousePos = Input.mousePosition; //When does start get called? Before the first frame of the game. Where is the mouse when the game starts? 
      //We can't know... so this line will not help you. You can delete it. 
    }

    // Update is called once per frame
    void Update()
    {
      mousePos = Input.mousePosition;//Now this frame, you will have the current mouse position. 
      ray = camera.ScreenPointToRay(Input.mousePosition); //Now you will have the ray updated on this frame. 
      
        if (Physics.Raycast(ray,out hit,Mathf.Infinity)){ //This will execute every single frame. Whether or not you click. 
        //You could do (Physics.Raycast(ray, out hit, Mathf.Infinity) && Input.GetMouseButtonDown(0)) to get that the left mouse was clicked on this frame.
            Debug.DrawRay(mousePos, Vector3.forward*Mathf.Infinity, Color.green);//(why doesn't this work?) I didn't use draw ray and haven't used unity for a while. I'm not positive on the ins and outs of debug draw ray. 
            
            if (hit.collider.gameObject.name=="Flask_03")
            {
                animator.SetBool("Flask_03_picked_up", true);
            }
        }
    }
}


It's best to put all your raycast stuff on the camera gameobject and learn how you can adjust it to be more versatile.

I won't write that code for you.

 

One thing you can do is learn about inheritance as an oop principle and see how you can apply that to your clickable items.


This would have been really time consuming for me to get to the bottom of and solve your minor script problems.
Learn what the default unity monobehaviour methods are and when they execute.

Link to post
Share on other sites

35 minutes ago, fpo said:

few things wrong... read my comments in my adjustments:

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Flask_03_pick_up : MonoBehaviour
{
    private Animator animator = new Animator();
    private Ray ray;
    RaycastHit hit;
    private Vector3 mousePos;
  [SerializeField] //This allows you to edit in the inspector.
    private Camera camera;
    
    private void OnMouseDown()
    {
        //ray = new Ray(mousePos, Vector3.forward);
        ray = camera.ScreenPointToRay(Input.mousePosition);
      //I don't know whether or not this method will execute... in unity you can do the following to find out
      print("On Mouse down is being called!");
      //check the console to see if the string is present when mouse down. 
    }

    void Start()
    {
        animator=GetComponent<Animator>();
        //camera = GetComponent<Camera>(); //You can set the value in the inspector with the serialize field attribute I added.
        mousePos = Input.mousePosition; //When does start get called? Before the first frame of the game. Where is the mouse when the game starts? 
      //We can't know... so this line will not help you. You can delete it. 
    }

    // Update is called once per frame
    void Update()
    {
      mousePos = Input.mousePosition;//Now this frame, you will have the current mouse position. 
      ray = camera.ScreenPointToRay(Input.mousePosition); //Now you will have the ray updated on this frame. 
      
        if (Physics.Raycast(ray,out hit,Mathf.Infinity)){ //This will execute every single frame. Whether or not you click. 
        //You could do (Physics.Raycast(ray, out hit, Mathf.Infinity) && Input.GetMouseButtonDown(0)) to get that the left mouse was clicked on this frame.
            Debug.DrawRay(mousePos, Vector3.forward*Mathf.Infinity, Color.green);//(why doesn't this work?) I didn't use draw ray and haven't used unity for a while. I'm not positive on the ins and outs of debug draw ray. 
            
            if (hit.collider.gameObject.name=="Flask_03")
            {
                animator.SetBool("Flask_03_picked_up", true);
            }
        }
    }
}


It's best to put all your raycast stuff on the camera gameobject and learn how you can adjust it to be more versatile.

I won't write that code for you.

 

One thing you can do is learn about inheritance as an oop principle and see how you can apply that to your clickable items.


This would have been really time consuming for me to get to the bottom of and solve your minor script problems.
Learn what the default unity monobehaviour methods are and when they execute.

no problem you actually helped you don't need to solve any other script problems I just forgot that the mouse position needs to be checked every frame. Also thanks for the SerializeField explanation that will be useful to know when I need to use the inspector to edit something from the script. Anyway this is all I need for now thanks for helping me figure this out on my own, and learning raycast which is an awesome technology and I love it! I definitely will use that if I am making games in the future once I figure out the details. Thanks a lot!

Link to post
Share on other sites

59 minutes ago, stefanmz said:

no problem you actually helped you don't need to solve any other script problems I just forgot that the mouse position needs to be checked every frame. Also thanks for the SerializeField explanation that will be useful to know when I need to use the inspector to edit something from the script. Anyway this is all I need for now thanks for helping me figure this out on my own, and learning raycast which is an awesome technology and I love it! I definitely will use that if I am making games in the future once I figure out the details. Thanks a lot!

You're welcome. 

 

Break your problems down and think of what you need to ask. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×