Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tasneem kmail · May 26, 2016 at 07:54 PM · rigidbodyspeedforce

How to speed up my character?

I have a character in a 3D game that moves around, the problem is he is very slow .I've tried to speed him up by adding a force ,it works perfectly, but it creates another problem which is the character starts to go inside walls because of this force.. How can i speed him up using another way? This is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Final_PlayerMovement : MonoBehaviour {
     GameObject ins;
     Transform mainCamera;        //holds the main camera as a Transform object
     public static Animator animController;    //holds the player character's animator controller
     public float rotationSpeed=80;
     public float speed=1000000;
     Rigidbody RG;
     //the character's possible movement states
     enum MovementState
     {
         Idle = 0,
         Walking = 1,
         Running = 2
     }
 
     //holds the player's current enumerated movement state
     MovementState currentMovementState;
     
     // Use this for initialization
     void Start () {
         mainCamera = Camera.main.transform;        //initialize the camera's transform
         animController = GetComponent<Animator>();    //initialize the animator controller
         RG=this.gameObject.GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         
         //get axis input from player (W, A, S, and D keys/Arrow keys)
         float verticalAxis = Input.GetAxis("Vertical");
         float horizontalAxis = Input.GetAxis("Horizontal");
 
         //get the forward-facing direction of the camera
         Vector3 cameraForward = mainCamera.TransformDirection(Vector3.forward);
         cameraForward.y = 0;    //set to 0 because of camera rotation on the X axis
 
         //get the right-facing direction of the camera
         Vector3 cameraRight = mainCamera.TransformDirection(Vector3.right);
 
         //determine the direction the player will face based on input and the camera's right and forward directions
         Vector3 targetDirection = horizontalAxis * cameraRight + verticalAxis * cameraForward;
 
         //normalize the direction the player should face
         Vector3 lookDirection = targetDirection.normalized;
 
         //rotate the player to face the correct direction ONLY if there is any player input
         if (lookDirection != Vector3.zero)
             transform.rotation = Quaternion.LookRotation(lookDirection);
 
         //if there is any player input...
             if (verticalAxis != 0 || horizontalAxis != 0) {
 
                 //if the run modifier key is pressed
             if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
                 currentMovementState = MovementState.Running;    //set movement state to running
                 RG.AddForce (targetDirection * 10000000);
 
             } else {    //if there is no movement modifier
                 currentMovementState = MovementState.Walking;
                 RG.AddForce( targetDirection*5000000 );
             }//set movement state to walking
             } else {    //default to idle if no input
                 currentMovementState = MovementState.Idle;    //set movement state to idle
             }
 
 
         //send the current movement state to the character controller
         animController.SetInteger("MovementState", (int)currentMovementState);
 }
 }
 


Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get collision force, not relative velocity? 1 Answer

Using rigidbody.AddForce and speeding the object 2 Answers

Calculate force for torque by specifying a required velocity 1 Answer

Calculating the force required (Rigidbody) 3 Answers

Simple colliding objects are updated a frame late. 0 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