Search Unity

JS - Player walks into the wall when turned 90 degrees

Discussion in 'Scripting' started by PhysicalBeef, Nov 28, 2015.

  1. PhysicalBeef

    PhysicalBeef

    Joined:
    Jun 13, 2013
    Posts:
    150
    Code (csharp):
    1.  
    2.     if(Physics.Raycast (transform.position, transform.forward, 0.8)) {
    3.      
    4.     }else{
    5.         if(Input.GetKey("w")) {
    6.             transform.Translate(Vector3.forward * GameHandler.walkSpeed * Time.deltaTime);
    7.         }
    8.     }
    9.  
    Before you go on saying "this could be better blah blah blah" yes I know this is bad.

    With this code, I'm generating a Raycast forwards from the player. This stops him from moving into walls, and it works. My problem is this: When I walk sideways into the wall, and look just right so that my ray isn't in the wall, i can walk into it. I can't walk more than half my body, since, you know, the ray is in the middle. I've tried, however, making 3 rays coming from the player, on both sides and one in the middle, but they acted weird, and didn't work. How can I work around this? I'd really need a quick answer. Thanks! :)

    EDIT: Please don't post rigidbody answers.
     
    Last edited: Nov 28, 2015
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Usually you would put a collider component (normally a sphere or capsule collider) on the player and move them around using physics. That way you would let the physics system detect the collisions and stop the player from going through objects.

    to move the player you would do something like this instead of transform.Translate():

    Code (csharp):
    1.  
    2. if(Input.GetKey("w"))
    3. {
    4.      Vector3 newVelocity = transform.forward * GameHandler.walkSpeed;
    5.      rigidbody.AddForce( newVelocity - rigidbody.velocity, ForceMode.VelocityChange ); // add the difference of new and old velocity so that we end up moving at newVelocity.
    6. }
    7.  
    note: make sure you have a rigidbody component on the player as well with isKinematic is set to false.
     
  3. PhysicalBeef

    PhysicalBeef

    Joined:
    Jun 13, 2013
    Posts:
    150

    I don't REALLY want a rigidbody since of the in-air speedup that I'm not sure how to work around, but if you have any way of that to not happen then please share.

    EDIT: I also have my own gravity, so I don't really like rigidbodies at all.
     
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    What do you mean by in-air speedup?

    Trust me, It'll be easier to get the rigidbody to move how you want than it will be to make your own collision detection system.
     
  5. PhysicalBeef

    PhysicalBeef

    Joined:
    Jun 13, 2013
    Posts:
    150
    When you jump while in movement you usually go a lot faster in the air than on the ground. At least by my rigidbody experience.

    Also, I'm trying to make most things by hand instead of using the in-built physics, since I'd rather have full control over (mostly) everything.
     
  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    If you're applying the walk force as I've done above you shouldn't go any faster. If you want to be able to jump and let gravity work as normal then you'll need to do something like this:

    Code (csharp):
    1.  
    2. if(Input.GetKey("w"))
    3. {
    4.      Vector3 newVelocity = transform.forward * GameHandler.walkSpeed;
    5.      newVecotiy.y = rigidbody.velocity.y; // don't interfere with gravity acceleration.
    6.      rigidbody.AddForce( newVelocity - rigidbody.velocity, ForceMode.VelocityChange ); // add the difference of new and old velocity so that we end up moving at newVelocity.
    7. }
    8.  
     
  7. PhysicalBeef

    PhysicalBeef

    Joined:
    Jun 13, 2013
    Posts:
    150
    line 4 returns an error. "insert semicolon at the end" possibly wrong parsing?
     
  8. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CharacterMovement : MonoBehaviour
    6. {
    7. public Rigidbody Rigidbody;
    8. float walkSpeed = 3;
    9.  
    10. void Update ()
    11. {
    12.      if(Input.GetKey("w"))
    13.      {
    14.           Vector3newVelocity = transform.forward * walkSpeed;
    15.           newVelocity.y = Rigidbody.velocity.y;
    16.           Rigidbody.AddForce( newVelocity - Rigidbody.velocity, ForceMode.VelocityChange; //addthedifferenceofnewandoldvelocitysothatweendupmovingatnewVelocity.
    17.           }
    18.      }
    19. }
    20.  
     
  9. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    There wasn't anything wrong with those lines I posted.. Here I wrote a whole script for you. But I have to run now. Good luck.