- Home /
 
`UnityEngine.Transform.position'. Consider storing the value in a temporary variable
JS
     #pragma strict
 var walkSpeed : float = 7; //Regular speed
 var crouchSpeed : float = 3; //Speed while crouching
 var sprintSpeed : float = 13; // Speed while sprinting
 private var charMotor : CharacterMotor;
 private var charController : CharacterController;
 private var theTransform : Transform;
 private var charHeight : float; //Initial height
 function Start () 
 {
     charMotor = GetComponent(CharacterMotor);
     theTransform = transform;
     charController = GetComponent(CharacterController);
     charHeight = charController.height;
 }
 function Update () 
 {
     var h = charHeight;
     var speed = walkSpeed;
     
     if (charController.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift"))
     {
         speed = sprintSpeed;
     }
     
     if (Input.GetKey("c"))
     {
         h = charHeight*0.5;
         speed = crouchSpeed;
     }
     
     charMotor.movement.maxForwardSpeed = speed; // Setting the max speed
     var lastHeight = charController.height; //Stand up/crouch smoothly
     charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);
     theTransform.position.y += (charController.height-lastHeight)/2; //Fix vertical position
 }
 
               C#
 using UnityEngine;
 using System.Collections;
 
 public class SprintAndCrouch : MonoBehaviour {
     
     public float walkSpeed = 7;
     public float runSpeed = 13;
     public float crouchSpeed = 3;
 
     private float speed;
 
     //Stamina Variables
     public int stamina;
     public int maxStamina = 100;
     private bool canRun = true;
 
     private CharacterController charControl;
     private CharacterMotor charMotor;
     private Transform theTransform;
     private float charHeight;
 
     // Use this for initialization
     void Start () 
     {
         charMotor = GetComponent <CharacterMotor>();
         charControl = GetComponent <CharacterController>();
         theTransform = new GameObject().transform;
         charHeight = charControl.height;
     }
     
     // Update is called once per frame
     void Update ()
     {
         speed = walkSpeed;
 
         Sprint ();
         Crouch ();
     }
     
     void Sprint()
     {
         int fatigueAdd = 1;
         
         if (Input.GetKey (KeyCode.LeftShift) && charControl.isGrounded && stamina <= maxStamina && canRun == true) 
         {
             canRun = true;
             
             speed = runSpeed;
             
             stamina = stamina + fatigueAdd;
             
             //Walks if stamina is greater than max stamina
             if(stamina >= maxStamina)
             {
                 speed = walkSpeed;
                 canRun = false;
             }
         }
         
         //If not running decrement stamina
         else if(stamina > 0)
         {
             stamina -= 1;
             
             //Set canRun to true if stamina hits 0
             if(stamina == 0)
                 canRun = true;
         }
         
         charMotor.movement.maxForwardSpeed = speed;
         charMotor.movement.maxSidewaysSpeed = speed;
     }
 
     void Crouch()
     {
         float h = charHeight;
 
         if(Input.GetKey(KeyCode.C))
         {
             h = charHeight / 2;
             speed = crouchSpeed;
         }
 
         charMotor.movement.maxForwardSpeed = speed;
         charMotor.movement.maxSidewaysSpeed = speed;
 
         float lastHeight = charControl.height;
         charControl.height = Mathf.Lerp (charControl.height, h, Time.deltaTime * 5);
         //theTransform.position.y += (charControl.height - lastHeight) / 2;
 
         //Vector3 temp = new Vector3(transform.position.x, charControl.height - lastHeight/2, transform.position.z);
 
 //Bug where the player falls out of the plane/ground
         Vector3 temp = Vector3.zero;
         temp.y = (charControl.height - lastHeight) / 2;
         theTransform.position += temp;
     }
 }
 
 
               Are this lines of code the same.
Since in the C# script there's a bug where the Player is gonna fall of the plane/ground while the JS is perfectly fine and doesn't produce a bug like in the C# script.
Well the Javascript version is directly setting y axis, the C# version you have is adding x, y & z axes all together, ins$$anonymous$$d of just the y axis like you want
Just replace
 Vector3 temp = theTransform.position;
 
                  With
 Vector3 temp = Vector3.zero;
 
                  Since you're using +=
Answer by AndreiKubyshkin · Dec 01, 2013 at 02:00 PM
in c# it would be like that:
 Vector3 temp = theTransform.position;
 temp.y += (charControl.height - lastHeight) / 2;
 theTransform.position = temp;
 
              Answer by belvita · Dec 01, 2013 at 12:56 PM
u must do like this, without the word new u will get that error okay!hope i haave helped u please vote,thank u
Vector3 temp=new Vector3(transform.position.x,charControl.height - lastHeight/2,transform.position.z);
Your answer
 
             Follow this Question
Related Questions
Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer
Issue's With Positioning 0 Answers
Play animation as the value changes from 0 to 1 with changing GameObject's position in unity 0 Answers
Reposition to minimum distance from target 2 Answers
How to make your object follow your touch position faster or instantaneously??? 2 Answers