Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jvt619 · Dec 01, 2013 at 12:29 PM · transformpositionvector3lines

`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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Josh707 · Dec 01, 2013 at 12:33 PM 0
Share

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 +=

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges