Unity moving objects

nananana·2022년 9월 20일
0

Youtube Link
Thank you creator

Tested moving objects using functions. While trying I attempted to use different classes but MonoBehaviour as the name might suggest did not like referencing a new MonoBehaviour.

Perhaps I could have

public class something : MonoBehaviour{
	public void Move(){
    ...
    }
    //Some more methods in here
}
public class cubes : something{
	void FixedUpdate(){
    	//use the functions in "something" 
    }
}

This might work? I might try.
But for now

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

public class MoveWithMouse : MonoBehaviour{
    Vector3 target = new Vector3(8, 1.5f, 1);
    Vector3 init;
    bool hasTouched;
    Vector3 v = Vector3.zero;
    void Start(){
        hasTouched = true;
        init = transform.position;
    }
    void Update(){
        
        // Debug.Log(init);
        
        // Debug.Log(transform.position);


        // transform.position = Vector3.SmoothDamp(transform.position, init, ref v, 0.1f);
        // Debug.Log(v);
        // transform.position = Vector3.SmoothDamp()
    }
    void FixedUpdate(){
        // Debug.Log(gameObject.name);
        if(gameObject.name == "A"){
            if(hasTouched){
                transform.position = Vector3.MoveTowards(transform.position, target, 0.1f); //start, target, speed
            }else{
                transform.position = Vector3.MoveTowards(transform.position, init, 0.1f); //start, target, speed
            }
            if(transform.position == target){
                hasTouched = false;
            }
            if(transform.position == init){
                hasTouched = true;
            }
        }else if(gameObject.name == "A (1)"){
            if(hasTouched){
                transform.position = Vector3.SmoothDamp(transform.position,target,ref v, 0.5f);
            }else{
                transform.position = Vector3.SmoothDamp(transform.position,init,ref v, 0.5f);
            }
            if(transform.position == target){
                hasTouched = false;
            }
            if(transform.position == init){
                hasTouched = true;
            }
        }else if(gameObject.name == "A (2)"){
            if(hasTouched){
                transform.position = Vector3.Lerp(transform.position, target, 0.07f);
            }else{
                transform.position = Vector3.Lerp(transform.position, init, 0.07f);
            }
            if(transform.position == target){
                hasTouched = false;
            }
            if(transform.position == init){
                hasTouched = true;
            }
        }else if(gameObject.name == "A (3)"){
            if(hasTouched){
                transform.position = Vector3.Slerp(transform.position, target, 0.07f);
            }else{
                transform.position = Vector3.Slerp(transform.position, init, 0.07f);
            }
            if(transform.position == target){
                hasTouched = false;
            }
            if(transform.position == init){
                hasTouched = true;
            }
        }









    }
}

Named cubes A, A (1), ... as the name suggests just copy pasted. I will try the above idea.

Turns out works very well

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

public class SubTest : TestBehaviour
{

    // Update is called once per frame
    void FixedUpdate()
    {
        if(gameObject.name == "A"){
            MoveTowards();
        }else if(gameObject.name == "A (1)"){
            SmoothDamp();
        }else if(gameObject.name == "A (2)"){
            Lerp();
        }else if(gameObject.name == "A (3)"){
            Slerp();
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestBehaviour : MonoBehaviour
{
    Vector3 target = new Vector3(8, 1.5f, 1);
    Vector3 init;
    bool hasTouched;
    Vector3 v = Vector3.zero;
    void Start(){
        hasTouched = true;
        init = transform.position;
    }
    public void MoveTowards(){
        if(hasTouched){
            transform.position = Vector3.MoveTowards(transform.position, target, 0.1f); //start, target, speed
        }else{
            transform.position = Vector3.MoveTowards(transform.position, init, 0.1f); //start, target, speed
        }
        if(transform.position == target){
            hasTouched = false;
        }
        if(transform.position == init){
            hasTouched = true;
        }
    }
    public void SmoothDamp(){
        if(hasTouched){
            transform.position = Vector3.SmoothDamp(transform.position,target,ref v, 0.5f);
        }else{
            transform.position = Vector3.SmoothDamp(transform.position,init,ref v, 0.5f);
        }
        if(transform.position == target){
            hasTouched = false;
        }
        if(transform.position == init){
            hasTouched = true;
        }
    }
    public void Lerp(){
         if(hasTouched){
            transform.position = Vector3.Lerp(transform.position, target, 0.07f);
        }else{
            transform.position = Vector3.Lerp(transform.position, init, 0.07f);
        }
        if(transform.position == target){
            hasTouched = false;
        }
        if(transform.position == init){
            hasTouched = true;
        }
    }
    public void Slerp(){
        if(hasTouched){
            transform.position = Vector3.Slerp(transform.position, target, 0.07f);
        }else{
            transform.position = Vector3.Slerp(transform.position, init, 0.07f);
        }
        if(transform.position == target){
            hasTouched = false;
        }
        if(transform.position == init){
            hasTouched = true;
        }
    }
}

0개의 댓글