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 /
  • Help Room /
avatar image
0
Question by Kamuehle · Mar 28, 2017 at 07:02 PM · codepage

Am I stupid ?

Hello Volkz :),

first to know ... i'm normally not speaking english :D (excuse me, it's just RealTime)

i'm coming over from unreal engine (not really learned, but got some impressions) and i tried to move a camera like i can do there) (Worth to say, i'm a bloody beginner). i've got the forward vector (i think i do) and multiply it with a, let's say, movement speed. The other vectors are handled equally.

It may be important that the project is nearly equal a blank project. I just added a Terrain, put some Cylinders on it, built a Navmesh and added "Ethan" (but only the .fbx not fully) to it (in confidence: just to see how the pathfinding works ;),thats why "Ethan" has an NavMeshAgent and a NavMeshObstacle (Deactivated) Component). (I wonder why i can import 200 meshes built out of MakeHuman with 28,796 faces (14,444 vertices) which take my framerate down to ~36 fps (cooked project) and only 40 "Ethans" which have (as i know (Blender v.2.78)) 7.443 faces (7.442 vertices) wich takes down my framerate to ~30 (cooked project also).

I just trying to test a little bit, (i'm amazed, first looks are like a candy in comparison to unreal engine).

But i didn't get the camera moved with some strange behaviours in the Debug.Log. It's just the "Main Camera" with a additional C# Script called CameraController. If i pushing "w" (standard configuration) it shows me my Debug.Log as i expected... but if i'm pressing "a" it shows me two Debug.Log Messages (I assume the one from the "w" key is showed too).

i've just programmed some kind of C (i'll say "nuts"-C because there wasn't any difficulty) before, but i think something do go wrong.

Here is my code for the CameraController Script (and i will be very happy if you tell me how i put this in an extra "box" (the helpfully text on the right of this window won't show me how) and ill do this (if i can (it's my first post here) as soon as i know how).

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraController : MonoBehaviour {

 private Vector3 ForwardMovement;
 private Vector3 RightMovement;
 private Vector3 UpMovement;
 private bool Debugging;

 void Start () {
     Debugging = true;
 }
 
 void Update () {
 }

 void LateUpdate () {
     float moveVertical = Input.GetAxis ("Vertical");
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveUpDown = Input.GetAxis ("UpDown");

     if (Debugging == true) {
         Debug.Log ("Forward Vector: " + transform.forward);
         Debug.Log ("Right Vector: " + transform.right);
         Debug.Log ("Up Vector: " + transform.up);
     }

     if (moveVertical != 0.0f) {
         Vector3 ForwardMovement = MultiplyVectorWithFloat (transform.forward, moveVertical * 5);
     } else {
         ForwardMovement = ClearVector (ForwardMovement);
     }
     if (moveHorizontal != 0.0f) {
         Vector3 RightMovement = MultiplyVectorWithFloat (transform.right, moveHorizontal * 5);
     } else {
         RightMovement = ClearVector (RightMovement);
     }
     if (moveHorizontal != 0.0f) {
         Vector3 UpMovement = MultiplyVectorWithFloat (transform.up, moveUpDown * 5);
     } else {
         UpMovement = ClearVector (UpMovement);
     }

     if (Debugging == true) {
         Debug.Log ("Forward: " + ForwardMovement.ToString ());
         Debug.Log ("Right: " + RightMovement.ToString ());
         Debug.Log ("Up: " + UpMovement.ToString ());
     }

     Vector3 Movement = ForwardMovement + UpMovement + RightMovement;
     transform.position = transform.position + Movement;
 }
     
 Vector3 AddFloatToVector( Vector3 Vector, float Float ) {
     Vector3 Test = Vector;
     Vector.x += Float;
     Vector.y += Float;
     Vector.z += Float;
     if (Debugging == true) {
         Debug.Log ("Added: " + Vector.ToString () + " with " + Float + " Original: " + Test.ToString ());
     }
     return Vector;
 }

 Vector3 MultiplyVectorWithFloat( Vector3 Vector, float Float ) {
     Vector3 Test = Vector;
     Vector.x *= Float;
     Vector.y *= Float;
     Vector.z *= Float;
     if (Debugging == true) {
         Debug.Log ("Multipied: " + Vector.ToString () + " with " + Float + " Original: " + Test.ToString ());
     }
     return Test;
 }
     
 Vector3 ClearVector(Vector3 Vector) {
     Vector.x += 0.0f;
     Vector.y += 0.0f;
     Vector.z += 0.0f;
     return Vector;
 }

}

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

1 Reply

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

Answer by Rhombuster · Mar 29, 2017 at 03:48 AM

You have the same condition for 2 different if/else statements. Here:

      if (moveHorizontal != 0.0f) {
          Vector3 RightMovement = MultiplyVectorWithFloat (transform.right, moveHorizontal * 5);
      } else {
          RightMovement = ClearVector (RightMovement);
      }
      if (moveHorizontal != 0.0f) {
          Vector3 UpMovement = MultiplyVectorWithFloat (transform.up, moveUpDown * 5);
      } else {
          UpMovement = ClearVector (UpMovement);
      }

The second one should be if (moveUpDown != 0.0f)

You should know that Unity has already overloaded the multiplication operator (*) so instead of writing functions and calling them like this:

 Vector3 UpMovement = MultiplyVectorWithFloat (transform.up, moveUpDown * 5);

You can write this instead:

 Vector3 UpMovement = transform.up * moveUpDown * 5;

Now you can simplify your movement code to look like this:

 void LateUpdate () {
     float moveVertical = Input.GetAxis("Vertical");
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveUpDown = Input.GetAxis("UpDown");

     transform.position += transform.forward * moveVertical * 5;
     transform.position += transform.right * moveHorizontal * 5;
     transform.position += transform.up* moveUpDown * 5;
 }
Comment
Add comment · Show 1 · 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 Kamuehle · Mar 29, 2017 at 06:26 AM 0
Share

This solved the problem entirely (and looks much more professional :) ), much thanks for this!

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

97 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rigidbody2D's velocity not working 1 Answer

Help with code for picking up objects 1 Answer

Unity "ArgumentExeption: The object you want to instantiate is null." need help. 0 Answers

Unexpected 'Void' expecting 'class' 0 Answers

ArgumentOutOfRange: Argument is out of range Parameter name: index 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