- Home /
How can I make two new methods one for the scaling one for the rotating to use from other scripts ?
When I press once on F key the object is scaling getting bigger and rotating facing the camera at the same time. Pressing F again will scale it down back with rotation facing the other direction.
What I want to do is to add two new public methods to the script so I can access the methods from other scripts when referencing to this script. And this two new methods should do the same thing when pressing F key but without pressing the F key. I want for example that if in a new script I will call both methods Scaleobj and Rotateobj in Update it will scale and rotate it at the same time just like I was pressing F.
The idea is that in some situation/s in the game I will want it to scale and rotate facing the camera once and then after something happen it will scale and rotate back.
The ObjectsManipulation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectsManipulation : UnityEngine.MonoBehaviour
{
//Camera
public Camera playerCamera;
//Scaling
private Scaling scaling;
//Lights
public DimLights dimlights;
private Coroutine lightCoroutine;
//Colors
private Colors colors;
//Rotating
public bool stopRotation = false;
private Rotating rotating;
private void Start()
{
scaling = GetComponent<Scaling>();
scaling.Inits();
colors = GetComponent<Colors>();
colors.Start();
rotating = GetComponent<Rotating>();
}
// Use this for initialization
void Update()
{
if (playerCamera != null)
{
//Scaling
if (Input.GetKeyDown(KeyCode.F))
{
//Flip the scale direction when F key is pressed
scaling.scaleUp = !scaling.scaleUp;
//Stop old coroutine
if (scaling.scaleCoroutine != null)
StopCoroutine(scaling.scaleCoroutine);
if (lightCoroutine != null)
StopCoroutine(lightCoroutine);
//Scale up
if (scaling.scaleUp)
{
//Start new coroutine and scale up within 5 seconds and return the coroutine reference
rotating.rotateBack = false;
scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.maxSize, scaling.duration, playerCamera));
if (dimlights.lightsOnOff == false)
lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(1, scaling.duration));
}
//Scale Down
else
{
//Start new coroutine and scale up within 5 seconds and return the coroutine reference
rotating.rotateBack = true;
scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.minSize, scaling.duration, playerCamera));
if (dimlights.lightsOnOff == false)
lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(0, scaling.duration)); ;
}
}
}
//Rotate
if (Input.GetKey(KeyCode.R) && !scaling.scaleUp)
{
rotating.x += Time.deltaTime * rotating.rotationSpeed;
scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
rotating.keyPressed = true;
}
if (Input.GetKeyUp(KeyCode.R))
{
rotating.keyPressed = false;
}
if (!rotating.keyPressed && !scaling.scaleUp && rotating.rotateBack == false
&& DetectInteractable.detected == false)
{
scaling.objectToScale.transform.rotation = Quaternion.LookRotation(playerCamera.transform.forward);
}
if (DetectInteractable.detected == true && !scaling.scaleUp && stopRotation == false)
{
rotating.x += Time.deltaTime * rotating.rotationSpeed;
scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
}
}
}
The Scaling script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scaling : UnityEngine.MonoBehaviour
{
public GameObject objectToScale;
public GameObject lookAtTarget;
public float duration = 1f;
public Vector3 minSize;
public Vector3 maxSize;
public bool scaleUp = false;
public Coroutine scaleCoroutine;
public void Inits()
{
objectToScale.transform.localScale = minSize;
}
public IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration, Camera objectToScaleCamera)
{
float counter = 0;
Vector3 startScaleSize = targetObj.transform.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
if (scaleUp)
{
var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
}
else
{
var lookPos = lookAtTarget.transform.position - objectToScale.transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(objectToScaleCamera.transform.forward);().transform.forward);
objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, counter / duration);
}
yield return null;
}
}
}
The Rotating script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotating : UnityEngine.MonoBehaviour
{
public float rotationSpeed;
public float rotationDuration;
public float x;
public bool keyPressed = false;
public bool rotateBack = false;
}
What I tried to do so far is creating a new script for testing what I want to do: It does scaling but not rotating and I'm not using any methods and it's far from what I wanted.
This is what I tried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NaviScene : MonoBehaviour
{
public Camera playerCamera;
public DimLights dimlights;
public Scaling scaling;
private Coroutine lightCoroutine;
private void Start()
{
scaling.scaleUp = true;
}
private void Update()
{
if (scaling.scaleUp == true && DOFControl.hasFinished == true)
{
scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.maxSize, scaling.duration, playerCamera));
if (dimlights.lightsOnOff == false)
lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(1, scaling.duration));
scaling.scaleUp = false;
scaling.objectToScale.transform.rotation = Quaternion.LookRotation(playerCamera.transform.forward);
}
}
}
Answer by RocketFriday · Jul 12, 2018 at 09:55 AM
Right well, I haven't gone through any of that ^ but I can tell you that all you gotta do is create a public method and copy the code from the two methods you use with the F key and then add in a bool to the Update method, then change that bool when you want to call that method. Example:
void Update()
{
if ( boolName == true )
{
otherScript.NewMethod();
boolName = false;
}
}
Answer by sisse008 · Jul 12, 2018 at 11:46 AM
your current script:
public class Current
{
Method1(){}
Method2(){}
void Update()
{
if(press key)
{
Method1();
Method2();
}
}
}
your updated script:
public class Updated
{
public Method1(){}
public Method2(){}
void Update()
{
}
}
your other script:
public class Other
{
public Updated updated;
void Update()
{
if(press key) //or what ever event you want
{
updated.Method1();
updated.Method2();
}
}
}
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i pick two randomly items from gameobject array ? 1 Answer
Script for a hue rainbow scroll on the material 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers