- Home /
 
               Question by 
               BaksiuGTD · Jul 06, 2012 at 07:45 PM · 
                transform.position  
              
 
              transform.position any suggestions?
I try convert SmoothFollow script from JavaScript to C#:
 using UnityEngine;
     using System.Collections;
     public class Kamera : MonoBehaviour {
      public Transform obiekt;
      public float distance = 10.0f;
      public float height = 10.0f;
      public float heightDamping = 2.0f;
      public float rotationDamping = 3.0f;
      
      void LateUpdate() {
      if (!obiekt)
      return;
      
      float wantedRotationAngle = obiekt.eulerAngles.y;
      float wantedHeight = obiekt.position.y + height;
      
      float currentRotationAngle = transform.eulerAngles.y;
      float currentHeight = transform.position.y;
      
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
     
      currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
     
      Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
     
      transform.position = obiekt.position;
      transform.position -= currentRotation * Vector3.forward * distance;
      transform.position.y = currentHeight;
      transform.LookAt (obiekt.transform);
      }
     }
I have this error: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable. The error concerns this line:
 transform.position.y = currentHeight;
I don't know what's wrong
               Comment
              
 
               
              Answer by whydoidoit · Jul 06, 2012 at 07:48 PM
You need to take the position, modify it and put it back otherwise it will have no effect:
   var pos = transform.position;
    pos.y = currentHeight;
    transform.position = pos;
Answer by madpuppet · Jul 20, 2012 at 12:40 AM
both 'transform' and 'transform.position' are properties, not variables. So, when you do:
 transform.position = obiekt.position
 obiekt.position = transform.position
The code is actually doing something more like:
 GetComponent<Transform>().SetPosition(obiekt.position)
 obiekt.position = GetComponent<Transform>().GetPosition()
Since GetComponent<> can be expensive, you should really do code like:
  Transform tx = transform;
  Vector3 position = obiekt.position;
  position -= currentRotation * Vector3.forward * distance;
  position.y = currentHeight;
  tx.position = position;
  tx.LookAt (obiekt.transform);
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                