简体   繁体   中英

How do I call the “Movement” method of an object in another script? Unity

I am new in Unity and trying to create an Android mobile game.

First, I decided to create a script called "SelectObject" that will move the objects, provided that the object has a collider.

I add this script to the "MainCamera" components, since we need to determine if the camera ray touched the object.

Here is the script "SelectObject":

using UnityEngine;
using System.Collections;
 
public class SelectObject : MonoBehaviour
{
    void Start() {
    }
    public void Update()
    {
        if ((Input.touchCount > 0) && (Input.touches[0].phase == TouchPhase.Began)) {
            Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
            RaycastHit hit; //Declaring the variable hit, in order to further determine if the camera's ray touhed the object
            if (Physics.Raycast(ray, out hit)) {
                if (hit.collider != null) { //If the ray of camera touches the collider
                    hit.collider.GetComponent<Move>().Movement(); //Calling the "Movement" method from script "Move" to move the object
                }
            }
        }
    }
}

The Move class is a component of the "Cylinder" object that should move when you click on it.

Here is the "Move" class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Move : MonoBehaviour {
    public float speedModifier; //Determine the speed of movement
    public Touch touch; //Using this variable, we will determine if there was at least some touch on the screen
    void Start() {
        speedModifier = 2.01f;
}
 
    public void Update() {
 
    }
    public void Movement() { //Creating a "Movement" method to call it in the "SelectObject" script
        touch = Input.GetTouch(0);
 
        if(touch.phase == TouchPhase.Moved) {
           transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * speedModifier, transform.position.y, transform.position.z + touch.deltaPosition.y * speedModifier);
        }
    }
}

But when I start the game, the cylinder still doesn't move.

What am I doing wrong?

Not 100% clear on what you're trying to do but have you tried passing your location into the Movement() method? Something like this.

public void Movement(Vector3 targetLocation) {
    transform.position = targetLocation;
}

Then you'd call it like this.

Movement(Input.touches[0].position);

Just a note since I noticed your speed modifier. The way your code is currently set up is going to instanly move the object to the touch location. If you would like to have it smoothly move to it at a specified speed you'll need to look up Lerping or store the previous position, current, and target position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM