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 benandlucy · Nov 03, 2013 at 06:11 AM · inputtranslatehorizontal

Player Moving forward and backward, instead of side-to-side

Well, I'm stumped again. Here's my code for my player that is moving

 #pragma strict
 
 
 public var TheCamera : GameObject;
 
 
 
 function Start () {
     Screen.showCursor=false;
     
 }
 
 function Update () {
 transform.forward=TheCamera.transform.forward;
 
     if(Input.GetKey(KeyCode.W)){
     transform.Translate(0,0,5*Time.deltaTime);
 }else if(Input.GetKey(KeyCode.S)){
     transform.Translate(0,0,-5*Time.deltaTime);
     
 }else if(Input.GetKey(KeyCode.D)){
     transform.Translate(0,5*Time.deltaTime,0);
 
 }else if(Input.GetKey(KeyCode.A)){
     transform.Translate(0,-5*Time.deltaTime,0);
 
 }
 }





And here is my code for my camera so that it follows the player

 #pragma strict
 public var ThePlayer : GameObject;
 function Start () {
     
 }
 
 function Update () {
     transform.position.x=ThePlayer.transform.position.x;
     transform.position.z=ThePlayer.transform.position.z;
 }



As you can see here, when I press A or D on my keyboard. My Player should move horizontally, but the D key moves my character forward, and my A key moves my player backward. Another weird thing is that the player doesn't go as fast forwards/backwards as if I were to press W or S. Please help, and asks questions if you have any. :)

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

2 Replies

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

Answer by benandlucy · Nov 03, 2013 at 06:45 AM

I am not a smart man. I have been thinking about this for like 30 minutes now going crazy. I put the numbers in the Y value. Well I feel stupid.

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
1

Answer by Gruffy · Jan 01, 2014 at 03:56 PM

Hello bud, Gruffy again here...

Take your ifs and separate them into individual if statements. This will see they are each checked per Update() and not based on the conditional of one or the other being true or not...which is what you have here.

Hope that helps

regarding your camera shakey stuff is probably because of it all happening in Update (imagine its trying to update the camera the same time as its trying to update the transform`s translation), take this script below and add it to the camera in the inspector(un-checking your other camera script of course).. Drag the object you want the camera to follow into the script that you just added to camera in inspector.

play with your distance and height parameters the script offers in the inspector.. Enjoy dude. Gruffy

(I use this script all the time, its well handy and a nice template for any expansions)

It was converted form the Unify community offering in UnityScript

 using UnityEngine;
 using System.Collections;
 /// <summary>
 /// Smooth cam follow.
 /// Converted by Gruffy (Gareth Wright)2013 from the Unity Scripts "SmoothFollow.js" script
 /// Rewritten in Csharp to conform to the project`s CSharp only program code rule.
 /// This camera smoothes out rotation around the y-axis and height.
 /// Horizontal Distance to the target is always fixed.
 /// There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
 /// For every of those smoothed values we calculate the wanted value and the current value.
 /// Then we smooth it using the Lerp function.
 /// Then we apply the smoothed values to the transform's position.
 /// </summary>
 public class SmoothCamFollow : MonoBehaviour 
 {
 /// <summary>
 /// The target we are following
 /// </summary>
 public Transform target;
 /// <summary>
 /// The distance in the x-z plane to the target
 /// </summary>
 public float distance = 10.0f;
 /// <summary>
 ///  the height we want the camera to be above the target
 /// </summary>
 public float height = 5.0f;
 /// <summary>
 /// The height damping.
 /// </summary>
 public float heightDamping = 2.0f;
     /// <summary>
     /// The rotation damping.
     /// </summary>
 public float rotationDamping = 3.0f;
     /// <summary>
     /// The target point.
     /// </summary>
 private Vector3 targetPoint;
     /// <summary>
     /// The target rotation.
     /// </summary>
 private Quaternion targetRotation;
     /// <summary>
     /// Late update.
     /// find the target and set the rotation to turn toward
     /// do same for world height of transform
     /// aim this transforms look direction at the target transform
     /// dampen correctional positoning and rotation
     /// </summary>
     void LateUpdate () 
     {
         if (!target)
         return;    
     
     float wantedRotationAngle = target.eulerAngles.y;
     float wantedHeight = target.position.y + height;
         
     float currentRotationAngle = transform.eulerAngles.y;
     float currentHeight = transform.position.y;
     
      
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); /// Damp the rotation around the y-axis
 
     
     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); /// Damp the height
 
     
     var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); /// Convert the angle into a rotation
     
     
     
     transform.position = target.position; /// Set the position of the camera on the x-z plane to:
     transform.position -= currentRotation * Vector3.forward * distance; /// distance meters behind the target
 
     
     transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z); /// Set the height of the camera
     
     
     transform.LookAt (target); /// Always look at the target - bad use of Unity in built heavy method
     
     }
 }



NOTE: this uses transform.LookAt(); method which isnt entirely optimized but does offer a simple way to rotate the camera towards your inspector specified transform.

:) Happy new year again dudes..

Gruffy :)

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

16 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

Related Questions

Adapting Horizontal and Vertical movement to New Input System? 0 Answers

Problems with horizontal input. 0 and -1 both= false. 1 Answer

How can I make GUI button send an input message when I press it? 0 Answers

The value of Input.GetAxis("Horizontal") gets stuck when Xbox controller is disconnected... 0 Answers

changing a float number by moving arrows (horizontal and vertical input)? 1 Answer


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