Question by
galaboy · Aug 21, 2015 at 06:45 AM ·
movementscriptingbasicsbasic programming
need help on character movement
hi, doing a subwaysurfer like game, but with car. i got stuck with the character's movement.my character is moving teleporting from one point to another, but i want a continues slow movement, where the player can see the character moving. need help on where iam making mistake.please explain.
using UnityEngine;
using System.Collections;
public class inputManager : MonoBehaviour {
public float playerSpeed;
public float playerStrifingValue;
public GameObject player;
public float playerTimeTakenToLerp;
private Vector3 firstSwipe;
private Vector3 secondSwipe;
private Vector3 currentSwipe;
private float movementDistance;
//private float carMovement;
//private float leftEndPosition = -3.17f;
//private float rightEndPosition = 3.17f;
//private float accleration;
//float otherObjectYlocalScale;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
player.transform.Translate((Vector3.forward * playerSpeed) * Time.deltaTime);
if (Input.GetMouseButtonDown (0))
{
firstSwipe = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButtonUp (0))
{
secondSwipe = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
currentSwipe = new Vector3(secondSwipe.x - firstSwipe.x,
secondSwipe.y - firstSwipe.y,
secondSwipe.z - firstSwipe.z);
currentSwipe.Normalize();
//print(firstSwipe);
//print(currentSwipe);
}
PlayerMovement ();
}
void PlayerMovement()
{
if (Input.GetMouseButtonUp (0))
{
if (currentSwipe.x < 0 & currentSwipe.y < 0.5f & currentSwipe.y > -0.5f)
{
player.transform.position = Vector3.Lerp(player.transform.position,
new Vector3((currentSwipe.x * playerStrifingValue) + player.transform.position.x,
player.transform.position.y,
player.transform.position.z),
playerTimeTakenToLerp * Time.deltaTime);
}
if (currentSwipe.x > 0 & currentSwipe.y < 0.5f & currentSwipe.y > -0.5f)
{
player.transform.position = Vector3.Lerp(player.transform.position,
new Vector3((currentSwipe.x * playerStrifingValue) + player.transform.position.x,
player.transform.position.y,
player.transform.position.z),
playerTimeTakenToLerp * Time.deltaTime);
}
if (player.transform.position.x < -3f)
player.transform.position = new Vector3(-3f, player.transform.position.y,player.transform.position.z);
if(player.transform.position.x > 3f)
player.transform.position = new Vector3(3f, player.transform.position.y,player.transform.position.z);
}
}
}
Comment
Answer by Wolfshadow · Aug 21, 2015 at 12:16 PM
try using addForce. As seen in unity scripting reference:
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour {
public float thrust;
public Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
rb.AddForce(transform.forward * thrust);
}
}
AddForce is very smooth. Think of pushing an object. I use it for my movement and jumping
Answer by moriggi · Aug 22, 2015 at 07:52 PM
AddForce is ok but multiply x Time.DeltaTime x speed that must be a public float speed =10f; and set well in the inspector by