Unity Community


Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31

  1. Posts
    72
    I'm using the C# code, and am having an issue the Player, AI controlled Enemies, and static objects ignoring collision. I'm using Capsules, Boxes, and Cylinders as collision bodies, but everything can pass through one another. Any suggestions?


  2. Location
    Varese (Italy)
    Posts
    13
    I don't know if this is the resolution to your problem... But i have use this script for push the player on the terrain:

    Code:  
    1. TerrainGame.activeTerrain.SampleHeight(obj.transform.position)

    Where TerrainGame is a Terrain variable, so you need to assign your terrain game.
    This return exactly the height of the terrain of the player position.


  3. Posts
    72
    Should this line be applied to the characters or to the terrain itself? Should this be used to replace the other script?

    The main script posted on this thread works really well to simulate faux gravity, but I'm not sure why the characters are ignoring collision, when the same prefabs work in a normal level.


  4. Location
    Brisbane,Australia
    Posts
    210
    Thanks for answers
    -Cem-


  5. Posts
    53
    this script is great, how can you use it for bullets or rockets objects to follow the curvature of the planet ?
    I know that if you add the script currently as it is to the bullets they will follow the curvature but only if you move it yourself with the horizontal / vertical axis. I tried disabling that part of the code but it didn't work right. Any help will be appreciated.
    here it is a link to live preview what I'm working on:

    http://www.amazonarecords.com.ve/metraplayers/terminalSphere/WebPlayer/WebPlayer.html

    as you can see, if you shoot bullets go on a straight line.

    thanks


  6. Location
    Tennessee, USA
    Posts
    3,452
    You will need to fake gravity for bullets.

    Oddly, I just posted this on the subject:
    http://forum.unity3d.com/threads/102...erical-surface

    Code:  
    1. var currentY=0.0;
    2. var bulletDampen = 0.1;
    3.  
    4. function Start(){
    5. var planet=GameObject.Find("Planet").transform;
    6. currentY=(transform.position - planet.position).magnitude;
    7. }
    8.  
    9. function Update(){
    10. var planet=GameObject.Find("Planet").transform;
    11. var bullet=transform;
    12.  
    13. bullet.position=upVector * currentY;
    14. var upVector=(bullet.position - planet.position).normalized;
    15. var lookAtVector=((bullet.position + rigidbody.velocity.normalized) - planet.position).normalized;
    16.  
    17. bullet.LookAt(lookAtVector * currentY, upVector);
    18. rigidbody.veloctiy = bullet.forward * rigidbody.velocity.magnitude;
    19. currentY * = 1 - bulletDampen * Time.deltaTime;
    20. }

    What this does is to maintain bullet distance from the planet core, maintain's bullet velocity for the duration of the bullet and forces bullet drop over the curvature.

    What it does not do is to allow arcing. To do this, you would need to add bullet climb to counteract the bullet drop.


  7. Posts
    138
    hey can any one provide me the script for cube?


  8. Posts
    138
    Quote Originally Posted by Grune View Post
    Thanks a lot

    The cube are aligning semselves to the surface, one gets a jiggling. I gues it rotates and then the raycast gets a new position and it rotates again. I'm more into 3d- modeling, and now starting with javascript. My next step will be to figure out how to send the cube to the position of the raycast hit plus half the height of the Object.
    can you provide me the source code files in through drop box?


  9. Location
    UK
    Posts
    68
    Hi,

    I'm using the JS code provided by cherub earlier in this thread;

    Code:  
    1. var rotationSpeed = 120.0;
    2.  
    3. var translationSpeed = 10.0;
    4.  
    5. var height = 2.0;          //height from ground level
    6.  
    7. private var centre : Transform;            //transform for planet
    8.  
    9. private var radius : float;               //calculated radius from collider
    10.  
    11. var planet : SphereCollider ;         //collider for planet
    12.  
    13. function Start ()
    14.  
    15. {
    16.  
    17.       //consider scale applied to planet transform (assuming uniform, just pick one)
    18.  
    19.       radius = planet.radius * planet.transform.localScale.y;
    20.  
    21.       centre = planet.transform;
    22.  
    23.       //starting position at north pole
    24.  
    25.       transform.position = centre.position + Vector3(0,radius+height,0);
    26.  
    27. }
    28.  
    29. function Update ()
    30.  
    31. {
    32.  
    33.       //translate based on input      
    34.  
    35.       var inputMag  = Input.GetAxis("Vertical")*translationSpeed*Time.deltaTime;
    36.  
    37.       transform.position += transform.forward * inputMag;
    38.  
    39.         //snap position to radius + height (could also use raycasts)
    40.  
    41.       targetPosition = transform.position - centre.position;
    42.  
    43.       var ratio = (radius + height) / targetPosition.magnitude;
    44.  
    45.       targetPosition.Scale(Vector3(ratio, ratio, ratio) );
    46.  
    47.       transform.position = targetPosition + centre.position;
    48.  
    49.       //calculate planet surface normal                      
    50.  
    51.       surfaceNormal = transform.position - centre.position;
    52.  
    53.       surfaceNormal.Normalize();
    54.  
    55.       //GameObject's heading
    56.  
    57.       var headingDeltaAngle = Input.GetAxis("Horizontal") * Time.deltaTime * rotationSpeed;
    58.  
    59.       headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
    60.  
    61.       //align with surface normal
    62.  
    63.       transform.rotation = Quaternion.FromToRotation( transform.up, surfaceNormal) * transform.rotation;
    64.  
    65.       //apply heading rotation
    66.  
    67.       transform.rotation = headingDelta * transform.rotation;
    68.  
    69.    }

    I have it working to great effect but now I'm trying to modify the code to act more Asteroids-like, with wild (though variable) inertia. I can mange inertia easily enough on a flat plane but I can't get this script to move my object around a sphere AND accelerate with a ton of inertia. I've been trying to add a Rigidbody with some force behind it but it messes up and breaks the spherical movement.
    Anyone with a line of code and a quick rundown of what's involved to expand this script a little further ?


  10. Posts
    4

    move around a small planet. The Planet is not absolutely regular

    how i can make ( not regular planet) have gravity and it snap all object to its surface

    and i can walk on it

    Quote Originally Posted by Grune View Post
    Hi i am at a Student Project, which could not be solved with standart unity gravity

    Character are suposed to move around a small planet.
    The Planet is not absolutely regular so it's not possible to solve this just by radius

    do so?


  11. Posts
    81
    I've just made a robust code for it will still work on it in future but you propably won't see the code, ... untill now I've got: jump, gravity, move around planet.

    was struggling with it for more than a week, ... was wanting to use collider but dint't go well through as on collision works only 1 time while here can be made with every move witch is much better otherwise I could do it with normal collider witch I'd prefer as of physics of guns, ...

    all you need is:
    GameObject with: Character controller (prefered sphere height), Movement script
    child GameObject with: Graphics, MouseLookX script
    child GameObject with: Camera, MouseLookY script

    Scripts:
    Mouse Look X
    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseLookX : MonoBehaviour {
    5.     public float MouseSensitivityX = 1;
    6.     void Update (){
    7.         transform.localEulerAngles += new Vector3(  0, Input.GetAxis("Mouse X") * MouseSensitivityX, 0);
    8.     }
    9. }

    Mouse Look Y

    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseLookY : MonoBehaviour {
    5.     public float MouseSensitivityY = 1;
    6.     void Update(){
    7.         transform.localEulerAngles += new Vector3 (Input.GetAxis("Mouse Y") * MouseSensitivityY, 0, 0);
    8.     }
    9. }
    Some added Math functions:
    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public struct MathFunctions {
    5.     // always positive
    6.     public float Positive (float FloatToChange){
    7.         if (FloatToChange < 0){
    8.             return (FloatToChange * (-1) );
    9.         }
    10.         return FloatToChange;
    11.     }
    12.     // Distance
    13.     public float Distance (Vector3 From, Vector3 To){
    14.         float TempX;
    15.         float TempY;
    16.         float TempZ;
    17.         TempX = From.x - To.x; TempX *= TempX;
    18.         TempY = From.y - To.y; TempY *= TempY;
    19.         TempZ = From.z - To.z; TempZ *= TempZ;
    20.         return Mathf.Sqrt(TempX + TempY + TempZ);
    21.     }
    22.     public Vector3 DirectionDistancePoint (Vector3 From, Vector3 To, float Length){
    23.         return (From + (To.normalized * Length) );
    24.     }
    25.     // Distance / sec
    26.     public float Vector3SpeedPerSec (Vector3 From, Vector3 To, float InTime){
    27.         return Positive( (Distance(From, To) ) / InTime  );
    28.     }
    29.     public Vector3 Direction (Vector3 From, Vector3 To){
    30.         return (To - From).normalized;
    31.     }
    32. }
    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.     MathFunctions MF;
    6.    
    7.     public float Speed = 10;
    8.     public Transform Planet;
    9.     public Transform LocalOrientation;
    10.    
    11.     private GroundDetection GDet;
    12.    
    13.     // I need to learn how to implement it
    14.     public AnimationCurve slopeSpeedMultiplier = new AnimationCurve (new Keyframe(-90, 1), new Keyframe(0, 1), new Keyframe(90, 0));
    15.    
    16.     // character controller from now on
    17.     CharacterController Controller;
    18.     public Transform Platform;
    19.    
    20.    
    21.     public float PlanetDistance;
    22.     public float LastPlanetDistance;
    23.     Vector3 LookAtPlanet = Vector3.zero;
    24.    
    25.     // Done something in the work
    26.     public float LastHitTime = 0;
    27. //  public float Distance = 0;
    28.     public bool Grounded = false;
    29.     public Vector3 LastSlopeTestPosition = Vector3.zero;
    30.     public Vector3 LastGroundTestPosition;
    31. //  public Vector3 Velocity = Vector3.zero;
    32.     public Vector3 MovementVelocity = Vector3.zero;
    33.     public float MovingStartTime = 0;
    34.     public float MovingStartStopTime = 0;
    35.     public float MovingTime = 0;
    36. //  public float MovingToStopTime = 0;
    37.     public Vector3 TempInputVelocity = Vector3.zero;
    38.     public Vector3 JumpVelocity = Vector3.zero;
    39.     public Vector3 GravityVelocity = Vector3.zero;
    40.     public Vector3 Gravity = Vector3.zero;
    41.     public float GravityStrength = 20;
    42.     public float JumpStartDistance = 0;
    43.     public bool StillJumping = false;
    44.     public bool StillInMovement = false;
    45.     public bool StoppingMovement = false;
    46.     public float JumpStrength = 10;
    47.     public float MovementSpeed = 1;
    48.     public float AccelerationTime = 1;
    49.     public float JumpHeight = 20;
    50.    
    51.     public bool AccelerationMovement = false;
    52.    
    53.     void Start(){
    54.         Controller = GetComponent("CharacterController") as CharacterController;
    55.         // so we don't end up teleporting at x0y0z0 if something unexpected happenes
    56.         LastSlopeTestPosition = transform.position;
    57.     }
    58.    
    59.  
    60.     // why fixed update? had something in my mind that I need it forgot it why, ... seems to be working well with normal update too less calculations to be made with it
    61.     // if I'm wrong something might not be sinhronized, ... don't know, ...
    62.     void Update() {
    63.         // Align to planet
    64.         // only south pole doesn't seem to be working
    65.         transform.rotation = Quaternion.FromToRotation (Vector3.up, -(Planet.position - transform.position) );
    66.         LookAtPlanet = -transform.up;
    67.        
    68.         if (Grounded){
    69.             // 1/2 Grounded false if not colliding
    70.             IsStillGrounded();
    71.         }
    72.         // we check if we are still grounded.
    73.         if (Grounded){
    74.             InputMoveMotion();
    75.             // inside 2/2 Grounded false if press a key
    76.             InputJumpMotion();
    77.         }
    78.         // if we jump we are still not using gravity
    79.         else if (! StillJumping){
    80.             GravityMotion();
    81.         }
    82.         if (StillJumping){
    83.             ContinueJumpMotion();
    84.         }
    85.         MoveChar();
    86.     }
    87.     void IsStillGrounded (){
    88.         // we are not moving OR we are in air
    89.         if (LastHitTime < (Time.time - Time.deltaTime * 2) ){
    90.             // we are moving in mid air
    91.             if (LastGroundTestPosition != transform.position){
    92.                 Grounded = false;
    93.             }
    94.           //else {we aren't in motion}
    95.         }
    96.         LastGroundTestPosition = transform.position;
    97.     }
    98.    
    99.     public void InputMoveMotion(){
    100.         Vector3 ImputVelocity = Vector3.zero;
    101. //      if (/*AI*/){
    102. //          ImputVelocity +=    /*all the moves*/
    103. //          ImputVelovity.Normalize();
    104. //      }
    105. //      else {
    106.             if (Input.GetKey(KeyCode.UpArrow)){
    107.                 ImputVelocity += ( LocalOrientation.transform.forward);
    108.             }
    109.             if (Input.GetKey(KeyCode.DownArrow)){
    110.                 ImputVelocity += ( -LocalOrientation.transform.forward);
    111.             }
    112.            
    113.             if (Input.GetKey(KeyCode.LeftArrow)){
    114.                 ImputVelocity += ( -LocalOrientation.transform.right);
    115.             }
    116.             if (Input.GetKey(KeyCode.RightArrow)){
    117.                 ImputVelocity += ( LocalOrientation.transform.right);
    118.             }
    119.             // direction length to 1 unit
    120.             ContinueMoveMotion(ImputVelocity.normalized);
    121. //      }
    122.     }
    123.     void ContinueMoveMotion(Vector3 InputVelocity){
    124.         if (! AccelerationMovement){
    125.             MovementVelocity = InputVelocity;
    126.             return ;
    127.         }
    128.        
    129.        
    130.         // seems to be working more tests need to be done, ...
    131.        
    132.         if (InputVelocity != Vector3.zero){
    133.             TempInputVelocity = InputVelocity;
    134.             // we didn't ended the stop
    135.             if (StoppingMovement){
    136.                 StoppingMovement = false;
    137.                 MovingStartTime = Time.time - MovingTime;
    138.             }
    139.             // we are just starting to move
    140.             else if (LastGroundTestPosition == transform.position && ! StillInMovement){
    141.                 MovingStartTime = Time.time;
    142.             }
    143.             StillInMovement = true;
    144.             if (AccelerationTime > (Time.time - MovingStartTime)){
    145.                 MovingTime = (Time.time - MovingStartTime);
    146.             }
    147.             else {
    148.                 MovingTime = AccelerationTime;
    149.             }
    150.             // where we will move
    151.             MovementVelocity = InputVelocity * MovingTime;
    152.         }
    153.         else {
    154.             if (StillInMovement && ! StoppingMovement){
    155.                 StoppingMovement = true;
    156.                 MovingStartStopTime = Time.time + MovingTime;
    157.             }
    158.             // I don't need this but so it doesn't calculate all the time but just veryfy the bool
    159.             if (StoppingMovement){
    160.                 MovingTime = MovingStartStopTime - Time.time;
    161.             }
    162.             // if we are inside stopping time
    163.             if (MovingTime >= 0){
    164.                 MovementVelocity = TempInputVelocity * MovingTime;
    165.             }
    166.             // if we stopped totally
    167.             else {
    168.                 MovementVelocity = Vector3.zero;
    169.                 StillInMovement = false;
    170.                 StoppingMovement = false;
    171.             }
    172.         }
    173.        
    174.     }
    175.     void InputJumpMotion(){
    176.         // 2/2 if we jump we know we aren't grounded
    177.         if (Input.GetMouseButton(2)){
    178.             JumpVelocity = transform.up * JumpStrength;
    179.             Grounded = false;
    180.             StillJumping = true;
    181.         }
    182.         JumpStartDistance = MF.Distance(transform.position, Planet.position);
    183.     }
    184.     void ContinueJumpMotion(){
    185.         PlanetDistance = MF.Distance(transform.position, Planet.position);
    186.         // if we stop pressing jump we stop getting in air
    187.         if (Input.GetMouseButtonUp(2)){
    188.             StillJumping = false;
    189.             JumpVelocity = Vector3.zero;
    190.         }
    191.         // if we reached our jump height we stop getting more in height
    192.         else if (JumpStartDistance + JumpHeight < PlanetDistance){
    193.             StillJumping = false;
    194.             JumpVelocity = Vector3.zero;
    195.         }
    196.         else if (StillJumping){
    197.             JumpVelocity -= (-transform.up * 0.3f * Time.deltaTime);
    198.         }
    199.     }
    200.     void GravityMotion (){
    201.         GravityVelocity = MF.Direction(transform.position, Planet.position).normalized * GravityStrength;
    202.     }
    203.     void MoveChar(){
    204.         if (StillJumping){
    205.             GravityVelocity = Vector3.zero;
    206.         }
    207.         if (Grounded){
    208.             Controller.Move( (MovementVelocity * MovementSpeed) * Time.deltaTime);
    209.         }
    210.         else {
    211.             Controller.Move(  ( (MovementVelocity * MovementSpeed) + JumpVelocity + GravityVelocity) * Time.deltaTime);
    212.         }
    213.     }
    214.         // the only indicator we are grounded
    215.         // if it's less than 45 we are grounded
    216.         float CurrentSlope = (Vector3.Angle(LookAtPlanet, hit.normal) -180 )*-1;
    217.         if (Controller.slopeLimit > CurrentSlope){
    218.             Grounded = true;
    219.             LastHitTime = Time.time;
    220.             LastSlopeTestPosition = transform.position;
    221.         }
    222.         // if we are still grounded means we are on 45 + slope
    223.         else if (Grounded && Controller.slopeLimit < CurrentSlope){
    224.             // we need to teleport back to slope below 45
    225.             transform.position = LastSlopeTestPosition;
    226.         }
    227.        
    228.         /****** START DEBUG Hit COMPONENTS ******
    229.         // world point
    230. //      Debug.Log("Point: " + hit.point);
    231.         // oposite of direction
    232. //      Debug.Log("Normal: " + hit.normal); // World Direction
    233.         // direction witch was forced back
    234. //      Debug.Log("MoveDirection: " + hit.moveDirection); // Local Direction & something strange unexplained about it
    235.         // more length that's stopped bigger number it'll return
    236. //      Debug.Log("moveLength: " + hit.moveLength);
    237. //      Platform = hit.transform;
    238.         /****** END DEBUG Hit COMPONENTS ******/
    239.     }
    240. }

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •