Reputation: 21
I'm trying to having moving platforms that my game Object can jump onto and move along with it. I can have the object stay on the platform but not fall through but when the game Object is on the moving platform it gets stretched out or shrunked. I thought I could fix this if I updated the local scale in the code but Unity says it doesn't have the definition for local scale which doesn't make since.
using UnityEngine;
using System.Collections;
public class trigger : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnTriggerEnter (Collider other)
{
other.transform.parent = gameObject.transform;
//other.transform.localScale (1, 1, 1);
}
void OnTriggerExit (Collider other)
{
other.transform.parent = null;
//other.transform.localScale (1, 1, 1);
}
}
Just in case you need to know I have a cube object without a mesh at the same position and size as the platform that acts as a trigger. Just in case you want to see it here's my script to move the platforms.
using UnityEngine;
using System.Collections;
public class movingPlatforms : MonoBehaviour {
public Vector3 pointB;
public GameObject pig;
IEnumerator Start()
{
var pointA = transform.position;
while (true) {
yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
}
}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i= 0.0f;
var rate= 1.0f/time;
while (i < 1.0f) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "PIG")
{
//this.transform.position = pig.transform.position;
//pig.transform.position += this.transform.position;
}
}
void triggerOnStay(Collider collider)
{
if (collider.tag == "PIG")
{
collider.transform.parent = transform.parent;
}
}
}
Is there a way to get the locale scale to be defined in my script, or if not that a better way to have my game Object move with the platform, any help would be appreciated.
update: i changed the locale scale updates to other.transform.localScale = Vector3.one; It workfs fine for the On trigger Exit but for on trigger Enter i get this picture
Upvotes: 1
Views: 6649
Reputation: 4095
Transform has a definition for Transform.localScale the problem is how are you trying to access that element, there is no method called localScale() you have to use the variable localScale (internally is a getter/setter).
other.transform.localScale(1, 1, 1);
try
other.transform.localScale = Vector3.one;
Upvotes: 4