Newbie movement axis Question!

Hello world i have a problem with my movement axis between scenes i am new with programming and i may be out of my league with this but i want to learn :D

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

public class PlayerMovement : MonoBehaviour
{
//walkfloats
[SerializeField] float walkSpeed = 6.0f;
[SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
CharacterController controller = null;
//dir
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
//gravity
[SerializeField] float gravity = 13.0f;
float velocityY = 0.0f;
private void Start()
{
controller = GetComponent();
}
private void Update()
{
UpdateMovement();
}
void UpdateMovement()
{//dir
Vector2 targetDir = new Vector2(Input.GetAxisRaw(“Horizontal”), Input.GetAxisRaw(“Vertical”));

    currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
    //gravity/check ground
    if (controller.isGrounded)
        velocityY = 0.0f;
    velocityY += gravity * Time.deltaTime;

 //force  
    Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x)*walkSpeed+Vector3.down*velocityY;
    controller.Move(-velocity * Time.deltaTime);
}

}

There are a number of points that rely on assumptions in the Controller code (not posted)… making it difficult to determine the correctness of this code… It looks like this might be the code from the old Standard Assets PlayerController?
If so, the Controller.Move() relies on the animator to make turns, etc…

For moving a cylinder… all of this can be reduced to fewer lines of code:

void Update()
{
     Vector3 targetDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis(Vertical));  
     //GetAxis automatically smooths changes in speed
     //You'll need a [SerializeField] float turnSpeed;   Adjust value to taste.  Make currentDir a Vector3
     currentDir=Vector3.Lerp(currentDir, targetDir, Time.DeltaTime*turnSpeed);
     transform.Lookat(transform.position + currentDir); //This faces the player
     //Next line will move the character forward in the direciton he's facing.
     transform.Translate(Vector3.forward, walkSpeed*Time.deltaTime); 
}

If you put a rigidbody on the player, gravity will deal with itself.

Privacy & Terms