Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

player doesnt collide with objects

Discussion in 'Scripting' started by ficho, May 29, 2011.

  1. ficho

    ficho

    Joined:
    Dec 17, 2010
    Posts:
    82
    hello whats wrong with my player collision i have a script on it to move and character controller to collide and doesnt colide

    note: tried box collider , with triggers and without triggers any idea why doesnt colide?
     
  2. JoshtheCoder

    JoshtheCoder

    Joined:
    May 29, 2011
    Posts:
    11
    So, you have a character controller as the player. right? Also you have the object(s) you want to collide with and in these object(s) you have a box collider and these/this box collider is not a trigger? If all is correct it should work correctly...
     
  3. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    What are you using to move the character? Transform.position = something? Transform.Translate? CharacterController.Move?
     
  4. JoshtheCoder

    JoshtheCoder

    Joined:
    May 29, 2011
    Posts:
    11
    indeed if you don't use CharacterController.Move, or the simpleMove.

    it won't collide properly...
     
  5. ficho

    ficho

    Joined:
    Dec 17, 2010
    Posts:
    82
    nope doesnt work
     
  6. JoshtheCoder

    JoshtheCoder

    Joined:
    May 29, 2011
    Posts:
    11
    what doesn't work?

    You know nobody is going to be able to help you unless you speak up a bit and by speaking up a bit I mean like, Posting your code and explaining a little bit more about what's going on..
     
  7. Todilo

    Todilo

    Joined:
    Feb 1, 2011
    Posts:
    88
    have you applied a rigidbody to your objects?
     
  8. ficho

    ficho

    Joined:
    Dec 17, 2010
    Posts:
    82
    Okay i will explain everything
    first im gonna make a game with ships and sea etc..
    i added the player now a cube
    added ground
    positioned everything 0,0,0
    lowered the ground(cube) to be lower than the player
    added this script to the player to move :

    Code (csharp):
    1. var smooth:float;
    2.  
    3. private var targetPosition:Vector3;
    4.  
    5. function Update () {
    6. if(Input.GetKey(KeyCode.Mouse0))
    7. {
    8. var playerPlane = new Plane(Vector3.up, transform.position);
    9. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    10. var hitdist = 0.0;
    11.  
    12. if (playerPlane.Raycast (ray, hitdist)) {
    13. var targetPoint = ray.GetPoint(hitdist);
    14. targetPosition = ray.GetPoint(hitdist);
    15. var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    16. transform.rotation = targetRotation;
    17. }
    18. }
    19.  
    20. transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
    21. }
    added character controller to the player made the radius to be equals to the cube

    added box collider to the wall (to test if it collides)

    doesnt collide

    and if i add rigidbody to the wall or the player acts werry stupid
     
  9. JoshtheCoder

    JoshtheCoder

    Joined:
    May 29, 2011
    Posts:
    11
    So the code you gave us was the player code? If so it most likely won't work with a characterController.
     
  10. ficho

    ficho

    Joined:
    Dec 17, 2010
    Posts:
    82
    then with what?
     
  11. JoshtheCoder

    JoshtheCoder

    Joined:
    May 29, 2011
    Posts:
    11
    try working with this

    Code (csharp):
    1. /// This script moves the character controller forward
    2. /// and sideways based on the arrow keys.
    3. /// It also jumps when pressing space.
    4. /// Make sure to attach a character controller to the same game object.
    5. /// It is recommended that you make only one call to Move or SimpleMove per frame.
    6.  
    7. var speed : float = 6.0;
    8. var jumpSpeed : float = 8.0;
    9. var gravity : float = 20.0;
    10.  
    11. private var moveDirection : Vector3 = Vector3.zero;
    12.  
    13. function Update() {
    14. var controller : CharacterController = GetComponent(CharacterController);
    15. if (controller.isGrounded) {
    16. // We are grounded, so recalculate
    17. // move direction directly from axes
    18. moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    19. Input.GetAxis("Vertical"));
    20. moveDirection = transform.TransformDirection(moveDirection);
    21. moveDirection *= speed;
    22.  
    23. if (Input.GetButton ("Jump")) {
    24. moveDirection.y = jumpSpeed;
    25. }
    26. }
    27.  
    28. // Apply gravity
    29. moveDirection.y -= gravity * Time.deltaTime;
    30.  
    31. // Move the controller
    32. controller.Move(moveDirection * Time.deltaTime);
    33. }
     
  12. ficho

    ficho

    Joined:
    Dec 17, 2010
    Posts:
    82
    works but its totaly diferent works with arrows can jump i can disable that and acts weird idk is there some way i can do that without applying gravity cuz its ship... and doesnt act like a ship totaly doesnt
     
  13. JoshtheCoder

    JoshtheCoder

    Joined:
    May 29, 2011
    Posts:
    11
    if you don't want gravity and jumping here it is:
    Code (csharp):
    1.  
    2. var speed : float = 6.0;
    3.  
    4. private var moveDirection : Vector3 = Vector3.zero;
    5.  
    6. function Update() {
    7. var controller : CharacterController = GetComponent(CharacterController);
    8. if (controller.isGrounded) {
    9. // We are grounded, so recalculate
    10. // move direction directly from axes
    11. moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    12. Input.GetAxis("Vertical"));
    13. moveDirection = transform.TransformDirection(moveDirection);
    14. moveDirection *= speed;
    15.  
    16. }
    17.  
    18. // Move the controller
    19. controller.Move(moveDirection * Time.deltaTime);
    20. }
    21.  
    this is untested code, but it should work...
     
  14. ficho

    ficho

    Joined:
    Dec 17, 2010
    Posts:
    82
    umm... it doesnt move at all xD
     
  15. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    since your object doesn't jump, you do not need to check if it's grounded, place a character controller on the playercube, put this code on it, make sure everything that has a collider on it, has the is Trigger option disabled.


    Code (csharp):
    1. var speed : float = 6.0;
    2. var moveDirection : Vector3 = Vector3.zero;
    3.  
    4. function Update() {
    5. var controller : CharacterController = GetComponent(CharacterController);
    6. moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    7. moveDirection = transform.TransformDirection(moveDirection);
    8. moveDirection *= speed;
    9. controller.Move(moveDirection * Time.deltaTime);
    10. }
     
  16. ficho

    ficho

    Joined:
    Dec 17, 2010
    Posts:
    82
    but as i told you i wana move on mouse click so it does need KeyCode.Mouse0
     
  17. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    you need to create a waypoint system, spawn a waypoint at the mouse coordinates (create a ray that intersects your plane and get the coords for that point and use instantiate) on the boat check every frame if there is a waypoint on the map, if there is go to it, you can use LookAt (your waypoint) to rotate and move towards it.

    if another mouseclick event happens you destroy the previous waypoint, and instantiate another one at the location mentioned above.
     
  18. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    here's some code to get you started, it spawns waypoints and it makes the character controller look at that specific waypoint.


    Movement script attached to your boat (character controller):
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class movement : MonoBehaviour {
    6.     public GameObject boat;
    7.     public GameObject wp;
    8.     public float speed;
    9.     public float distance;
    10.     public CharacterController controller;
    11.     // Use this for initialization
    12.     void Start () {
    13.         speed = 2.0f;
    14.         distance = 0.0f;
    15.         boat = GameObject.FindGameObjectWithTag("Player");
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         wp = GameObject.FindGameObjectWithTag("Waypoint");
    21.         if(wp){
    22.             distance = Vector3.Distance(boat.transform.position, wp.transform.position);
    23.             boat.transform.LookAt(wp.transform.position);
    24.             Debug.Log(distance);
    25.             if(distance > 0){
    26.                 //place your movement script here
    27.                
    28.             }
    29.                
    30.                
    31.         }
    32.        
    33.     }
    34. }
    35.  
    and here's the code for spawning waypoints: you attach it to the ground.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. public var wp : GameObject;
    5.  
    6. private var position : Vector3;
    7.  
    8.  
    9.  
    10. function Update(){
    11.  
    12.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    13.  
    14.     var hit : RaycastHit;
    15.  
    16.     if (collider.Raycast (ray, hit, 100.0)) {
    17.  
    18.         Debug.DrawLine (ray.origin, hit.point);
    19.  
    20.         position = hit.point;
    21.  
    22.         position.y = 0;
    23.  
    24.         if(Input.GetButtonDown("Fire2"))
    25.  
    26.             Instantiate(wp, position, Quaternion.identity);
    27.  
    28.     }
    29.  
    30. }
     
  19. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    ohh, i forgot about the waypoint code. you must first create a new prefab, place a sphere in it, and remove mesh renderer. make a new tag name it "Waypoint" and tag the new prefab.

    and tag your boat "Player"


    here's the waypoint code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class waypoint : MonoBehaviour {
    5.     private GameObject wp ;
    6.     // Use this for initialization
    7.     void Start () {
    8.         wp = GameObject.FindGameObjectWithTag("Waypoint");    
    9.     }
    10.  
    11.    
    12.  
    13.    
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if(Input.GetButtonDown("Fire2"))
    18.             DestroyImmediate(wp);
    19.     }
    20.  
    21. }
    22.